Update ci.yml #97
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
name: 'Terraform Cloud' | |
on: | |
push: | |
branches: [ main ] | |
pull_request: | |
env: | |
TF_CLOUD_ORGANIZATION: "Evilness-ACE" | |
TF_API_TOKEN: "${{ secrets.TF_API_TOKEN }}" | |
TF_WORKSPACE: "K3S-OCI" | |
CONFIG_DIRECTORY: "./" | |
jobs: | |
terraform: | |
name: 'Terraform' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: hashicorp/tfc-workflows-github/actions/[email protected] | |
id: run | |
## run may fail, if so continue to output PR comment | |
## step.terraform-cloud-check-run-status will fail job after pr comment is created/updated. | |
continue-on-error: true | |
with: | |
workspace: ${{ env.TF_WORKSPACE }} | |
configuration_version: ${{ steps.upload.outputs.configuration_version_id }} | |
plan_only: true | |
## OPTIONAL: set your own message for run. A default message will be defined for you. | |
## Example: | |
# message: "Triggered From GitHub Actions CI ${{ github.sha }}" | |
- uses: hashicorp/tfc-workflows-github/actions/[email protected] | |
id: plan-output | |
with: | |
plan: ${{ steps.run.outputs.plan_id }} | |
## REQUIRED: Workflow permissions: `Read and write permissions` | |
- uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' | |
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 Cloud Plan Output | |
\`\`\`\n | |
Plan: ${{ steps.plan-output.outputs.add }} to add, ${{ steps.plan-output.outputs.change }} to change, ${{ steps.plan-output.outputs.destroy }} to destroy. | |
\`\`\` | |
[Terraform Cloud Plan](${{ steps.run.outputs.run_link }}) | |
` | |
// 3. If we have a comment, update it, otherwise create a new one | |
if (botComment) { | |
github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: botComment.id, | |
body: output | |
}) | |
} else { | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
} | |
## Check Run Status, if not planned_and_finished fail the job | |
- id: terraform-cloud-check-run-status | |
if: ${{ steps.run.outputs.run_status != 'planned_and_finished'}} | |
run: | | |
echo "Terraform Cloud Run Failed or Requires Further Attention" | |
echo "Run Status: '${{ steps.run.outputs.run_status }}'" | |
echo "${{ steps.run.outputs.run_link }}" | |
exit 1 | |
- name: Terraform Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 |