All Projects → infracost → actions

infracost / actions

Licence: Apache-2.0 license
A set of GitHub actions for Infracost. See cloud cost estimates for Terraform in pull requests. 💰📉 Love your cloud bill!

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to actions

action.playbook
Github Action for running Ansible Playbooks.
Stars: ✭ 26 (-82.31%)
Mutual labels:  actions, github-actions
chrome-addon
☁ GitHub action to upload addon to Chrome
Stars: ✭ 53 (-63.95%)
Mutual labels:  actions, github-actions
actions-suggest-related-links
A GitHub Action to suggest related or similar issues, documents, and links. Based on the power of NLP and fastText.
Stars: ✭ 23 (-84.35%)
Mutual labels:  actions, github-actions
Actions Gh Pages
GitHub Actions for GitHub Pages 🚀 Deploy static files and publish your site easily. Static-Site-Generators-friendly.
Stars: ✭ 2,576 (+1652.38%)
Mutual labels:  actions, github-actions
netlify-build-github-actions
An example of triggering a Netlify build using Github Actions Scheduled Events
Stars: ✭ 31 (-78.91%)
Mutual labels:  actions, github-actions
Awesome Actions
A curated list of awesome actions to use on GitHub
Stars: ✭ 16,943 (+11425.85%)
Mutual labels:  actions, github-actions
action-homebrew-bump-formula
⚙️ A GitHub Action to easily bump Homebrew formula on new release
Stars: ✭ 68 (-53.74%)
Mutual labels:  actions, github-actions
github-run-tests-action
mabl Github Actions implementation
Stars: ✭ 39 (-73.47%)
Mutual labels:  actions, github-actions
ghaction-virustotal
GitHub Action to upload and scan files with VirusTotal
Stars: ✭ 105 (-28.57%)
Mutual labels:  actions, github-actions
recent-activity
Add your recent activity to your profile readme!
Stars: ✭ 87 (-40.82%)
Mutual labels:  actions, github-actions
Actions Openwrt
A template for building OpenWrt with GitHub Actions | 使用 GitHub Actions 云编译 OpenWrt
Stars: ✭ 4,742 (+3125.85%)
Mutual labels:  actions, github-actions
python-actions-alpha-archived
Please note that this was for the *alpha* version of GitHub Actions for Python.
Stars: ✭ 15 (-89.8%)
Mutual labels:  actions, github-actions
gh-pages-action
A GitHub Action to deploy a static site on GitHub Pages.
Stars: ✭ 26 (-82.31%)
Mutual labels:  actions, github-actions
juejin-actions
掘金每天自动签到 github actions 。组织了每周一起学习200行左右的【源码共读】活动,感兴趣可以加我微信 ruochuan12 参与。
Stars: ✭ 47 (-68.03%)
Mutual labels:  actions, github-actions
upx-action
Strips and runs upx on binaries
Stars: ✭ 17 (-88.44%)
Mutual labels:  actions, github-actions
actions
Our Library of GitHub Actions
Stars: ✭ 49 (-66.67%)
Mutual labels:  actions, github-actions
npm-update-check-action
npm new package version check action for GitHub Actions.
Stars: ✭ 17 (-88.44%)
Mutual labels:  actions, github-actions
action
📦📊 GitHub Action to reports on the size of your npm package
Stars: ✭ 36 (-75.51%)
Mutual labels:  actions, github-actions
clang-format-action
GitHub Action for clang-format checking
Stars: ✭ 48 (-67.35%)
Mutual labels:  actions, github-actions
actions-deploy-gist
📌 Deploy file to Github Gist
Stars: ✭ 26 (-82.31%)
Mutual labels:  actions, github-actions

Infracost GitHub Actions

This project provide a GitHub Action and examples for Infracost, so you can see cloud cost estimates for Terraform in pull requests 💰

Example screenshot

Quick start

The following steps assume a simple Terraform directory is being used, we recommend you use a more relevant example if required.

  1. If you haven't done so already, download Infracost and run infracost auth login to get a free API key.

  2. Retrieve your Infracost API key by running infracost configure get api_key.

  3. Create a repo secret called INFRACOST_API_KEY with your API key.

  4. Create a new file in .github/workflows/infracost.yml in your repo with the following content.

# The GitHub Actions docs (https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#on)
# describe other options for 'on', 'pull_request' is a good default.
on: [pull_request]
env:
  # If you use private modules you'll need this env variable to use
  # the same ssh-agent socket value across all jobs & steps.
  SSH_AUTH_SOCK: /tmp/ssh_agent.sock
