diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml new file mode 100644 index 0000000..14ad745 --- /dev/null +++ b/.github/workflows/terraform.yml @@ -0,0 +1,130 @@ +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: "cloud-resume-api" + CONFIG_DIRECTORY: "./" + +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 + + - name: Terraform Init + id: init + run: terraform init + continue-on-error: true + + - 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 + + - name: Terraform Validate + id: validate + run: terraform validate -no-color + continue-on-error: true + + - name: Terraform Plan + id: plan + if: github.event_name == 'pull_request' + run: terraform plan -no-color -input=false + continue-on-error: true + 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 }}\` +
Validation Output + + \`\`\`\n + ${{ steps.validate.outputs.stdout }} + \`\`\` + +
+ + #### Terraform Plan 📖 \`${{ steps.plan.outcome }}\` + +
Show Plan + + \`\`\`terraform\n + ${process.env.PLAN} + \`\`\` + +
`; + + 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.validate.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' + run: terraform apply -auto-approve -input=false \ No newline at end of file