version: 2.1
orbs: snyk: snyk/[email protected] docker: circleci/[email protected] heroku: circleci/[email protected]
jobs: build: docker: - image: cimg/openjdk:17.0.7 parameters: dockerhub-username: type: string dockerhub-password: type: string steps: - checkout - run: mvn clean package -DskipTests=true
test:
docker:
- image: cimg/openjdk:17.0.7
- image: cimg/postgres:15.2-postgis
environment:
POSTGRES_PORT:
# pulling source code from github repository into test job's workspace
# Dockerize is a utility that helps you to set up your applications
# in Docker containers, wait tells Dockerize to wait up to 1 minute
# for the PostgreSQL database to be ready before running the tests
steps:
- checkout
- run: dockerize -wait tcp://localhost:5432 -timeout 1m
- run:
name: Run Tests and rebuild the project
command: mvn clean install
scan: docker: - image: cimg/openjdk:17.0.7 steps: - setup_remote_docker - checkout # Build the Docker image - run: docker build -t $DOCKER_LOGIN/$IMAGE_NAME:latest . # Login to Docker Hub using environment variables - run: echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_LOGIN}" --password-stdin # Push the image to Docker Hub - run: docker push $DOCKER_LOGIN/$IMAGE_NAME:latest # Run Snyk scan on the Docker image - snyk/scan: docker-image-name: $DOCKER_LOGIN/$IMAGE_NAME:latest severity-threshold: critical
build-and-push: # executor is a Docker image that contains a pre-installed Docker client executor: docker/docker
# set up remote environment for Docker commands
# needed for building, pushing and pulling Docker images
steps:
- setup_remote_docker
- checkout
# Command provided by Docker orb to check for semantic errors in Dockerfiles
- docker/check
# Build the Docker image
# both the image name and tag are set using environment variables
# and pipeline.git.tag is the tag of the git commit
- docker/build:
image: $DOCKER_LOGIN/$IMAGE_NAME
tag: <<pipeline.git.tag>>
# Push the Docker image to Docker Hub
- docker/push:
image: $DOCKER_LOGIN/$IMAGE_NAME
tag: <<pipeline.git.tag>>
pull-and-deploy: docker: - image: cimg/openjdk:17.0.7 steps: - setup_remote_docker - heroku/install - run: name: Pull Docker Image From Docker Hub command: docker pull $DOCKER_LOGIN/$IMAGE_NAME:latest - run: name: Heroku Container Push With Image From Docker Hub command: | heroku container:login docker tag $DOCKER_LOGIN/$IMAGE_NAME:latest registry.heroku.com/$HEROKU_APP_NAME/web # format --- docker tag registry.heroku.com//web docker push registry.heroku.com/$HEROKU_APP_NAME/web # format --- docker push registry.heroku.com//web heroku container:release web -a $HEROKU_APP_NAME
workflows:
simple_workflow: jobs: # Build job triggered on changes to the main branch - build: dockerhub-username: $DOCKER_LOGIN dockerhub-password: $DOCKER_PASSWORD filters: branches: only: main
# Test job runs after the build job and only on the main branch
- test:
# to let `test` job run after the `build` job
requires:
- build
filters:
branches:
only: main
- scan:
requires:
- test
# Build-and-push job runs after the test job
- build-and-push:
requires:
- test
# Deploy job runs after the build-and-push job
- pull-and-deploy:
requires:
- build-and-push