Skip to content
Merged
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
44 changes: 44 additions & 0 deletions .github/workflows/terraform-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,33 @@ jobs:
terraform-plan:
name: Terraform Plan
runs-on: ubuntu-latest
# Runs after terraform-docs so it knows whether docs pushed a commit.
# !cancelled() keeps a docs failure from skipping the plan.
needs: terraform-docs
if: ${{ !cancelled() }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCS_SHA: ${{ needs.terraform-docs.outputs.docs_sha }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

permissions:
id-token: write
contents: read
pull-requests: write
statuses: write

steps:
# Check runs attach to the commit that triggered the workflow. When
# terraform-docs pushes its [skip ci] commit, that commit becomes the PR head
# and has no checks, so the PR page shows none. Mirror the plan's result onto
# it as a commit status.
- name: Mark plan pending on the docs commit
if: ${{ needs.terraform-docs.outputs.docs_sha != '' }}
run: |
gh api "repos/${{ github.repository }}/statuses/$DOCS_SHA" \
-f state=pending -f context="Terraform Plan" \
-f description="Plan running" -f target_url="$RUN_URL"

- name: Checkout code
uses: actions/checkout@v4
- name: Configure AWS Credentials
Expand All @@ -36,6 +54,16 @@ jobs:
backend_config_file: terraform/prod.backend.tfvars
variables: iam_only = false

- name: Report plan result on the docs commit
if: ${{ always() && needs.terraform-docs.outputs.docs_sha != '' }}
env:
RESULT: ${{ job.status }}
run: |
if [ "$RESULT" = success ]; then state=success; else state=failure; fi
gh api "repos/${{ github.repository }}/statuses/$DOCS_SHA" \
-f state="$state" -f context="Terraform Plan" \
-f description="Plan $RESULT" -f target_url="$RUN_URL"

terraform-docs:
name: Generate Terraform Docs
runs-on: ubuntu-latest
Expand All @@ -44,6 +72,9 @@ jobs:
contents: write
pull-requests: write

outputs:
docs_sha: ${{ steps.head.outputs.docs_sha }}

steps:
- uses: actions/checkout@v3
with:
Expand All @@ -61,3 +92,16 @@ jobs:
# first_time_contributors approval policy gates, so every PR touching .tf
# needed a manual approval for a plan that was already green one commit earlier.
git-commit-message: "terraform-docs: automated updates to Terraform modules README.md [skip ci]"

# Output the docs commit's SHA only if this job made it directly on top of
# the commit that triggered the run. Empty if docs had nothing to change, or
# if someone pushed to the branch in between (their push gets its own run).
- name: Find the docs commit
id: head
env:
EVENT_SHA: ${{ github.event.pull_request.head.sha }}
run: |
head=$(git rev-parse HEAD)
if [ "$head" != "$EVENT_SHA" ] && [ "$(git rev-parse HEAD~1 2>/dev/null)" = "$EVENT_SHA" ]; then
echo "docs_sha=$head" >> "$GITHUB_OUTPUT"
fi