Skip to content

Instantly share code, notes, and snippets.

@corymacd
Created July 2, 2024 18:24
Show Gist options
  • Save corymacd/27f6e39ce62784e6875d268b0b7eba80 to your computer and use it in GitHub Desktop.
Save corymacd/27f6e39ce62784e6875d268b0b7eba80 to your computer and use it in GitHub Desktop.
het-terraform-actions-workflow
name: 'Hetzner Terraform Plan/Apply'
on:
push:
branches:
- main
paths:
- 'infra-hetzner/**'
#Special permissions required for OIDC authentication
permissions:
id-token: write
contents: read
pull-requests: write
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
#These environment variables are used by the terraform azure provider to setup OIDD authenticate.
env:
ARM_CLIENT_ID: "${{ vars.ARM_CLIENT_ID }}"
ARM_SUBSCRIPTION_ID: "${{ vars.ARM_SUBSCRIPTION_ID }}"
ARM_TENANT_ID: "${{ vars.ARM_TENANT_ID }}"
HCLOUD_TOKEN: "${{ secrets.HCLOUD_TOKEN }}"
CLOUDFLARE_TOKEN: "${{ secrets.CLOUDFLARE_TOKEN }}"
jobs:
terraform-plan:
name: 'Terraform Plan'
runs-on: ubuntu-latest
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action == 'opened')
# environment: prod-hetzner
outputs:
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }}
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v4
# Install the latest version of the Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
- name: 'Az CLI login'
uses: azure/login@v1
with:
client-id: ${{ vars.ARM_CLIENT_ID }}
tenant-id: ${{ vars.ARM_TENANT_ID }}
subscription-id: ${{ vars.ARM_SUBSCRIPTION_ID }}
# creds: ${{ secrets.AZURE_CREDENTIALS }}/
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: terraform init
working-directory: infra-hetzner
# Generates an execution plan for Terraform
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes.
- name: Terraform Plan
id: tf-plan
working-directory: infra-hetzner
run: |
export exitcode=0
terraform plan -detailed-exitcode -var hcloud_token=${{ secrets.HCLOUD_TOKEN }} -var cloudflare_token=${{ secrets.CLOUDFLARE_TOKEN }} -var tailscale_api_token=${{secrets.TAILSCALE_API_TOKEN}} -no-color -out tfplan || export exitcode=$?
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
if [ $exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
else
exit 0
fi
# Checks that all Terraform configuration files adhere to a canonical format
# Will fail the build if not
- name: Terraform Format
run: terraform fmt -check
working-directory: infra-hetzner
- name: Checkov GitHub Action
uses: bridgecrewio/checkov-action@v12
with:
# This will add both a CLI output to the console and create a results.sarif file
output_format: cli,sarif
output_file_path: console
file: infra-hetzner/tfplan
soft_fail: false
# Save plan to artifacts
- name: Publish Terraform Plan
uses: actions/upload-artifact@v4
with:
name: tfplan
path: infra-hetzner/tfplan
# Create string output of Terraform Plan
- name: Create String Output
id: tf-plan-string
working-directory: infra-hetzner
run: |
TERRAFORM_PLAN=$(terraform show -no-color tfplan)
delimiter="$(openssl rand -hex 8)"
echo "summary<<${delimiter}" >> $GITHUB_OUTPUT
echo "## Terraform Plan Output" >> $GITHUB_OUTPUT
echo "<details><summary>Click to expand</summary>" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo '```terraform' >> $GITHUB_OUTPUT
echo "$TERRAFORM_PLAN" >> $GITHUB_OUTPUT
echo '```' >> $GITHUB_OUTPUT
echo "</details>" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
# Publish Terraform Plan as task summary
- name: Publish Terraform Plan to Task Summary
working-directory: infra-hetzner
env:
SUMMARY: ${{ steps.tf-plan-string.outputs.summary }}
run: |
echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY
# If this is a PR post the changes
- name: Push Terraform Output to PR
if: github.ref != 'refs/heads/main'
uses: actions/github-script@v7
env:
SUMMARY: "${{ steps.tf-plan-string.outputs.summary }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const body = `${process.env.SUMMARY}`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
})
terraform-apply:
name: 'Terraform Apply'
if: github.ref == 'refs/heads/main' && needs.terraform-plan.outputs.tfplanExitCode == 2
runs-on: ubuntu-latest
environment: prod-hetzner
needs: [terraform-plan]
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v4
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: 'Az CLI login'
uses: azure/login@v1
with:
client-id: ${{ vars.ARM_CLIENT_ID }}
tenant-id: ${{ vars.ARM_TENANT_ID }}
subscription-id: ${{ vars.ARM_SUBSCRIPTION_ID }}
# creds: ${{ secrets.AZURE_CREDENTIALS }}
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: terraform init
working-directory: infra-hetzner
# Download saved plan from artifacts
- name: Download Terraform Plan
uses: actions/download-artifact@v4
with:
name: tfplan
path: infra-hetzner
# Terraform Apply
- name: Terraform Apply
working-directory: infra-hetzner
run: terraform apply -auto-approve tfplan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment