Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create terraform-apply-actions #16

Open
wants to merge 21 commits into
base: ci-enhancement
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions .github/workflows/deploy-to-gcp.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
name: Deploy Xplorers API to GCP

on: push

on:
push:
pull_request:
types:
- ready_for_review
permissions:
contents: read
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/[email protected]
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: Terraform Apply
if: github.ref == 'refs/heads/"main"' && github.event_name == 'push'
run: task terraform-apply
- 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 = `#### TASK Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\n
\`\`\`\n
${process.env.PLAN}
\`\`\`
\n
</details>
*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
})
41 changes: 41 additions & 0 deletions .github/workflows/terraform-apply-actions
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Terraform Workflow

on:
push:
branches:
- main
workflow_dispatch:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👌
image

Are you able to test this by running this on your branch (removing hardcoded main on L6)? Just to be sure that this works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a limitation workflow dispatch works on main branch only regardless of which branch we declared in workflow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/[email protected]
with:
workload_identity_provider: ${{ secrets.GOOGLE_CLOUD_WORKLOAD_IDENTITY_PROVIDER }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1

- name: Terraform Init
run: terraform init
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

🛠️ Refactor suggestion

Missing backend configuration in Terraform Init

  • The Terraform initialization does not include backend configuration, which is essential for state management in CI/CD environments.
🔗 Analysis chain

Pin Terraform setup action version and consider adding backend configuration.

  1. Pin the version of the Terraform setup action to ensure consistency:
- name: Setup Terraform
  uses: hashicorp/[email protected]  # Or the latest specific version
  1. Consider adding backend configuration to the Terraform init step. This is important for state management, especially in a CI/CD context:
- name: Terraform Init
  run: |
    terraform init \
      -backend-config="bucket=${{ secrets.TF_STATE_BUCKET }}" \
      -backend-config="prefix=terraform/state"

Don't forget to add the TF_STATE_BUCKET secret to your GitHub repository secrets.

To ensure the Terraform configuration includes a backend, run:

This will help verify that a backend is defined in your Terraform configuration.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if Terraform configuration includes a backend
grep -R 'backend "' .

Length of output: 6531

samurato marked this conversation as resolved.
Show resolved Hide resolved

- 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance error handling and output capturing for Terraform steps.

While the conditional execution of plan and apply steps is correct, consider adding error handling and output capturing to improve the workflow's robustness and observability.

Here's an improved version of these steps:

- name: Terraform Plan
  if: github.event.inputs.action == 'plan'
  run: |
    task terraform-plan 2>&1 | tee plan_output.txt
    if [ ${PIPESTATUS[0]} -ne 0 ]; then
      echo "Terraform plan failed"
      exit 1
    fi

- name: Terraform Apply
  if: github.event.inputs.action == 'apply'
  run: |
    task terraform-apply 2>&1 | tee apply_output.txt
    if [ ${PIPESTATUS[0]} -ne 0 ]; then
      echo "Terraform apply failed"
      exit 1
    fi

- name: Upload Terraform Output
  if: always()
  uses: actions/upload-artifact@v3
  with:
    name: terraform-output
    path: |
      plan_output.txt
      apply_output.txt

This change adds error checking and uploads the output as artifacts, which can be useful for debugging and auditing.

4 changes: 3 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ tasks:
terraform init \
-backend-config="bucket=${ARTIFACTS_BUCKET_NAME}" \
-backend-config="prefix=${ARTIFACTS_BUCKET_TERRAFORM_PREFIX}"
- terraform validate -no-color

terraform-plan:
dir: terraform
Expand All @@ -118,6 +119,7 @@ tasks:
- 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}" \
Expand All @@ -134,7 +136,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}" \
Expand Down
Loading