Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update workflow #7

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions .github/workflows/terraform-apply.yml

This file was deleted.

95 changes: 0 additions & 95 deletions .github/workflows/terraform-plan.yml

This file was deleted.

137 changes: 137 additions & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
name: Terraform

on:
pull_request:
branches:
- main

push:
branches:
- main

env:
TF_CLOUD_ORGANIZATION: "${{ secrets.TF_CLOUD_ORGANIZATION }}"
TF_API_TOKEN: "${{ secrets.TF_API_TOKEN }}"
TF_WORKSPACE: "learn-terraform"
CONFIG_DIRECTORY: "./learn-terraform-vnet"

jobs:
terraform:
name: Plan / Apply
runs-on: ubuntu-latest
defaults:
run:
working-directory: terraform
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Use Terraform 1.3.7
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.10.2

- uses: hashicorp/setup-terraform@v3
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

- name: Terraform Format
id: fmt
run: terraform fmt -write
continue-on-error: true
working-directory: ${{ env.CONFIG_DIRECTORY }}

- name: Terraform Init
id: init
run: terraform init
continue-on-error: true
working-directory: ${{ env.CONFIG_DIRECTORY }}

# - name: Mask Azure Subscription ID
# run: |
# SUBSCRIPTION_ID=$(terraform plan | grep -oP '/subscriptions/\K[a-f0-9-]+')
# if [ -n "$SUBSCRIPTION_ID" ]; then
# echo "::add-mask::$SUBSCRIPTION_ID"
# fi
# working-directory: ${{ env.CONFIG_DIRECTORY }}

# - name: Terraform Validate
# id: validate
# run: terraform validate -no-color
# continue-on-error: true
# working-directory: ${{ env.CONFIG_DIRECTORY }}

- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: terraform plan -no-color -input=false
continue-on-error: true
working-directory: ${{ env.CONFIG_DIRECTORY }}
env:
ARM_DEBUG: "false"

- name: Pull Request Comment
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
PLAN: "${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
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')
})

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>

\`\`\`terraform\n
${process.env.PLAN}
\`\`\`

</details>`;

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
})
}

- name: Terraform Status
if: steps.plan.outcome == 'failure' || steps.init.outcome == 'failure' || steps.fmt.outcome == 'failure'
run: exit 1

- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push' && contains(github.event.head_commit.message, 'Merge pull request')
run: terraform apply -auto-approve -input=false
working-directory: ${{ env.CONFIG_DIRECTORY }}
Loading