Skip to content

Commit

Permalink
Update terraform.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-AbdulRehman-Aamer authored Aug 22, 2024
1 parent 263dea5 commit b0c5104
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,82 @@
This is a basic workflow to help you get started with Actions

name: 'Terraform Cloud'

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "terraform"
terraform:
name: 'Terraform'
# The type of runner that the job will run on
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout

uses: actions/checkout@v3
uses: actions/checkout@v2

- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

# Check for proper terraform formatting
- name: Terraform Format
id: fmt
run: terraform fmt -check

# Initialize Terraform
- name: Terraform Init
id: init
run: terraform init

# Terraform plan on pull requests only
- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: terraform plan -no-color
continue-on-error: true

# Add terraform plan output back into the pull request
- name: Update Pull Request
uses: actions/github-script@v6
uses: actions/github-script@0.9.0
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Cloud Plan Output')
});
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
@@ -64,7 +76,14 @@ jobs:
<details><summary>Show Plan</summary>
\`\`\`
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.issues.createComment({
if (botComment) {
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
});
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
# Exit on Plan failure
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1

# Terraform apply on push to main branch
- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve

0 comments on commit b0c5104

Please sign in to comment.