Skip to content

Commit

Permalink
[CI] Inital Infracost setup (#3)
Browse files Browse the repository at this point in the history
* Inital Infracost

* Add out-file flag

* Don't comment on PR if no infra diff
  • Loading branch information
MNThomson committed Apr 24, 2022
1 parent d52fc75 commit 093b8d7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
50 changes: 50 additions & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,53 @@ jobs:
if: github.event_name == 'pull_request'
run: terraform plan -no-color
continue-on-error: true

infracost:
runs-on: ubuntu-latest
name: Run Infracost
needs: terraform
if: github.event_name == 'pull_request'
defaults:
run:
working-directory: ./src/infra
steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Setup CI
run: .github/workflows/resources/setupci.sh
working-directory: .

- name: Install terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_wrapper: false # This is recommended so the `terraform show` command outputs valid JSON

- name: Terraform init
run: terraform init

- name: Terraform plan
run: terraform plan -out tfplan.binary

- name: Terraform show
run: terraform show -json tfplan.binary > plan.json

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

- name: Generate Infracost JSON
run: infracost breakdown --path=plan.json --format json --out-file /tmp/infracost.json

- name: Exit on no Infra diff
run: echo "DIFF=`jq -r '.projects[0].diff.resources | length' /tmp/infracost.json`" >> $GITHUB_ENV

- name: Post Infracost comment
if: ${{ env.DIFF != '0' }}
run: |
infracost comment github --path /tmp/infracost.json \
--repo $GITHUB_REPOSITORY \
--github-token ${{github.token}} \
--pull-request ${{github.event.pull_request.number}} \
--behavior update
1 change: 0 additions & 1 deletion src/infra/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ variable "instance_profile_name" {

variable "my_public_ip_cidr" {
type = string
default = "23.16.122.44/32"
description = "My public ip CIDR"
}

Expand Down

0 comments on commit 093b8d7

Please sign in to comment.