-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from ZeroGachis/feature/pla-1581
feat(infracost): init workflow
- Loading branch information
Showing
2 changed files
with
162 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters