update terraform and add k8s files #9
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 Plan" | |
on: | |
pull_request: | |
paths: | |
- 'terraform/**' | |
env: | |
TF_WORKSPACE: "default" | |
CONFIG_DIRECTORY: "./terraform" | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
jobs: | |
terraform: | |
name: "Terraform Plan" | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ${{ env.CONFIG_DIRECTORY }} | |
permissions: | |
contents: read | |
id-token: write | |
pull-requests: write | |
env: | |
TF_VAR_state_bucket: ${{ secrets.TF_VAR_STATE_BUCKET }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Assume AWS Credentials | |
id: assume | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-region: ${{ secrets.AWS_REGION }} | |
role-session-name: ${{ github.actor }} | |
role-to-assume: ${{ secrets.AWS_ROLE_ARN }} | |
- uses: hashicorp/setup-terraform@v3 | |
- 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: terraform validate -no-color | |
- name: Terraform Plan | |
id: plan | |
run: terraform plan -no-color | |
continue-on-error: true | |
- uses: actions/github-script@v7 | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | |
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 Format and Style') | |
}) | |
// 2. Prepare format of the comment | |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` | |
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` | |
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` | |
<details><summary>Validation Output</summary> | |
\`\`\`\n | |
${{ steps.validate.outputs.stdout }} | |
\`\`\` | |
</details> | |
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\` | |
<details><summary>Show Plan</summary> | |
\`\`\`\n | |
${process.env.PLAN} | |
\`\`\` | |
</details> | |
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.CONFIG_DIRECTORY }}\`, Workflow: \`${{ github.workflow }}\`*`; | |
// 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 | |
}) | |
} |