Skip to content

Commit 3199546

Browse files
authored
feat: Added infracost analysis workflow (#72)
1 parent 7c732b2 commit 3199546

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

.github/workflows/infracost.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
name: Infracost Analysis
3+
on:
4+
workflow_call:
5+
inputs:
6+
working-directory:
7+
required: true
8+
description: Working directory of terraform resources
9+
type: string
10+
default: ./
11+
slack_notification:
12+
description: Slack notification required or not
13+
type: string
14+
default: false
15+
secrets:
16+
INFRACOST_API_KEY:
17+
required: true
18+
description: Provide Key from infracost api
19+
GITHUB:
20+
required: true
21+
description: GitHub token.
22+
SLACK_WEBHOOK:
23+
required: false
24+
description: Slack webhook url
25+
jobs:
26+
infracost:
27+
name: Infracost Analysis
28+
runs-on: ubuntu-latest
29+
steps:
30+
- name: Setup Infracost
31+
uses: infracost/actions/setup@6bdd3cb01a306596e8a614e62af7a9c0a133bc5c
32+
with:
33+
api-key: ${{ secrets.INFRACOST_API_KEY }}
34+
35+
- name: Checkout base branch
36+
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
37+
with:
38+
ref: ${{ github.event.pull_request.base.ref }}
39+
40+
- name: Print debug info
41+
run: |
42+
echo "github base branch is ${{ github.event.pull_request.base.ref }}"
43+
echo "github.event.pull_request.number is ${{ github.event.pull_request.number }}"
44+
45+
- name: Generate Infracost cost estimate baseline
46+
run: |
47+
export INFRACOST_API_KEY=${{ secrets.INFRACOST_API_KEY }}
48+
cd ${{ inputs.working-directory }}
49+
infracost breakdown --path . \
50+
--format=json \
51+
--out-file=/tmp/infracost-base.json
52+
53+
- name: Checkout PR branch
54+
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
55+
56+
- name: Generate Infracost diff
57+
run: |
58+
export INFRACOST_API_KEY=${{ secrets.INFRACOST_API_KEY }}
59+
cd ${{ inputs.working-directory }}
60+
infracost diff --path=. \
61+
--format=json \
62+
--show-skipped \
63+
--compare-to=/tmp/infracost-base.json \
64+
--out-file=/tmp/infracost.json
65+
66+
- name: Generate Infracost Report
67+
run: |
68+
export INFRACOST_API_KEY=${{ secrets.INFRACOST_API_KEY }}
69+
cd ${{ inputs.working-directory }}
70+
infracost output --path /tmp/infracost.json --show-skipped --format html --out-file report.html
71+
72+
- name: Upload current cost in artifactory
73+
uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
74+
with:
75+
name: report.html
76+
path: ${{ inputs.working-directory }}/report.html
77+
78+
- name: Post Infracost comment
79+
run: |
80+
export INFRACOST_API_KEY=${{ secrets.INFRACOST_API_KEY }}
81+
infracost comment github --path=/tmp/infracost.json \
82+
--repo=$GITHUB_REPOSITORY \
83+
--github-token=${{ secrets.GITHUB }} \
84+
--pull-request=${{ github.event.pull_request.number }} \
85+
--behavior=update
86+
87+
- name: Generate Slack message
88+
id: infracost-slack
89+
run: |
90+
echo "::set-output name=slack-message::$(infracost output --path=/tmp/infracost.json --format=slack-message --show-skipped)"
91+
echo "::set-output name=diffTotalMonthlyCost::$(jq '(.diffTotalMonthlyCost // 0) | tonumber' /tmp/infracost.json)"
92+
93+
- name: Send cost estimate to Slack
94+
uses: slackapi/slack-github-action@v1
95+
if: ${{ inputs.slack_notification == 'true' }}
96+
with:
97+
payload: ${{ steps.infracost-slack.outputs.slack-message }}
98+
env:
99+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK}}
100+
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
101+
...

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Above example is just a simple example to call workflow from github shared workf
6262
7. [Terraform Checks Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md)
6363
7. [Checkov Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/checkov.md)
6464
8. [Terraform Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform_workflow.md)
65+
9. [Infracost workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/infracost.md)
6566
6667
## Feedback
6768
If you come accross a bug or have any feedback, please log it in our [issue tracker](https://github.com/clouddrove/github-shared-workflows/issues), or feel free to drop us an email at [[email protected]](mailto:[email protected]).

docs/infracost.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## [Infra-Cost Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/.github/workflows/infracost.yml)
2+
Infracost shows cloud cost estimates for Terraform. It lets engineers see a cost breakdown and understand costs before making changes, either in the terminal,VS Code or pull requests.
3+
It utilizes the workflows defined in `.github/workflows/infracost.yml`
4+
5+
#### Usage
6+
- With Terraform, you can easily estimate cloud costs by leveraging Infracost, and you can easily compare potential bills between different vendors. Infracost, as they state on their website, makes you love your cloud bill
7+
- When you create PR we show the difference of cost in a PR comment, and the actual cost report generated in artifactory
8+
9+
#### Get started
10+
Infracost also has many CI/CD integrations so you can easily post cost estimates in pull requests. This provides your team with a safety net as people can discuss costs as part of the workflow.
11+
12+
First you need to generate **INFRACOST-API-KEY** using following commands on your CLI.
13+
```
14+
brew install infracost
15+
infracost --version
16+
infracost auth login # signup here
17+
infracost configure get api_key # here we got API-KEY copy this & add in github
18+
19+
```
20+
**Infra-cost Api generation Reference Link:** https://www.infracost.io/docs/
21+
22+
#### Example
23+
24+
```yaml
25+
name: "Infracost analysis"
26+
permissions: write-all
27+
28+
on:
29+
pull_request:
30+
branches: [ master ]
31+
jobs:
32+
infracost:
33+
uses: clouddrove/github-shared-workflows/.github/workflows/infracost.yml@master
34+
with:
35+
working-directory: # Need to specify working-directory as that's where the terraform files live in the source code
36+
slack_notification: # If we need slack notification then its value is true else false
37+
secrets:
38+
INFRACOST_API_KEY: # Provide Infra-Cost-API-key
39+
GITHUB: # Add github token here
40+
SLACK_WEBHOOK: # If slack_notification is true then add Webhook URL
41+
```
42+
43+
## When you do some changes in resources, after that create Pull Request in this we shows difference between previous cost & new cost
44+
Show below picture for more understanding
45+
46+
![image](https://github.com/clouddrove-sandbox/Infra-cost/assets/116706588/4de7eb51-57a2-4165-8332-fea4a9c5311d)
47+

0 commit comments

Comments
 (0)