Skip to content

Commit

Permalink
fix: comment-action uses infracost-comment
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorut committed Mar 11, 2022
1 parent c385b44 commit facfccd
Show file tree
Hide file tree
Showing 23 changed files with 428 additions and 219 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/comment_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@ defaults:
shell: bash

jobs:
test_pr:
name: Test PR Comment
test_commit:
name: Test Commit Comment
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup infracost
uses: ./setup
with:
api-key: abcdefg123456
- name: Infracost comment
uses: ./comment
with:
path: ./testdata/comment/simple_breakdown.json
test_commit:
name: Test Commit Comment
path: "./testdata/comment/*.json"
target-type: commit
test_pr:
name: Test PR Comment
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup infracost
uses: ./setup
with:
api-key: abcdefg123456
- name: Infracost comment
uses: ./comment
with:
path: '["./testdata/comment/simple_breakdown.json", "./testdata/comment/simple_breakdown.json"]'
target-type: commit
path: ./testdata/comment/simple_breakdown.json
195 changes: 113 additions & 82 deletions .github/workflows/examples_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ jobs:
DEV_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_DEV_AWS_SECRET_ACCESS_KEY }}
PROD_AWS_ACCESS_KEY_ID: ${{ secrets.EXAMPLE_PROD_AWS_ACCESS_KEY_ID }}
PROD_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_PROD_AWS_SECRET_ACCESS_KEY }}
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: >-
diff ./testdata/multi-project-config-file_comment_golden.md
/tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- 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 \
--dry-run true \
> infracost-comment.md
- run: >-
diff -y ./testdata/multi-project-config-file_comment_golden.md
infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: >-
cp /tmp/infracost_comment.md
Expand Down Expand Up @@ -95,20 +99,19 @@ jobs:
uses: ./setup
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- name: Combine the results
run: >
infracost output --path="/tmp/infracost_jsons/*.json" --format=json
--out-file=/tmp/infracost_combined.json
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost_combined.json
--format=github-comment --show-skipped
--out-file=/tmp/infracost_comment.md
- name: Check the comment
run: >-
diff ./testdata/multi-project-matrix-merge_comment_golden.md
/tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- name: Post Infracost comment
run: |-
infracost comment github --path "/tmp/infracost_jsons/*.json" \
--repo $GITHUB_REPOSITORY \
--github-token ${{github.token}} \
--pull-request ${{github.event.pull_request.number}} \
--behavior update \
--dry-run true \
> infracost-comment.md
- run: >-
diff -y ./testdata/multi-project-matrix-merge_comment_golden.md
infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: >-
cp /tmp/infracost_comment.md
Expand Down Expand Up @@ -138,16 +141,20 @@ jobs:
DEV_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_DEV_AWS_SECRET_ACCESS_KEY }}
PROD_AWS_ACCESS_KEY_ID: ${{ secrets.EXAMPLE_PROD_AWS_ACCESS_KEY_ID }}
PROD_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_PROD_AWS_SECRET_ACCESS_KEY }}
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: >-
diff
- 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 \
--dry-run true \
> infracost-comment.md
- run: >-
diff -y
./testdata/multi-terraform-workspace-config-file_comment_golden.md
/tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: >-
cp /tmp/infracost_comment.md
Expand Down Expand Up @@ -180,15 +187,19 @@ jobs:
run: >-
infracost breakdown --path=examples/private-terraform-module/code
--format=json --out-file=/tmp/infracost.json
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: >-
diff ./testdata/private-terraform-module_comment_golden.md
/tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- 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 \
--dry-run true \
> infracost-comment.md
- run: >-
diff -y ./testdata/private-terraform-module_comment_golden.md
infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: >-
cp /tmp/infracost_comment.md
Expand Down Expand Up @@ -243,13 +254,17 @@ jobs:
run: >-
infracost breakdown --path=examples/slack/code/plan.json --format json
--out-file /tmp/infracost.json
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: diff ./testdata/slack_comment_golden.md /tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- 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 \
--dry-run true \
> infracost-comment.md
- run: diff -y ./testdata/slack_comment_golden.md infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: cp /tmp/infracost_comment.md ./testdata/slack_comment_golden.md
if: env.UPDATE_GOLDEN_FILES == 'true'
Expand Down Expand Up @@ -295,15 +310,19 @@ jobs:
--format=json --out-file=/tmp/infracost.json
env:
INFRACOST_TERRAFORM_CLOUD_TOKEN: ${{ secrets.TFC_TOKEN }}
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: >-
diff ./testdata/terraform-cloud-enterprise_comment_golden.md
/tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- 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 \
--dry-run true \
> infracost-comment.md
- run: >-
diff -y ./testdata/terraform-cloud-enterprise_comment_golden.md
infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: >-
cp /tmp/infracost_comment.md
Expand All @@ -326,15 +345,19 @@ jobs:
run: >-
infracost breakdown --path=examples/terraform-directory/code
--format=json --out-file=/tmp/infracost.json
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: >-
diff ./testdata/terraform-directory_comment_golden.md
/tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- 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 \
--dry-run true \
> infracost-comment.md
- run: >-
diff -y ./testdata/terraform-directory_comment_golden.md
infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: >-
cp /tmp/infracost_comment.md
Expand All @@ -353,15 +376,19 @@ jobs:
run: >-
infracost breakdown --path=examples/terraform-plan-json/code/plan.json
--format=json --out-file=/tmp/infracost.json
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: >-
diff ./testdata/terraform-plan-json_comment_golden.md
/tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- 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 \
--dry-run true \
> infracost-comment.md
- run: >-
diff -y ./testdata/terraform-plan-json_comment_golden.md
infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: >-
cp /tmp/infracost_comment.md
Expand All @@ -388,13 +415,17 @@ jobs:
run: >-
infracost breakdown --path=examples/terragrunt/code --format=json
--out-file=/tmp/infracost.json
- name: Generate Infracost comment
run: >-
infracost output --path=/tmp/infracost.json --format=github-comment
--show-skipped --out-file=/tmp/infracost_comment.md
- name: Check the comment
run: diff ./testdata/terragrunt_comment_golden.md /tmp/infracost_comment.md
if: env.UPDATE_GOLDEN_FILES != 'true'
- 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 \
--dry-run true \
> infracost-comment.md
- run: diff -y ./testdata/terragrunt_comment_golden.md infracost-comment.md
name: Check the comment
- name: Update the golden comment file
run: cp /tmp/infracost_comment.md ./testdata/terragrunt_comment_golden.md
if: env.UPDATE_GOLDEN_FILES == 'true'
32 changes: 19 additions & 13 deletions .github/workflows/get_comment_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,36 @@ jobs:
with:
api-key: abcdefg123456

