name: Infracost CI

on:
  pull_request:
    paths:
      - '**/*.tf'

jobs:
  infracost:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Terraform
        uses: hashicorp/setup-terraform@v1

      - name: Initialize Terraform
        run: terraform init

      - name: Generate Terraform plan
        run: terraform plan -out=tfplan.binary
        continue-on-error: true

      - name: Install Infracost
        run: |
          curl -s https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh
          infracost --version

      - name: Generate Infracost breakdown
        run: infracost breakdown --path tfplan.binary --format json --out-file infracost.json

      - name: Post Infracost comment on PR
        uses: infracost/actions/comment@v1
        with:
          path: infracost.json
          behavior: update