From e388a3c0861fc5ec64e3cb71c0aded9f8256716a Mon Sep 17 00:00:00 2001 From: Andrea Morabito <78792023+and-mora@users.noreply.github.com> Date: Tue, 19 Nov 2024 09:50:37 +0100 Subject: [PATCH] chore: Add deploy workflow (#16) --- .github/workflows/build-n-push-main.yml | 9 +-- .github/workflows/deploy.yml | 67 +++++++++++++++++++ .github/workflows/post-merge.yml | 12 ++-- .../env/{dev => cstar-d-weu-rtp}/backend.ini | 0 .../{dev => cstar-d-weu-rtp}/backend.tfvars | 0 .../{dev => cstar-d-weu-rtp}/terraform.tfvars | 0 .../env/{prod => cstar-p-weu-rtp}/backend.ini | 0 .../{prod => cstar-p-weu-rtp}/backend.tfvars | 0 .../terraform.tfvars | 0 .../env/{uat => cstar-u-weu-rtp}/backend.ini | 0 .../{uat => cstar-u-weu-rtp}/backend.tfvars | 0 .../{uat => cstar-u-weu-rtp}/terraform.tfvars | 0 12 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/deploy.yml rename src/main/terraform/env/{dev => cstar-d-weu-rtp}/backend.ini (100%) rename src/main/terraform/env/{dev => cstar-d-weu-rtp}/backend.tfvars (100%) rename src/main/terraform/env/{dev => cstar-d-weu-rtp}/terraform.tfvars (100%) rename src/main/terraform/env/{prod => cstar-p-weu-rtp}/backend.ini (100%) rename src/main/terraform/env/{prod => cstar-p-weu-rtp}/backend.tfvars (100%) rename src/main/terraform/env/{prod => cstar-p-weu-rtp}/terraform.tfvars (100%) rename src/main/terraform/env/{uat => cstar-u-weu-rtp}/backend.ini (100%) rename src/main/terraform/env/{uat => cstar-u-weu-rtp}/backend.tfvars (100%) rename src/main/terraform/env/{uat => cstar-u-weu-rtp}/terraform.tfvars (100%) diff --git a/.github/workflows/build-n-push-main.yml b/.github/workflows/build-n-push-main.yml index 4210bc8..6218c56 100644 --- a/.github/workflows/build-n-push-main.yml +++ b/.github/workflows/build-n-push-main.yml @@ -1,4 +1,4 @@ -name: Build, push and update +name: Deploy from branch to DEV on: workflow_dispatch: @@ -42,13 +42,14 @@ jobs: uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1 - name: Build the app image + id: image uses: docker/build-push-action@5cd11c3a4ced054e52742c5fd54dca954e0edd85 # v6.7.0 with: push: true context: . file: src/main/docker/Dockerfile platforms: linux/amd64 - tags: ghcr.io/${{ github.repository }}:latest, ghcr.io/${{ github.repository }}:${{ steps.semantic.outputs.new_release_version }}-RC + tags: ghcr.io/${{ github.repository }}:latest, ghcr.io/${{ github.repository }}:${{ github.ref_name }} secrets: | "gh_token=${{ secrets.GIT_PAT }}" @@ -63,5 +64,5 @@ jobs: ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}" ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}" run: | - terraform init -backend-config="env/dev/backend.tfvars" -reconfigure - terraform apply -var-file="env/dev/terraform.tfvars" -var="rtp_activator_image=${{ steps.build_image.outputs.image }}" -auto-approve -lock-timeout=300s + terraform init -backend-config="env/cstar-d-weu-rtp/backend.tfvars" -reconfigure + terraform apply -var-file="env/cstar-d-weu-rtp/terraform.tfvars" -var="rtp_activator_image=ghcr.io/${{ github.repository }}:${{ github.ref_name }}@${{ steps.image.outputs.digest }}" -auto-approve -lock-timeout=300s diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..097123f --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,67 @@ +name: Deploy + +on: + workflow_dispatch: + inputs: + environment: + type: environment + description: Environment where to deploy + image-hash: + type: string + required: false + default: '' + description: SHA256 of the image to deploy + image-tag: + type: string + required: false + default: 'latest' + description: Tag of the image to deploy + +jobs: + deploy: + runs-on: ubuntu-22.04 + + environment: ${{ inputs.environment }} + + permissions: + id-token: write + + steps: + # + # Setup Terraform + # + - name: Setup Terraform + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 + with: + terraform_version: 1.9.7 + + # + # Checkout the source code + # + - name: Checkout the source code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # 4.2.2 + + # + # Terraform + # + - name: Terraform + shell: bash + working-directory: src/main/terraform + env: + IMAGE_HASH: "${{ inputs.image-hash }}" + IMAGE_TAG: "${{ inputs.image-tag }}" + ENVIRONMENT: "${{ inputs.environment }}" + ARM_CLIENT_ID: "${{ secrets.AZURE_CLIENT_ID }}" + ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}" + ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}" + run: | + if [ -n "$IMAGE_HASH" ]; then + IMAGE="ghcr.io/${{ github.repository }}:$IMAGE_TAG@sha256:$IMAGE_HASH" + else + IMAGE="ghcr.io/${{ github.repository }}:$IMAGE_TAG" + fi + echo "[$IMAGE] will be deployed." + + terraform init -backend-config="env/$ENVIRONMENT/backend.tfvars" -reconfigure + + terraform apply -var-file="env/$ENVIRONMENT/terraform.tfvars" -var="rtp_activator_image=$IMAGE" -auto-approve -lock-timeout=300s diff --git a/.github/workflows/post-merge.yml b/.github/workflows/post-merge.yml index 936a88e..8bd41b3 100644 --- a/.github/workflows/post-merge.yml +++ b/.github/workflows/post-merge.yml @@ -126,8 +126,8 @@ jobs: ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}" ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}" run: | - terraform init -backend-config="env/dev/backend.tfvars" -reconfigure - terraform apply -var-file="env/dev/terraform.tfvars" -var="rtp_activator_image=ghcr.io/${{ github.repository }}:${{ needs.post_merge.outputs.new_version }}-RC@${{ steps.rc-image.outputs.digest }}" -auto-approve -lock-timeout=300s + terraform init -backend-config="env/cstar-d-weu-rtp/backend.tfvars" -reconfigure + terraform apply -var-file="env/cstar-d-weu-rtp/terraform.tfvars" -var="rtp_activator_image=ghcr.io/${{ github.repository }}:${{ needs.post_merge.outputs.new_version }}-RC@${{ steps.rc-image.outputs.digest }}" -auto-approve -lock-timeout=300s # # # # Install Newman. @@ -215,8 +215,8 @@ jobs: ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}" ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}" run: | - terraform init -backend-config="env/dev/backend.tfvars" -reconfigure - terraform apply -var-file="env/dev/terraform.tfvars" -var="rtp_activator_image=ghcr.io/${{ github.repository }}:${{ needs.post_merge.outputs.new_version }}@${{ steps.stable-image.outputs.digest }}" -auto-approve -lock-timeout=300s + terraform init -backend-config="env/cstar-d-weu-rtp/backend.tfvars" -reconfigure + terraform apply -var-file="env/cstar-d-weu-rtp/terraform.tfvars" -var="rtp_activator_image=ghcr.io/${{ github.repository }}:${{ needs.post_merge.outputs.new_version }}@${{ steps.stable-image.outputs.digest }}" -auto-approve -lock-timeout=300s uat_deployment: needs: [ post_merge, dev_deployment] @@ -256,5 +256,5 @@ jobs: ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}" ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}" run: | - terraform init -backend-config="env/uat/backend.tfvars" -reconfigure - terraform apply -var-file="env/uat/terraform.tfvars" -var="rtp_activator_image=ghcr.io/${{ github.repository }}:${{ needs.post_merge.outputs.new_version }}@${{ needs.dev_deployment.outputs.digest }}" -auto-approve -lock-timeout=300s + terraform init -backend-config="env/cstar-u-weu-rtp/backend.tfvars" -reconfigure + terraform apply -var-file="env/cstar-u-weu-rtp/terraform.tfvars" -var="rtp_activator_image=ghcr.io/${{ github.repository }}:${{ needs.post_merge.outputs.new_version }}@${{ needs.dev_deployment.outputs.digest }}" -auto-approve -lock-timeout=300s diff --git a/src/main/terraform/env/dev/backend.ini b/src/main/terraform/env/cstar-d-weu-rtp/backend.ini similarity index 100% rename from src/main/terraform/env/dev/backend.ini rename to src/main/terraform/env/cstar-d-weu-rtp/backend.ini diff --git a/src/main/terraform/env/dev/backend.tfvars b/src/main/terraform/env/cstar-d-weu-rtp/backend.tfvars similarity index 100% rename from src/main/terraform/env/dev/backend.tfvars rename to src/main/terraform/env/cstar-d-weu-rtp/backend.tfvars diff --git a/src/main/terraform/env/dev/terraform.tfvars b/src/main/terraform/env/cstar-d-weu-rtp/terraform.tfvars similarity index 100% rename from src/main/terraform/env/dev/terraform.tfvars rename to src/main/terraform/env/cstar-d-weu-rtp/terraform.tfvars diff --git a/src/main/terraform/env/prod/backend.ini b/src/main/terraform/env/cstar-p-weu-rtp/backend.ini similarity index 100% rename from src/main/terraform/env/prod/backend.ini rename to src/main/terraform/env/cstar-p-weu-rtp/backend.ini diff --git a/src/main/terraform/env/prod/backend.tfvars b/src/main/terraform/env/cstar-p-weu-rtp/backend.tfvars similarity index 100% rename from src/main/terraform/env/prod/backend.tfvars rename to src/main/terraform/env/cstar-p-weu-rtp/backend.tfvars diff --git a/src/main/terraform/env/prod/terraform.tfvars b/src/main/terraform/env/cstar-p-weu-rtp/terraform.tfvars similarity index 100% rename from src/main/terraform/env/prod/terraform.tfvars rename to src/main/terraform/env/cstar-p-weu-rtp/terraform.tfvars diff --git a/src/main/terraform/env/uat/backend.ini b/src/main/terraform/env/cstar-u-weu-rtp/backend.ini similarity index 100% rename from src/main/terraform/env/uat/backend.ini rename to src/main/terraform/env/cstar-u-weu-rtp/backend.ini diff --git a/src/main/terraform/env/uat/backend.tfvars b/src/main/terraform/env/cstar-u-weu-rtp/backend.tfvars similarity index 100% rename from src/main/terraform/env/uat/backend.tfvars rename to src/main/terraform/env/cstar-u-weu-rtp/backend.tfvars diff --git a/src/main/terraform/env/uat/terraform.tfvars b/src/main/terraform/env/cstar-u-weu-rtp/terraform.tfvars similarity index 100% rename from src/main/terraform/env/uat/terraform.tfvars rename to src/main/terraform/env/cstar-u-weu-rtp/terraform.tfvars