From e9fd49dd79faf97205a5411cc214ae3adb30195c Mon Sep 17 00:00:00 2001 From: Quang Le Date: Fri, 8 Nov 2024 14:25:58 +0700 Subject: [PATCH] ci: add cicd --- .github/workflows/build-and-push-dev.yml | 69 +++++++++++++++++++++++ .github/workflows/build-and-push-prod.yml | 68 ++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 .github/workflows/build-and-push-dev.yml create mode 100644 .github/workflows/build-and-push-prod.yml diff --git a/.github/workflows/build-and-push-dev.yml b/.github/workflows/build-and-push-dev.yml new file mode 100644 index 0000000..11a56a8 --- /dev/null +++ b/.github/workflows/build-and-push-dev.yml @@ -0,0 +1,69 @@ +name: Dev - Build and Push to Artifact Registry + +on: + push: + branches: + - 'develop' + - 'cicd' + +env: + PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} + AR_REGION: asia-southeast1 + REPOSITORY: icy + IMAGE_NAME: backend + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Google Auth + id: auth + uses: 'google-github-actions/auth@v0' + with: + credentials_json: '${{ secrets.GCP_CREDENTIALS }}' + + - name: Set up Cloud SDK + uses: 'google-github-actions/setup-gcloud@v0' + + - name: Configure Docker + run: | + gcloud auth configure-docker ${{ env.AR_REGION }}-docker.pkg.dev --quiet + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Build and push final Docker image + uses: docker/build-push-action@v2 + with: + context: . + push: true + tags: | + ${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + ${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:latest + cache-from: type=gha + cache-to: type=gha,mode=max + + # Setup kustomize + - name: Setup kustomize + run: | + curl -o kustomize --location https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64 + chmod u+x ./kustomize + mv kustomize /tmp + - name: Checkout dwarvesf/infrastructure + uses: actions/checkout@master + with: + repository: dwarvesf/infrastructure + token: ${{ secrets.GH_PAT }} + path: ./infrastructure + ref: main + - name: Update api version + run: | + cd ./infrastructure/icy/backend/$K8S_ENVIRONMENT + git config user.name lmquang + git config user.email quanglm.ops@gmail.com + /tmp/kustomize edit set image ${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}=${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + git commit -am "[skip ci] icy-backend ${K8S_ENVIRONMENT} image update" + git push origin main diff --git a/.github/workflows/build-and-push-prod.yml b/.github/workflows/build-and-push-prod.yml new file mode 100644 index 0000000..2414d6c --- /dev/null +++ b/.github/workflows/build-and-push-prod.yml @@ -0,0 +1,68 @@ +name: Prod - Build and Push to Artifact Registry + +on: + push: + tags: + - 'v*' + +env: + PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} + AR_REGION: asia-southeast1 + REPOSITORY: icy + IMAGE_NAME: backend + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Google Auth + id: auth + uses: 'google-github-actions/auth@v0' + with: + credentials_json: '${{ secrets.GCP_CREDENTIALS }}' + + - name: Set up Cloud SDK + uses: 'google-github-actions/setup-gcloud@v0' + + - name: Configure Docker + run: | + gcloud auth configure-docker ${{ env.AR_REGION }}-docker.pkg.dev --quiet + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Build and push final Docker image + uses: docker/build-push-action@v2 + with: + context: . + push: true + tags: | + ${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + ${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:latest + cache-from: type=gha + cache-to: type=gha,mode=max + + # Setup kustomize + - name: Setup kustomize + run: | + curl -o kustomize --location https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64 + chmod u+x ./kustomize + mv kustomize /tmp + - name: Checkout dwarvesf/infrastructure + uses: actions/checkout@master + with: + repository: dwarvesf/infrastructure + token: ${{ secrets.GH_PAT }} + path: ./infrastructure + ref: main + - name: Update api version + run: | + cd ./infrastructure/icy/backend/$K8S_ENVIRONMENT + git config user.name lmquang + git config user.email quanglm.ops@gmail.com + /tmp/kustomize edit set image ${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}=${{ env.AR_REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + git commit -am "[skip ci] icy-backend ${K8S_ENVIRONMENT} image update" + git push origin main