Skip to content

Commit

Permalink
infracost
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Morgan committed Oct 18, 2024
1 parent 3f96233 commit c271d01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/infracost.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: 'Run Infracost'

on:
pull_request:
types: [opened, synchronize, closed]

jobs:
infracost-pull-request-checks:
name: Infracost Pull Request Checks
Expand All @@ -13,44 +11,39 @@ jobs:
permissions:
contents: read
pull-requests: write # Required to post comments

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

# Checkout the base branch of the pull request (e.g. main/master).
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: '${{ github.event.pull_request.base.ref }}'

# Generate Infracost JSON file as the baseline.
- name: Generate Infracost cost estimate baseline
run: |
infracost breakdown --path=. \
--format=json \
--out-file=/tmp/infracost-base.json
# Checkout the current PR branch so we can create a diff.
- name: Checkout PR branch
uses: actions/checkout@v4

# Generate an Infracost diff and save it to a JSON file.
- name: Generate Infracost diff
run: |
infracost diff --path=. \
--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=${{ github.token }} \
--pull-request=${{ github.event.pull_request.number }} \
--behavior=update
--behavior=update \
--policy-path ./policies/cost.rego
24 changes: 24 additions & 0 deletions policies/cost.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package infracost # You must specify infracost as the Rego package name

# Each file can have a number of "deny" rules that must return an "out" object
# with keys "msg" & "failed". You can write as many "deny[out]" rule sets as you wish.
# You can read more about rule definitions in Rego here: https://www.openpolicyagent.org/docs/latest/policy-language/#rules
deny[out] {
# maxDiff defines the threshold that you require the cost estimate to be below
maxDiff = 5.0

# msg defines the output that will be shown in PR comments under the Policy Checks/Failures section
msg := sprintf(
"Total monthly cost diff must be less than $%.2f (actual diff is $%.2f)",
[maxDiff, to_number(input.diffTotalMonthlyCost)],
)

# out defines the output for this policy. This output must be formatted with a `msg` and `failed` property.
out := {
# the msg you want to display in your PR comment, must be a string
"msg": msg,
# a boolean value that determines if this policy has failed.
# In this case if the Infracost breakdown output diffTotalMonthlyCost is greater that $5000
"failed": to_number(input.diffTotalMonthlyCost) >= maxDiff
}
}

0 comments on commit c271d01

Please sign in to comment.