Skip to content

Instantly share code, notes, and snippets.

@pathakashish
Last active July 18, 2022 06:39
Show Gist options
  • Save pathakashish/6a1449168eb870ce65ba68c12d1776f2 to your computer and use it in GitHub Desktop.
Save pathakashish/6a1449168eb870ce65ba68c12d1776f2 to your computer and use it in GitHub Desktop.
CI/CD for simple spring boot app which uses docker. Here is github action and circle ci file which do the same thing.
version: 2.1
jobs:
build:
docker:
- image: gradle:alpine
steps:
- checkout
- run: ./gradlew bootJar
- persist_to_workspace:
root: ./
paths:
- build/
- .gradle/
test:
docker:
- image: gradle:alpine
steps:
- checkout
- run: ./gradlew test
- store_test_results:
path: build/test-results
package:
docker:
- image: docker:dind
steps:
- setup_remote_docker:
version: 19.03.13
- checkout
- attach_workspace:
at: ./
- run: docker build --iidfile image.txt -t registry.heroku.com/ashish-demo-app/web:${CIRCLE_SHA1} .
- run: docker login --username=_ --password=$HEROKU_TOKEN registry.heroku.com
- run: docker push registry.heroku.com/ashish-demo-app/web:${CIRCLE_SHA1}
- persist_to_workspace:
root: ./
paths:
- image.txt
deploy:
docker:
- image: docker:dind
steps:
- setup_remote_docker:
version: 19.03.13
- attach_workspace:
at: ./
- run: apk add --no-cache curl
- run:
name: "Patch the container hosted in heroku"
command: |
curl -X PATCH https://api.heroku.com/apps/ashish-demo-app/formation --header "Content-Type: application/json" --header "Accept: application/vnd.heroku+json; version=3.docker-releases" --header "Authorization: Bearer ${HEROKU_TOKEN}" --data '{ "updates": [ { "type": "web", "docker_image": "'$(cat image.txt)'" } ] }'
workflows:
version: 2
build_and_test:
jobs:
- build
- test
- package:
requires:
- build
- deploy:
requires:
- test
- package
name: Demo CI/CD for bootcamp
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
java-version: '8'
distribution: 'adopt'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew bootJar
# Using cache instead of artifacts to avoid reaching the artifacts free quota size
- name: Cache jar file for next job
uses: actions/cache@v2
with:
path: build/libs/ashish-dev-boot-camp-demp-0.0.1-SNAPSHOT.jar
key: boopapp-jar-${{ github.sha }}
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
java-version: '8'
distribution: 'adopt'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Execute all tests
run: ./gradlew test
packageAndDeploy:
needs: [build, test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: build/libs/ashish-dev-boot-camp-demp-0.0.1-SNAPSHOT.jar
key: boopapp-jar-${{ github.sha }}
restore-keys: |
boopapp-jar
- name: Build the tagged Docker image
run: docker build --iidfile image.txt --build-arg JAR_FILE=build/libs/ashish-dev-boot-camp-demp-0.0.1-SNAPSHOT.jar -t registry.heroku.com/ashish-demo-app/web:${GITHUB_SHA} .
- name: Login to DockerHub Registry
run: docker login --username=_ --password=${{ secrets.HEROKU_TOKEN }} registry.heroku.com
- name: Push docker image to heroku
run: docker push registry.heroku.com/ashish-demo-app/web:${GITHUB_SHA}
- name: Patch the container
run: |
curl -X PATCH https://api.heroku.com/apps/ashish-demo-app/formation --header "Content-Type: application/json" --header "Accept: application/vnd.heroku+json; version=3.docker-releases" --header "Authorization: Bearer ${{ secrets.HEROKU_TOKEN }}" --data '{ "updates": [ { "type": "web", "docker_image": "'$(cat image.txt)'" } ] }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment