Update ci.yml #92
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 | |
permissions: | |
contents: read | |
pull-requests: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Upload Configuration | |
uses: hashicorp/tfc-workflows-github/actions/upload-configuration@v1.0.0 | |
id: plan-upload | |
with: | |
workspace: ${{ env.TF_WORKSPACE }} | |
directory: ${{ env.CONFIG_DIRECTORY }} | |
speculative: true | |
- name: Create Plan Run | |
uses: hashicorp/tfc-workflows-github/actions/create-run@v1.0.0 | |
id: plan-run | |
with: | |
workspace: ${{ env.TF_WORKSPACE }} | |
configuration_version: ${{ steps.plan-upload.outputs.configuration_version_id }} | |
plan_only: true | |
- name: Get Plan Output | |
uses: hashicorp/tfc-workflows-github/actions/plan-output@v1.0.0 | |
id: plan-output | |
with: | |
plan: ${{ fromJSON(steps.plan-run.outputs.payload).data.relationships.plan.data.id }} | |
- name: Update Pull Request | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const output = `#### Terraform Format and Style <img draggable="false" role="img" class="emoji" alt="🖌" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f58c.svg">\` | |
#### Terraform Initialization <img draggable="false" role="img" class="emoji" alt="🤖" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f916.svg">\` | |
#### Terraform Validation <img draggable="false" role="img" class="emoji" alt="🤖" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f916.svg">\` | |
#### Terraform Plan <img draggable="false" role="img" class="emoji" alt="📖" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f4d6.svg">\` | |
<details><summary>Show Plan</summary> | |
\`\`\`\n\n\`\`\` | |
</details> | |
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; | |
const { owner, repo } = context.repo; | |
const { number } = context.payload.pull_request || {}; | |
if (number) { | |
await github.rest.issues.createComment({ | |
issue_number: number, | |
owner, | |
repo, | |
body: output | |
}); | |
} else { | |
console.log('No pull request context found. Creating a new pull request...'); | |
const newPullRequest = await github.rest.pulls.create({ | |
owner, | |
repo, | |
title: 'Your pull request title here', | |
head: 'main', // Replace with the name of your branch | |
base: 'main', // Replace with the name of the base branch | |
body: 'Your pull request description here' | |
}); | |
console.log('New pull request created:', newPullRequest.data); | |
} | |
- name: Terraform Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 | |
- name: Terraform Apply | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
run: terraform apply -auto-approve |