Skip to content

Commit

Permalink
Merge pull request #121 from ZeroGachis/feature/pla-1581
Browse files Browse the repository at this point in the history
feat(infracost): init workflow
  • Loading branch information
nicolasbriere1 authored Aug 6, 2024
2 parents 38ceb7f + ded1355 commit dca9c01
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 9 deletions.
106 changes: 106 additions & 0 deletions .github/workflows/infra-cost.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: Infracost Pull Request Checks

on:
workflow_call:
inputs:
infracost_terraform_workspace:
description: 'Comma-delimited list of Terraform workspaces'
required: false
type: string
default: main
terraform_vars:
description: 'terraform variables to add to CLI'
required: false
type: string

jobs:
infracost:
name: Infracost Pull Request Checks
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

steps:
- name: Setup Infracost
uses: infracost/actions/setup@v3
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}

- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.base.ref }}"

- name: Generate Infracost config file
run: |
echo "version: 0.1" > infracost.yml
echo "projects:" >> infracost.yml
IFS=',' read -r -a workspaces <<< "${{ inputs.infracost_terraform_workspace }}"
for workspace in "${workspaces[@]}"; do
echo " - path: ." >> infracost.yml
echo " name: $workspace" >> infracost.yml
echo " terraform_workspace: $workspace" >> infracost.yml
if [ -n "${{ inputs.terraform_args }}" ]; then
if [[ "${{ inputs.terraform_args }}" == *-var=* ]]; then
terraform_var_key=$(echo "${{ inputs.terraform_args }}" | sed -n 's/.*-var \([^=]*\)=.*/\1/p')
terraform_var_value=$(echo "${{ inputs.terraform_args }}" | sed -n 's/.*-var [^=]*=\(.*\)/\1/p')
echo " terraform_vars:" >> infracost.yml
echo " $terraform_var_key: $terraform_var_value" >> infracost.yml
elif [[ "${{ inputs.terraform_args }}" == *-var-file=* ]]; then
terraform_var_file=$(echo "${{ inputs.terraform_args }}" | sed -n 's/.*-var-file=\(.*\)/\1/p')
echo " terraform_var_files:" >> infracost.yml
echo " - $terraform_var_file" >> infracost.yml
fi
fi
done
- name: Generate Infracost cost estimate baseline
run: |
infracost breakdown --config-file=infracost.yml \
--format=json \
--out-file=/tmp/infracost-base.json
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.head.ref }}"

- name: Generate Infracost config file
run: |
echo "version: 0.1" > infracost.yml
echo "projects:" >> infracost.yml
IFS=',' read -r -a workspaces <<< "${{ inputs.infracost_terraform_workspace }}"
for workspace in "${workspaces[@]}"; do
echo " - path: ." >> infracost.yml
echo " name: $workspace" >> infracost.yml
echo " terraform_workspace: $workspace" >> infracost.yml
if [ -n "${{ inputs.terraform_args }}" ]; then
if [[ "${{ inputs.terraform_args }}" == *-var=* ]]; then
terraform_var_key=$(echo "${{ inputs.terraform_args }}" | sed -n 's/.*-var \([^=]*\)=.*/\1/p')
terraform_var_value=$(echo "${{ inputs.terraform_args }}" | sed -n 's/.*-var [^=]*=\(.*\)/\1/p')
echo " terraform_vars:" >> infracost.yml
echo " $terraform_var_key: $terraform_var_value" >> infracost.yml
elif [[ "${{ inputs.terraform_args }}" == *-var-file=* ]]; then
terraform_var_file=$(echo "${{ inputs.terraform_args }}" | sed -n 's/.*-var-file=\(.*\)/\1/p')
echo " terraform_var_files:" >> infracost.yml
echo " - $terraform_var_file" >> infracost.yml
fi
fi
done
- name: Generate Infracost diff
run: |
infracost diff --config-file=infracost.yml \
--format=json \
--compare-to=/tmp/infracost-base.json \
--out-file=/tmp/infracost.json
- name: Post Infracost comment
run: |
infracost comment github --path=/tmp/infracost.json \
--repo=$GITHUB_REPOSITORY \
--github-token=${{ secrets.GITHUB_TOKEN }} \
--pull-request=${{ github.event.pull_request.number }} \
--show-all-projects \
--behavior=update
65 changes: 56 additions & 9 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ on:
required: false
type: string
aws_account_id:
required: false
type: string
required: false
type: string
aws_region:
required: false
type: string
Expand All @@ -52,22 +52,29 @@ on:
required: false
type: string
aws_secrets:
required: false
type: string
required: false
type: string
aws_additional_secrets:
required: false
type: string
tailscale_enabled:
required: false
type: boolean
default: true

infracost_enabled:
required: false
type: boolean
default: true
infracost_terraform_workspace:
type: string
default: "develop,main"
jobs:
terraform:
name: "Terraform"
permissions:
id-token: write
contents: read
pull-requests: read
runs-on: ubuntu-latest
environment:
name: ${{ inputs.environment_name }}
Expand Down Expand Up @@ -108,11 +115,8 @@ jobs:
secrets: |
github/token/read_repositories token | GITHUB_READ_REPO_TOKEN;
${{ inputs.vault_secrets }}
- uses: hashicorp/setup-terraform@v3
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -145,13 +149,22 @@ jobs:
- name: Terraform Init
id: init
working-directory: ${{ inputs.workdir || github.workspace }}
run: terraform init -backend-config="bucket=${{ inputs.terraform_backend || env.TERRAFORM_DEFAULT_BACKEND }}"
run: terraform init -backend-config="bucket=${{ inputs.terraform_backend || env.TERRAFORM_DEFAULT_BACKEND }}"

- name: Terraform Validate
id: validate
working-directory: ${{ inputs.workdir || github.workspace }}
run: terraform validate

- name: Check if .tf files changed
if: inputs.infracost_enabled
id: files_changed
uses: dorny/paths-filter@v3
with:
filters: |
terraform:
- '**.tf'
- name: Terraform Workspace
id: workspace
working-directory: ${{ inputs.workdir || github.workspace }}
Expand All @@ -166,3 +179,37 @@ jobs:
if: ${{ inputs.terraform_check_only == false }}
working-directory: ${{ inputs.workdir || github.workspace }}
run: terraform apply -auto-approve -input=false -parallelism=${{ inputs.terraform_parallelism }} tfplan

infracost-prerequisites:
name: "Infracost Prerequisites"
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' && inputs.infracost_enabled }}
outputs:
files_changed: ${{ steps.files_changed.outputs.terraform }}
terraform_vars: ${{ steps.filter_args.outputs.filtered_args}}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Check if .tf files changed
id: files_changed
uses: dorny/paths-filter@v3
with:
filters: |
terraform:
- '**.tf'
- name: Filter Terraform Args
if: ${{ inputs.terraform_args }}
id: filter_args
run: |
filtered_args=$(echo '${{ inputs.terraform_args }}' | sed -e 's/.*"\(.*\)".*/\1/')
echo "filtered_args=$filtered_args" >> "$GITHUB_OUTPUT"
infracost:
name: "Run Infracost Workflow"
needs: [infracost-prerequisites]
if: ${{ github.event_name == 'pull_request' && needs.infracost-prerequisites.outputs.files_changed == 'true' && inputs.infracost_enabled }}
uses: ZeroGachis/.github/.github/workflows/infra-cost.yml@v4
with:
infracost_terraform_workspace: ${{ inputs.infracost_terraform_workspace }}
terraform_vars: ${{ needs.infracost-prerequisites.outputs.terraform_vars }}
secrets: inherit

0 comments on commit dca9c01

Please sign in to comment.