- name: Save comment output
run: |
infracost comment github --path ./testdata/comment/simple_breakdown.json \
--repo $GITHUB_REPOSITORY \
--github-token ${{github.token}} \
--pull-request ${{github.event.pull_request.number}} \
--behavior new \
--dry-run true \
> expected.md
head -n -2 expected.md > tmp.txt && mv tmp.txt expected.md
- name: Infracost comment
id: post-comment
uses: ./comment
with:
path: ./testdata/comment/simple_breakdown.json
tag: "custom-tag"
behavior: new

- name: Infracost get comment
id: get-comment
uses: ./get-comment
with:
tag: "custom-tag"

- name: Check comment
shell: bash
run: |
expected=$(cat <<\EOF
${{ steps.post-comment.outputs.body }}
EOF
)
- run: |
actual=$(cat <<\EOF
${{ steps.get-comment.outputs.body }}
EOF
)
if [ "$actual" != "$expected" ]; then
echo "::error::Expected comment '$expected' got '$actual'"
exit 1
fi
diff -y <(echo "$actual") expected.md
name: Check the comment
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,23 @@ The following steps assume a simple Terraform directory is being used, we recomm
# env:
# MY_ENV: ${{ secrets.MY_ENV }}

# See https://github.com/infracost/actions/tree/master/comment
# See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests
# for other inputs such as target-type.
- name: Post Infracost comment
uses: infracost/actions/comment@v1
with:
path: /tmp/infracost.json
# Choose the commenting behavior, 'update' is a good default:
behavior: update # Create a single comment and update it. The "quietest" option.
# behavior: delete-and-new # Delete previous comments and create a new one.
# behavior: hide-and-new # Minimize previous comments and create a new one.
# behavior: new # Create a new cost estimate comment on every push.
run: |
# Posts a comment to the PR using the 'update' behavior.
# This creates a single comment and updates it. The "quietest" option.
# The other valid behaviors are:
# delete-and-new - Delete previous comments and create a new one.
# hide-and-new - Minimize previous comments and create a new one.
# new - Create a new cost estimate comment on every push.
infracost comment github --path /tmp/infracost.json \
--repo $GITHUB_REPOSITORY \
--github-token ${{github.token}} \
--pull-request ${{github.event.pull_request.number}} \
--behavior update
```
4. 🎉 That's it! Send a new pull request to change something in Terraform that costs money. You should see a pull request comment that gets updated, e.g. the 📉 and 📈 emojis will update as changes are pushed!
Expand Down Expand Up @@ -120,8 +126,10 @@ Infracost cost estimates can also be configured to work against policies written
We recommend you use the above [quick start](#quick-start) guide and examples, which combine the following individual actions:
- [setup](setup): downloads and installs the Infracost CLI in your GitHub Actions workflow.
- [comment](comment): adds comments to pull requests.
- [get-comment](get-comment): reads a comment from a pull request.
### Deprecated Actions
- [comment](comment): adds comments to pull requests. This action is deprecated, please use `infracost comment` directly.
- [get-comment](get-comment): reads a comment from a pull request. This action is deprecated.

## Contributing

Expand Down
Loading

0 comments on commit facfccd

Please sign in to comment.