Skip to content

Refactor Terraform workflow to use GitHub Actions setup and cache #160

Refactor Terraform workflow to use GitHub Actions setup and cache

Refactor Terraform workflow to use GitHub Actions setup and cache #160

Workflow file for this run

# This is a basic workflow to help you get started with Actions
name: Terraform Plan
# Controls when the workflow will run
on:
pull_request:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
permissions: write-all
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
Plan:
# The type of runner that the job will run on
runs-on: [ "229685449397" ]
env:
GITHUB_APP_INSTALLATION_ID: ${{ vars.GH_APP_INSTALLATION_ID }}
GITHUB_APP_PEM_FILE: ${{ secrets.GH_APP_PEM_FILE }}
GITHUB_OWNER: CSVD
GITHUB_BASE_URL: https://github.e.it.census.gov/
TF_WORKSPACE: ${{ vars.terraform_workspace }}
TF_CLI_ARGS_plan: -lock-timeout=30m
TF_CLI_ARGS_apply: -lock-timeout=30m
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Setup AWS Credentials
id: aws_credentials
run: |
curl -qL -o aws_credentials.json http://169.254.170.2/${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI} > aws_credentials.json
aws configure set aws_access_key_id `jq -r '.AccessKeyId' aws_credentials.json`
echo AWS_ACCESS_KEY_ID=`jq -r '.AccessKeyId' aws_credentials.json` >> $GITHUB_ENV
aws configure set aws_secret_access_key `jq -r '.SecretAccessKey' aws_credentials.json`
echo AWS_SECRET_ACCESS_KEY=`jq -r '.SecretAccessKey' aws_credentials.json` >> $GITHUB_ENV
aws configure set aws_session_token `jq -r '.Token' aws_credentials.json`
echo AWS_SESSION_TOKEN=`jq -r '.Token' aws_credentials.json` >> $GITHUB_ENV
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Setup GITHUB Credentials
id: github_credentials
run: |
echo GITHUB_TOKEN=$(python encode_jwt.py "$GITHUB_APP_PEM_FILE" "$GITHUB_APP_INSTALLATION_ID" "$GITHUB_BASE_URL") >> $GITHUB_ENV
echo "PyGenerated GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}"
- name: Terraform Init
uses: CSVD/terraform-init@main
id: terraform_init
with:
terraform_version: "1.9.1"
workspace: ${{ vars.terraform_workspace }}
setup_terraform: true
terraform_init: true
env:
GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}
- name: Terraform Plan
uses: CSVD/terraform-plan@main
with:
terraform_version: "1.9.1"
workspace: ${{ vars.terraform_workspace }}
commit_sha: ${{ steps.terraform_init.outputs.commit_sha }}
varfile: varfiles/${{ vars.terraform_workspace }}.tfvars
download_cache: true
setup_terraform: false