Terraform + Ansible Github Actions #17
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
pull_request: | |
branches: | |
- main | |
- tf-actions | |
#paths: | |
# - 'tf/environments/production/**' | |
env: | |
tf_actions_working_dir: "./tf/environments/production" | |
jobs: | |
terraform: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ${{ env.tf_actions_working_dir }} | |
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<<EOF" >> "$GITHUB_OUTPUT" | |
terraform validate -no-color >> "$GITHUB_OUTPUT" | |
echo "EOF" >> "$GITHUB_OUTPUT" | |
- name: Terraform Plan | |
id: plan | |
run: | | |
echo "terraform_plan<<EOF" >> "$GITHUB_OUTPUT" | |
terraform plan -no-color >> "$GITHUB_OUTPUT" | |
echo "EOF" >> "$GITHUB_OUTPUT" | |
continue-on-error: true | |
- uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const body = `### Terraform Run Output 🤖 | |
#### Format and Style 🖌\`${{ steps.fmt.outcome }}\` | |
#### Initialization ⚙️\`${{ steps.init.outcome }}\` | |
#### Validation 🤖${{ steps.validate.outcome }}\` | |
<details><summary>Validation Output</summary> | |
\`\`\`\n | |
${{ steps.validate.outputs.terraform_validate }} | |
\`\`\` | |
</details> | |
#### 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.includes('### Terraform Run Output') | |
}) | |
if (existingBotComment) { | |
github.rest.issues.updateComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: existingBotComment.id, | |
body | |
}) | |
} else { | |
github.rest.issues.createComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body | |
}) | |
} | |
} |