Update ci.yml #93
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/[email protected] | |
id: plan-upload | |
with: | |
workspace: ${{ env.TF_WORKSPACE }} | |
directory: ${{ env.CONFIG_DIRECTORY }} | |
speculative: true | |
- name: Create Plan Run | |
uses: hashicorp/tfc-workflows-github/actions/[email protected] | |
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/[email protected] | |
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 commits = await github.rest.repos.listCommits({ | |
owner, | |
repo, | |
sha: context.sha | |
}); | |
const commitMessages = commits.data.map(commit => commit.commit.message); | |
const title = commitMessages[0]; // Use the first commit message as the title | |
const body = commitMessages.join('\n\n'); // Join all commit messages as the body | |
const newPullRequest = await github.rest.pulls.create({ | |
owner, | |
repo, | |
title, | |
head: 'dev-deployments', // Replace with the name of your feature branch | |
base: 'main', | |
body | |
}); | |
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 |