From e19a5c64d2df7a3e4fe0a3230d36c0adb274481c Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sat, 28 Sep 2024 22:11:53 +1000 Subject: [PATCH 01/21] Added extra plan display --- .github/workflows/deploy-to-gcp.yaml | 39 +++++++++++++++++++++++++++- Taskfile.yml | 10 ++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 74a65f3..43ed2e5 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -1,6 +1,16 @@ name: Deploy Xplorers API to GCP -on: push +on: + push: + paths: + - "**.tf" + - "**.hcl" + - "**.tpl" + pull_request: + paths: + - "**.tf" + - "**.hcl" + - "**.tpl" permissions: contents: read @@ -37,7 +47,34 @@ jobs: sudo snap install task --classic - name: Terraform Plan + if: github.event_name == 'pull_request' run: task terraform-plan + - name: Update Pull Request + uses: actions/github-script@v6 + if: github.event_name == 'pull_request' + env: + PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" + with: + result-encoding: string + script: | + const output = + +
Show Plan + + \`\`\`\n + ${process.env.PLAN} + \`\`\` + +
+ + *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: output + }) - name: Terraform Apply if: github.ref == 'refs/heads/"main"' && github.event_name == 'push' diff --git a/Taskfile.yml b/Taskfile.yml index 0e9e633..96ea53f 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -108,16 +108,24 @@ tasks: terraform init \ -backend-config="bucket=${ARTIFACTS_BUCKET_NAME}" \ -backend-config="prefix=${ARTIFACTS_BUCKET_TERRAFORM_PREFIX}" + - terraform validate -no-color \ + -var "artifacts_bucket_name=${ARTIFACTS_BUCKET_NAME}" \ + -var "project_id=${GOOGLE_CLOUD_PROJECT_ID}" \ + -var "project_number=${GOOGLE_PROJECT_NUMBER}" \ + -var "region=${GOOGLE_CLOUD_PROJECT_REGION}" \ + -var "docker_image_tag={{.DOCKER_IMAGE_TAG}}" terraform-plan: dir: terraform desc: Creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure. cmds: + - task: teraform validate -no-color - task: terraform-init - task: switch-to-terraform-workspace - task: package - | terraform plan \ + -no-color \ -var "artifacts_bucket_name=${ARTIFACTS_BUCKET_NAME}" \ -var "project_id=${GOOGLE_CLOUD_PROJECT_ID}" \ -var "project_number=${GOOGLE_PROJECT_NUMBER}" \ @@ -134,7 +142,7 @@ tasks: - task: package - task: build-xplorers-backend-image - | - terraform apply -auto-approve \ + terraform apply -no-color -auto-approve \ -var "artifacts_bucket_name=${ARTIFACTS_BUCKET_NAME}" \ -var "project_id=${GOOGLE_CLOUD_PROJECT_ID}" \ -var "project_number=${GOOGLE_PROJECT_NUMBER}" \ From e70910868d0e74ad5c96d9ba69f66b329c6dc24a Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sat, 28 Sep 2024 22:13:14 +1000 Subject: [PATCH 02/21] Fix id --- .github/workflows/deploy-to-gcp.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 43ed2e5..837f47a 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -48,7 +48,9 @@ jobs: - name: Terraform Plan if: github.event_name == 'pull_request' + id: plan run: task terraform-plan + - name: Update Pull Request uses: actions/github-script@v6 if: github.event_name == 'pull_request' From 30ab6339a7fe30f16821213db02d1577c0427576 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sat, 28 Sep 2024 22:18:18 +1000 Subject: [PATCH 03/21] Fix id --- .github/workflows/deploy-to-gcp.yaml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 837f47a..caca133 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -1,16 +1,7 @@ name: Deploy Xplorers API to GCP -on: - push: - paths: - - "**.tf" - - "**.hcl" - - "**.tpl" - pull_request: - paths: - - "**.tf" - - "**.hcl" - - "**.tpl" +- on: push + permissions: contents: read From 4d26c8674bdcc8184f93d8ff62348e48c133256a Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sat, 28 Sep 2024 22:19:08 +1000 Subject: [PATCH 04/21] Fix id --- .github/workflows/deploy-to-gcp.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index caca133..151f190 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -1,7 +1,5 @@ name: Deploy Xplorers API to GCP - -- on: push - +on: push permissions: contents: read From 0d2067382d552f8e2de1068eb36061da9c9c4df9 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sat, 28 Sep 2024 22:21:23 +1000 Subject: [PATCH 05/21] Fix id --- .github/workflows/deploy-to-gcp.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 151f190..9b51ae5 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -36,7 +36,7 @@ jobs: sudo snap install task --classic - name: Terraform Plan - if: github.event_name == 'pull_request' + if: github.event_name == 'push' id: plan run: task terraform-plan From d827a60ce8149dfcd091b309965d05ebea007ba3 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sat, 28 Sep 2024 22:23:44 +1000 Subject: [PATCH 06/21] Fix id --- Taskfile.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/Taskfile.yml b/Taskfile.yml index 96ea53f..7a1285e 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -119,7 +119,6 @@ tasks: dir: terraform desc: Creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure. cmds: - - task: teraform validate -no-color - task: terraform-init - task: switch-to-terraform-workspace - task: package From 54cff3bdb91a845afcee85ae20744b45b7a3bdcd Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sat, 28 Sep 2024 22:24:43 +1000 Subject: [PATCH 07/21] Fix id --- Taskfile.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 7a1285e..cf8a450 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -108,12 +108,7 @@ tasks: terraform init \ -backend-config="bucket=${ARTIFACTS_BUCKET_NAME}" \ -backend-config="prefix=${ARTIFACTS_BUCKET_TERRAFORM_PREFIX}" - - terraform validate -no-color \ - -var "artifacts_bucket_name=${ARTIFACTS_BUCKET_NAME}" \ - -var "project_id=${GOOGLE_CLOUD_PROJECT_ID}" \ - -var "project_number=${GOOGLE_PROJECT_NUMBER}" \ - -var "region=${GOOGLE_CLOUD_PROJECT_REGION}" \ - -var "docker_image_tag={{.DOCKER_IMAGE_TAG}}" + - terraform validate -no-color terraform-plan: dir: terraform From 012c5c136625d73c23058f5df5b6a2bd54357c5a Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 00:17:41 +1000 Subject: [PATCH 08/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 9b51ae5..a70019e 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -36,7 +36,7 @@ jobs: sudo snap install task --classic - name: Terraform Plan - if: github.event_name == 'push' + if: github.event_name == 'push' || github.event_name == 'pull_request' id: plan run: task terraform-plan From bd1deafab9a8ccee498bd779301a713711b4c09e Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 00:23:27 +1000 Subject: [PATCH 09/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index a70019e..fc9bfe8 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -1,5 +1,7 @@ name: Deploy Xplorers API to GCP -on: push +on: + push: + pull_request: permissions: contents: read From ff903ae96541862ebeef5c42551b647627ff6ee1 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 00:31:35 +1000 Subject: [PATCH 10/21] Fix body --- .github/workflows/deploy-to-gcp.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index fc9bfe8..2292016 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -50,8 +50,7 @@ jobs: with: result-encoding: string script: | - const output = - + const output = `#### TASK Terraform Plan 📖\`${{ steps.plan.outcome }}\`
Show Plan \`\`\`\n From bdc3d5bda6259b02c1af45ee2348041bf6d07f30 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 00:36:28 +1000 Subject: [PATCH 11/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 2292016..a88d902 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -52,15 +52,11 @@ jobs: script: | const output = `#### TASK Terraform Plan 📖\`${{ steps.plan.outcome }}\`
Show Plan - \`\`\`\n ${process.env.PLAN} \`\`\` -
- *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; - github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, From 413676109b8759ffa21dcd01a0e04af4f571fbd6 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 00:42:38 +1000 Subject: [PATCH 12/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index a88d902..c52433b 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -11,6 +11,10 @@ jobs: deploy-xplorers-api: name: Deploy Xplorers API to GCP runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + repository-projects: write defaults: run: From dab090052aee446fc45d5b9f977bb3bb0e31cc71 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 00:44:38 +1000 Subject: [PATCH 13/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index c52433b..d022584 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -4,17 +4,16 @@ on: pull_request: permissions: - contents: read id-token: write + contents: write + pull-requests: write + repository-projects: write jobs: deploy-xplorers-api: name: Deploy Xplorers API to GCP runs-on: ubuntu-latest - permissions: - contents: write - pull-requests: write - repository-projects: write + defaults: run: From f225bc6710a61f8625a0cf83a88f726dfa9b4c23 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 00:48:07 +1000 Subject: [PATCH 14/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index d022584..17b9363 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -41,7 +41,7 @@ jobs: sudo snap install task --classic - name: Terraform Plan - if: github.event_name == 'push' || github.event_name == 'pull_request' + if: github.event_name == 'pull_request' id: plan run: task terraform-plan From dfb19846533410feff0fcba481d1c115e79cbf6b Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 19:09:44 +1000 Subject: [PATCH 15/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 17b9363..45c53b7 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -7,7 +7,6 @@ permissions: id-token: write contents: write pull-requests: write - repository-projects: write jobs: deploy-xplorers-api: From 8e66503d4971a110e1e3ef3e1781c17230b4ef79 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Wed, 2 Oct 2024 19:19:10 +1000 Subject: [PATCH 16/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index 45c53b7..ef9456c 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -2,48 +2,37 @@ name: Deploy Xplorers API to GCP on: push: pull_request: - permissions: id-token: write contents: write pull-requests: write - jobs: deploy-xplorers-api: name: Deploy Xplorers API to GCP runs-on: ubuntu-latest - - defaults: run: shell: bash - steps: - name: Checkout uses: actions/checkout@v4 - - id: auth uses: google-github-actions/auth@v2.0.0 with: workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }} - - name: Setup Terraform uses: hashicorp/setup-terraform@v1 - - name: Setup pnpm uses: pnpm/action-setup@v4 with: version: 9 - - name: Install taskfile run: | sudo snap install task --classic - - name: Terraform Plan if: github.event_name == 'pull_request' id: plan run: task terraform-plan - - name: Update Pull Request uses: actions/github-script@v6 if: github.event_name == 'pull_request' From 129dbbac1f05650e52a0f33ff3ab662f393beb79 Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sun, 6 Oct 2024 21:35:01 +1100 Subject: [PATCH 17/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index ef9456c..d2a1b52 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -43,9 +43,11 @@ jobs: script: | const output = `#### TASK Terraform Plan 📖\`${{ steps.plan.outcome }}\`
Show Plan + \n \`\`\`\n ${process.env.PLAN} \`\`\` + \n
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; github.rest.issues.createComment({ From 20d9e799bdfe24237bcb30c7d88370adfce7ef7d Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Sun, 6 Oct 2024 21:52:59 +1100 Subject: [PATCH 18/21] Update deploy-to-gcp.yaml --- .github/workflows/deploy-to-gcp.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy-to-gcp.yaml b/.github/workflows/deploy-to-gcp.yaml index d2a1b52..11bf8c3 100644 --- a/.github/workflows/deploy-to-gcp.yaml +++ b/.github/workflows/deploy-to-gcp.yaml @@ -2,6 +2,8 @@ name: Deploy Xplorers API to GCP on: push: pull_request: + types: + - ready_for_review permissions: id-token: write contents: write @@ -56,7 +58,3 @@ jobs: repo: context.repo.repo, body: output }) - - - name: Terraform Apply - if: github.ref == 'refs/heads/"main"' && github.event_name == 'push' - run: task terraform-apply From c0a327dfe3f2f8a2110627ef5c003eaad5ddebcd Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Mon, 7 Oct 2024 11:23:20 +1100 Subject: [PATCH 19/21] Create terraform-apply-actions --- .github/workflows/terraform-apply-actions | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/terraform-apply-actions diff --git a/.github/workflows/terraform-apply-actions b/.github/workflows/terraform-apply-actions new file mode 100644 index 0000000..54ab9d2 --- /dev/null +++ b/.github/workflows/terraform-apply-actions @@ -0,0 +1,41 @@ +name: Terraform Workflow + +on: + push: + branches: + - master + workflow_dispatch: + inputs: + action: + description: 'Terraform action to perform' + required: true + default: 'plan' + type: choice + options: + - plan + - apply + +jobs: + terraform: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + - id: auth + uses: google-github-actions/auth@v2.0.0 + with: + workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }} + - name: Setup Terraform + uses: hashicorp/setup-terraform@v1 + + - name: Terraform Init + run: terraform init + + - name: Terraform Plan + if: github.event.inputs.action == 'plan' + run: task terraform-plan + + - name: Terraform Apply + if: github.event.inputs.action == 'apply' + run: task terraform-apply From e9e6dc0d262d22f700f5cc72dc5f42943ed2ff9f Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Mon, 7 Oct 2024 11:28:08 +1100 Subject: [PATCH 20/21] Update terraform-apply-actions --- .github/workflows/terraform-apply-actions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/terraform-apply-actions b/.github/workflows/terraform-apply-actions index 54ab9d2..a1ea73b 100644 --- a/.github/workflows/terraform-apply-actions +++ b/.github/workflows/terraform-apply-actions @@ -3,7 +3,7 @@ name: Terraform Workflow on: push: branches: - - master + - main workflow_dispatch: inputs: action: From 883b36882f966901883ea4464bccbb2fd9c62dde Mon Sep 17 00:00:00 2001 From: Samrat Pant Date: Mon, 7 Oct 2024 12:49:00 +1100 Subject: [PATCH 21/21] Fix indent --- .github/workflows/terraform-apply-actions | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/terraform-apply-actions b/.github/workflows/terraform-apply-actions index a1ea73b..9e5f196 100644 --- a/.github/workflows/terraform-apply-actions +++ b/.github/workflows/terraform-apply-actions @@ -14,11 +14,9 @@ on: options: - plan - apply - jobs: terraform: runs-on: ubuntu-latest - steps: - name: Checkout uses: actions/checkout@v4 @@ -28,14 +26,11 @@ jobs: workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }} - name: Setup Terraform uses: hashicorp/setup-terraform@v1 - - name: Terraform Init run: terraform init - - name: Terraform Plan if: github.event.inputs.action == 'plan' run: task terraform-plan - - name: Terraform Apply if: github.event.inputs.action == 'apply' - run: task terraform-apply + run: task terraform-apply \ No newline at end of file