-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update workflow * add work dir * add work dir * add work dir * update workflow
- Loading branch information
Showing
3 changed files
with
137 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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 }} |