-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: ci-enhancement
Are you sure you want to change the base?
Changes from 20 commits
e19a5c6
e709108
30ab633
4d26c86
0d20673
d827a60
54cff3b
012c5c1
bd1deaf
ff903ae
bdc3d5b
4136761
dab0900
f225bc6
dfb1984
8e66503
129dbba
20d9e79
c0a327d
e9e6dc0
883b368
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: Terraform Workflow | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
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/[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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification 🛠️ Refactor suggestion Missing backend configuration in Terraform Init
🔗 Analysis chainPin Terraform setup action version and consider adding backend configuration.
- name: Setup Terraform
uses: hashicorp/[email protected] # Or the latest specific version
- name: Terraform Init
run: |
terraform init \
-backend-config="bucket=${{ secrets.TF_STATE_BUCKET }}" \
-backend-config="prefix=terraform/state" Don't forget to add the To ensure the Terraform configuration includes a backend, run: This will help verify that a backend is defined in your Terraform configuration. 🏁 Scripts executedThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👌
Are you able to test this by running this on your branch (removing hardcoded main on L6)? Just to be sure that this works?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But did my testing here https://github.com/samurato/gh-actions-test