init cicd #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI and CD | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
jobs: | |
# ============== | |
# CI task | |
# ============== | |
quality-check: | |
name: Quality Scan | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 | |
- uses: sonarsource/sonarqube-scan-action@master | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} | |
# If you wish to fail your job when the Quality Gate is red, uncomment the | |
# following lines. This would typically be used to fail a deployment. | |
- uses: sonarsource/sonarqube-quality-gate-action@master | |
timeout-minutes: 5 | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
build-and-push-docker-image: | |
name: Build Docker image and push to repositories | |
runs-on: ubuntu-latest | |
needs: quality-check | |
steps: | |
- uses: actions/checkout@v3 | |
- 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-training/devops-go-example:${{ env.GITHUB_SHA }} | |
- name: Integration test inside Docker-compose cleanup | |
run: docker-compose -f docker-compose.test.yml down | |
- name: Integration test inside Docker-compose | |
run: docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit --exit-code-from it_tests | |
- name: Login ghcr.io | |
uses: docker/login-action@v1.8.0 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
logout: true | |
- name: Push to GitHub Container Registry | |
uses: docker/build-push-action@v2 | |
with: | |
context: . | |
tags: | | |
ghcr.io/aorjoa-training/devops-go-example:${{ env.GITHUB_SHA }} | |
# build on feature branches, push only on main branch | |
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@v2 | |
with: | |
repository: aorjoa-training/devops-argocd | |
persist-credentials: false | |
fetch-depth: 0 | |
- name: change image tag | |
run: | | |
git --version | |
git config user.name "aorjoa" | |
git config user.email "root@aorjoa.link" | |
sed -i -E "s/ghcr.io\/aorjoa-training\/devops-go-example.*$/ghcr.io\/aorjoa-training\/devops-go-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-training/devops-argocd | |
branch: main |