|
| 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 | +... |
0 commit comments