Skip to content

Terraform + Ansible Github Actions #14

Terraform + Ansible Github Actions

Terraform + Ansible Github Actions #14

on:
pull_request:
branches:
- main
- tf-actions
#paths:
# - 'tf/environments/production/**'
jobs:
terraform:
runs-on: ubuntu-latest
defaults:
run:
working-directory: "./tf/environments/production"
permissions:
pull-requests: write
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
TF_VAR_aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
TF_VAR_aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
TF_VAR_datadog_api_key: ${{ secrets.DATADOG_API_KEY}}
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
- name: Terraform fmt
id: fmt
run: terraform fmt -check
continue-on-error: true
- name: Terraform Init
id: init
run: terraform init
- name: Terraform Validate
id: validate
run: echo "terraform_validate=$(terraform validate -no-color)" >> "$GITHUB_OUTPUT"
- name: Terraform Plan
id: plan
run: echo "terraform_plan=$(terraform plan -no-color)" >> "$GITHUB_OUTPUT"
continue-on-error: true
- uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const body = `### Terraform Run Output 🤖
#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>
\`\`\`\n
${{ steps.validate.outputs.terraform_validate }}
\`\`\`
</details>
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${{ steps.plan.outputs.terraform_plan }}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`,
Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*
Last updated: ${(new Date()).toUTCString()};
`;
const prNumber = context.payload.pull_request.number
if (prNumber) {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
})
const existingBotComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.startsWith('### Terraform Run Output 🤖')
})
if (existingBotComment) {
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body
})
} else {
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body
})
}
}