From 4ecbd4e9132f1fd4f3a94cf64a064ccf3bff58f6 Mon Sep 17 00:00:00 2001 From: Viv B Date: Thu, 26 Sep 2024 14:24:35 +1000 Subject: [PATCH] feat(cd): add github workflow for cd --- .github/workflows/build-push-deploy.yaml | 104 +++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 .github/workflows/build-push-deploy.yaml diff --git a/.github/workflows/build-push-deploy.yaml b/.github/workflows/build-push-deploy.yaml new file mode 100644 index 000000000..6bc99d639 --- /dev/null +++ b/.github/workflows/build-push-deploy.yaml @@ -0,0 +1,104 @@ +name: Build, Push, Deploy + +on: + push: + branches: + - uat + - prod +env: + IMAGE_NAME: www-mckinsey + DEV_REGISTRY: mckinsey-cloud-infra-eks-dev-docker.jfrog.io + PROD_REGISTRY: mckinsey-cloud-infra-eks-converge-prod-docker.jfrog.io + DEV_DOCKER_USERNAME: ${{ secrets.DEV_DOCKER_USERNAME }} + DEV_DOCKER_PASSWORD: ${{ secrets.DEV_DOCKER_PASSWORD }} + PROD_DOCKER_USERNAME: ${{ secrets.PROD_DOCKER_USERNAME }} + PROD_DOCKER_PASSWORD: ${{ secrets.PROD_DOCKER_PASSWORD }} + DEV_DEPLOYMENT_REPO: https://github.com/McK-Internal/cloud_infra_eks_dev + PROD_DEPLOYMENT_REPO: https://github.com/McK-Internal/cloud_infra_eks_converge_prod + DEV_DEPLOYMENT_PAT: ${{ secrets.DEV_DEPLOYMENT_PAT }} + PROD_DEPLOYMENT_PAT: ${{ secrets.PROD_DEPLOYMENT_PAT }} + +jobs: + + build: + runs-on: ubuntu-latest + outputs: + commit: ${{ steps.vars.outputs.commit }} + steps: + + - name: Set environment variables based on branch + run: | + if [[ ${{ github.ref }} == 'refs/heads/uat' ]]; then + echo "REGISTRY=${{ env.DEV_REGISTRY }}" >> $GITHUB_ENV + elif [[ ${{ github.ref }} == 'refs/heads/prod' ]]; then + echo "REGISTRY=${{ env.PROD_REGISTRY }}" >> $GITHUB_ENV + fi + + - uses: actions/checkout@v3 + + - name: Get short SHA of the commit + run: | + echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + + - name: Build Docker image + run: | + docker build . -t $REGISTRY/${{ env.IMAGE_NAME }}:$SHORT_SHA + + - name: Set output + id: vars + run: echo "commit=$SHORT_SHA" >> $GITHUB_OUTPUT + + - name: Login to image registry + uses: docker/login-action@v1 + with: + username: ${{ env.DOCKER_USERNAME }} + password: ${{ env.DOCKER_PASSWORD }} + registry: $REGISTRY + + - name: Push Docker image + run: | + docker push $REGISTRY/${{ env.IMAGE_NAME }}:$SHORT_SHA + + deploy: + needs: build + runs-on: ubuntu-latest + steps: + + - name: Set environment variables based on branch + run: | + if [[ ${{ github.ref }} == 'refs/heads/uat' ]]; then + echo "DEPLOYMENT_REPO=${{ env.DEV_DEPLOYMENT_REPO }}" >> $GITHUB_ENV + echo "DEPLOYMENT_PAT=${{ env.DEV_DEPLOYMENT_PAT }}" >> $GITHUB_ENV + echo "DEPLOYMENT_DIR=./resources" >> $GITHUB_ENV + echo "DEPLOYMENT_BRANCH=main" >> $GITHUB_ENV + elif [[ ${{ github.ref }} == 'refs/heads/prod' ]]; then + echo "DEPLOYMENT_REPO=${{ env.PROD_DEPLOYMENT_REPO }}" >> $GITHUB_ENV + echo "DEPLOYMENT_PAT=${{ env.PROD_DEPLOYMENT_PAT }}" >> $GITHUB_ENV + echo "DEPLOYMENT_DIR=./prod/resources" >> $GITHUB_ENV + echo "DEPLOYMENT_BRANCH=cd/${{ env.IMAGE_NAME }}-${{ needs.build.output.commit }}" >> $GITHUB_ENV + fi + + - name: Clone deployment repo + uses: actions/checkout@v3 + with: + repository: ${{ env.DEPLOYMENT_REPO }} + token: ${{ env.DEPLOYMENT_PAT }} + path: 'deployment-repo' + ref: main + + - name: Update YAML files with the new tag + working-directory: deployment-repo + run: | + sed -i \ + "s/${{ env.REGISTRY }}\/${{ env.IMAGE_NAME }}:[^ ]*/${{ env.REGISTRY }}\/${{ env.IMAGE_NAME }}:${{ needs.build.outputs.commit }}/g" \ + ${{ env.DEPLOYMENT_DIR }}/*.yaml + + - name: Commit and push changes + working-directory: deployment-repo + run: | + if [[ "${{ env.DEPLOYMENT_BRANCH }}" != "main" ]]; then + git checkout -b ${{ env.DEPLOYMENT_BRANCH }} + fi + git add ${{ env.DEPLOYMENT_DIR }}/*.yaml + git diff --staged --quiet || git commit -m "Update ${{ env.IMAGE_NAME }} image tags to ${{ needs.build.outputs.commit }}" + git push --set-upstream origin ${{ env.DEPLOYMENT_BRANCH }}