Skip to content

Commit

Permalink
initial ci/cd
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuridech committed Mar 18, 2024
1 parent a614f08 commit d15f5b0
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: CI and CD

on:
push:
branches: [ main ]

jobs:
# ==============
# CI task
# ==============
quality-check:
name: Quality Scan
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Cache SonarQube packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Build and analyze
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: ./gradlew build sonar --info

build-and-push-docker-image:
name: Build docker image and push to repositories
runs-on: ubuntu-latest
needs: quality-check

steps:
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17

- uses: actions/checkout@v4

- name: Run test
run: ./gradlew test

- name: Setup SHA
run: echo "GITHUB_SHA=${GITHUB_SHA}" >> $GITHUB_ENV

- name: Build the Docker image
run: docker build . --file Dockerfile --tag ghcr.io/aorjoa-bootcamp/devops-java-example:${{ env.GITHUB_SHA }}

- name: Login ghcr.io
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: ${{ github.ref == 'refs/heads/main' }}
tags: ${{ env.GITHUB_SHA }}

- name: Push to GitHub Container Registry
uses: docker/build-push-action@v2
with:
context: .
tags: |
ghcr.io/aorjoa-bootcamp/devops-java-example:${{ env.GITHUB_SHA }}
push: ${{ github.ref == 'refs/heads/main' }}

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
# ==============
# CD task
# ==============
gitops-versioning:
runs-on: ubuntu-latest
needs: build-and-push-docker-image
steps:
- name: checkout
uses: actions/checkout@v4
with:
repository: aorjoa-bootcamp/devops-argocd
persist-credentials: false
fetch-depth: 0
- name: change image tag
run: |
git --version
git config user.name "aorjoa"
git config user.email "[email protected]"
sed -i -E "s/ghcr.io\/aorjoa-bootcamp\/devops-java-example.*$/ghcr.io\/aorjoa-bootcamp\/devops-java-example:${GITHUB_SHA}/" kube-gitops/deployment.yml
git add kube-gitops/deployment.yml
git commit -m "🤖 change docker image version to ${GITHUB_SHA}"
- name: push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.PAT }}
repository: aorjoa-bootcamp/devops-argocd
branch: main

0 comments on commit d15f5b0

Please sign in to comment.