Skip to content

Commit

Permalink
Update ci.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiraalA committed Apr 2, 2024
1 parent 2a1e63c commit 3a27063
Showing 1 changed file with 33 additions and 54 deletions.
87 changes: 33 additions & 54 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
name: Terraform Cloud
name: 'Terraform Cloud'

Check failure on line 1 in .github/workflows/ci.yml

View workflow job for this annotation

GitHub Actions / scan

CKV2_GHA_1: "Ensure top-level permissions are not set to write-all"

on:
push:
branches: [main]
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
Expand All @@ -44,58 +35,46 @@ jobs:
with:
plan: ${{ fromJSON(steps.plan-run.outputs.payload).data.relationships.plan.data.id }}

- name: Update Pull Request
uses: actions/github-script@v7
- name: Update PR
uses: actions/github-script@v6
id: plan-comment
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 { 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
\`\`\`
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.plan-run.outputs.run_link }})
`;
if (botComment) {
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
});
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);
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1

- name: Terraform Init
run: terraform init

- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
Expand Down

0 comments on commit 3a27063

Please sign in to comment.