jobs:
  infracost:
    name: Infracost
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write

    env:
      TF_ROOT: examples/terraform-project/code
      # If you're using Terraform Cloud/Enterprise and have variables or private modules stored
      # on there, specify the following to automatically retrieve the variables:
      #   INFRACOST_TERRAFORM_CLOUD_TOKEN: ${{ secrets.TFC_TOKEN }}
      #   INFRACOST_TERRAFORM_CLOUD_HOST: app.terraform.io # Change this if you're using Terraform Enterprise

    steps:
      # If you use private modules, add an environment variable or secret
      # called GIT_SSH_KEY with your private key, so Infracost can access
      # private repositories (similar to how Terraform/Terragrunt does).
      # - name: add GIT_SSH_KEY
      #   run: |
      #     ssh-agent -a $SSH_AUTH_SOCK
      #     mkdir -p ~/.ssh
      #     echo "${{ secrets.GIT_SSH_KEY }}" | tr -d '\r' | ssh-add -
      #     ssh-keyscan github.com >> ~/.ssh/known_hosts

      - name: Setup Infracost
        uses: infracost/actions/setup@v2
        # See https://github.com/infracost/actions/tree/master/setup for other inputs
        # If you can't use this action, see Docker images in https://infracost.io/cicd
        with:
          api-key: ${{ secrets.INFRACOST_API_KEY }}

      # Checkout the base branch of the pull request (e.g. main/master).
      - name: Checkout base branch
        uses: actions/checkout@v3
        with:
          ref: '${{ github.event.pull_request.base.ref }}'

      # Generate Infracost JSON file as the baseline.
      - name: Generate Infracost cost estimate baseline
        run: |
          infracost breakdown --path=${TF_ROOT} \
                              --format=json \
                              --out-file=/tmp/infracost-base.json

      # Checkout the current PR branch so we can create a diff.
      - name: Checkout PR branch
        uses: actions/checkout@v3

      # Generate an Infracost diff and save it to a JSON file.
      - name: Generate Infracost diff
        run: |
          infracost diff --path=${TF_ROOT} \
                          --format=json \
                          --compare-to=/tmp/infracost-base.json \
                          --out-file=/tmp/infracost.json

      # 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.
      # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests for other options.
      - 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
  1. 🎉 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!

    If there are issues, check the GitHub Actions logs and this page.

    Example pull request
  2. Enable Infracost Cloud and trigger your CI/CD pipeline again. This causes the CLI to send its JSON output to your dashboard; the JSON does not contain any cloud credentials or secrets, see the FAQ for more information. This is our SaaS product that builds on top of Infracost open source and enables team leads, managers and FinOps practitioners to see all cost estimates from a central place so they can help guide the team. To learn more, see our docs.

    Infracost Cloud gives team leads, managers and FinOps practitioners visibility across all cost estimates in CI/CD

Troubleshooting

Permissions issue

If you receive an error when running the infracost comment command in your pipeline, it's probably related to ${{ github.token }}. This is the default GitHub token available to actions and is used to post comments. The default token permissions work fine but pull-requests: write is required if you need to customize these. If you are using SAML single sign-on, you must first authorize the token.

The add GIT_SSH_KEY step fails

If you are using private modules and receive a option requires an argument -- a error in the add GIT_SSH_KEY step:

  1. Make sure you have the following set in your workflow SSH_AUTH_SOCK:
    env:
      SSH_AUTH_SOCK: /tmp/ssh_agent.sock
  2. Try changing the ssh-agent -a $SSH_AUTH_SOCK line to the following:
    ssh-agent -a "${{ env.SSH_AUTH_SOCK }}"

Examples

The examples directory demonstrates how these actions can be used for different projects. They all work by using the default Infracost CLI option that parses HCL, thus a Terraform plan JSON is not needed.

For advanced use cases where the estimate needs to be generated from Terraform plan JSON files, see the plan JSON examples here.

Cost policies

Infracost policies enable centralized teams, who are often helping others with cloud costs, to provide advice before resources are launched, setup guardrails, and prevent human error. Follow our docs to use Infracost's native support for Open Policy Agent (OPA) policies. This enables you to see passing/failing policies in Infracost pull request comments (shown below) without having to install anything else.

If you use HashiCorp Sentinel, follow our example to output the policy pass/fail results into CI/CD logs.

Contributing

Issues and pull requests are welcome! For development details, see the contributing guide. For major changes, including interface changes, please open an issue first to discuss what you would like to change. Join our community Slack channel, we are a friendly bunch and happy to help you get started :)

License

Apache License 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].