Skip to Content
IntegrationsCI/CD Platforms

Scan in GitHub Actions

Use the Mondoo GitHub Actions to scan infrastructure code, containers, cloud accounts, and GitHub organizations for security issues in CI/CD workflows.

The Mondoo GitHub Actions scan your infrastructure code, container images, Kubernetes clusters, AWS accounts, and GitHub organizations and repositories for security issues during your CI/CD workflow. Catching misconfigurations before deployment makes them easier to fix and prevents them from reaching your production environment. The scan actions run cnspec in a container, so you don't need to install it. You specify what to scan and provide your Mondoo credentials. Scan results appear in both the GitHub Actions UI and the Mondoo App's CI/CD view. To learn more, read View CI/CD Scan Results in Mondoo. For the full list, see Available actions.

You can also use Mondoo to continuously assess the security of your GitHub repositories and organization. To learn more, read Secure GitHub with Mondoo.

Configure GitHub Actions security

To set up a GitHub Actions integration with Mondoo:

  • Create Mondoo credentials

  • Store those credentials in GitHub

Create credentials in Mondoo

Configure a Mondoo service account to fetch policies and send scan results to Mondoo Platform:

  1. In the Mondoo App, navigate to the space where you want to add the integration. In the side navigation bar, select Integrations. In the top right, select INSTALL.

  2. On the Install Integration page, under CI/CD, select GitHub Actions (or search by name).

  3. Copy the value in the Copy the Mondoo Platform credentials box to use it as a variable in your pipeline.

The credential is a base64-encoded value that contains all the information needed to send the results of the scan to Mondoo. You can decode and check the content using this command:

echo <Credentials> | base64 -d

Securely store credentials in GitHub

  1. Go to your GitHub repo and, in the top-right corner, select Settings.

  2. In the left navigation panel, under Security, select Secrets and variables and then select Actions.

  3. Select the New repository secret button.

  4. Name the secret MONDOO_CONFIG_BASE64 and, in the Secret box, paste the credentials you copied in the steps above.

    Create a GitHub Actions secret

  5. Select the Add secret button.

Available actions

Reference each action in a workflow step as mondoohq/actions/<action>@main. Every action except xgrep is a Docker container action, so it runs only on Linux runners, such as ubuntu-latest. The with values each action requires differ:

ActionWhat it doesRequired with values
k8s-manifestScans a Kubernetes manifest filepath: the manifest file
terraform-hclScans Terraform HCL configurationpath: the Terraform working directory
terraform-planScans a Terraform plan in JSON, the output of terraform show -jsonNone. path (default terraform) is the directory that holds the plan, and plan-file (default plan.json) its name.
terraform-stateScans a Terraform state in JSON, the output of terraform show -json. A raw terraform.tfstate file can't be scanned.path: the JSON state file
docker-imageScans a container image, either built earlier in the workflow or in a registryimage: the image ID or name:tag
k8sScans the Kubernetes cluster that the runner's KUBECONFIG gives access toNone
awsScans the AWS account of the AWS credentials available to the runner, for example from aws-actions/configure-aws-credentialsNone
github-orgScans a GitHub organization and its repositories. Read Scan a GitHub organization or repository.organization: the organization name
github-repoScans one GitHub repository. Read Scan a GitHub organization or repository.repository: the repository, as owner/name
policyUploads a policy bundle to Mondoo Platformpath: the policy bundle file
cnspec-lintLints policy bundles and writes the results as a SARIF report. Upload the report with GitHub's upload-sarif action to see the results in code scanning. A lint error doesn't fail the job.path (default .) and output-file (default results.sarif). Both have defaults, so you can leave them out.
xgrepScans source code with xgrep and reports findings to GitHub code scanning. Read Show findings in GitHub code scanning.Read xgrep CI Integration

Workflow configuration options

The examples on this page use these with values:

  • path is what to scan when using the k8s-manifest or terraform-hcl action: the manifest file for k8s-manifest, or the Terraform working directory for terraform-hcl.

  • image is the container image name when using the docker-image action.

Every action that scans (all of them except policy, cnspec-lint, and xgrep) also accepts these optional values:

  • risk-threshold: the job fails if any risk meets or exceeds it. The default, 101, never fails the job. The examples below use 90, which fails the job only on critical risks. To learn how to choose a value, read Exit code handling.
  • output: the format of the results in the job log, such as compact (the default), summary, full, json, junit, or csv.
  • log-level: error, warn, info (the default), debug, or trace.

Pass the Mondoo service account credentials that you stored in the GitHub secret in the service-account-credentials with value. The action uses them to fetch policies and send results to Mondoo Platform. Never set credentials directly in the workflow configuration file. Reference the secret as ${{ secrets.MONDOO_CONFIG_BASE64 }}.

Example workflows

Kubernetes manifest scanning

This example runs a Mondoo scan against a Kubernetes manifest:

.github/workflows/manifest-scan.yml
name: mondoo-scan

on:
  pull_request:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v7
      - name: Scan with Mondoo
        uses: mondoohq/actions/k8s-manifest@main
        with:
          service-account-credentials: ${{ secrets.MONDOO_CONFIG_BASE64 }}
          path: nginx.yml
          # fail the job on critical risks (90 or higher); lower it to fail on less severe risks
          risk-threshold: 90

Terraform configuration files scanning

This example scans the Terraform configuration in the repository's terraform directory:

.github/workflows/terraform-scan.yml
name: mondoo-scan

on:
  pull_request:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v7
      - name: Scan with Mondoo
        uses: mondoohq/actions/terraform-hcl@main
        with:
          service-account-credentials: ${{ secrets.MONDOO_CONFIG_BASE64 }}
          path: terraform
          # fail the job on critical risks (90 or higher); lower it to fail on less severe risks
          risk-threshold: 90

Scan infrastructure as code

To scan every infrastructure as code entry point in the repository in one step, run cnspec scan iac. It finds and scans Ansible, Bicep, CloudFormation, Dockerfile, Helm, Kubernetes manifest, and Kustomize files, plus Terraform when you name it in --discover. New charts, templates, and modules are picked up without changing the workflow.

The iac provider requires cnspec 14.0 or later. None of the actions in Available actions runs cnspec scan iac, and the scan actions don't save a report file, so this workflow installs cnspec directly and passes the credentials in the MONDOO_CONFIG_BASE64 environment variable, which cnspec reads:

.github/workflows/iac-scan.yml
name: mondoo-iac-scan

on:
  pull_request:
  push:
    branches: [main]

jobs:
  scan:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v7
      - name: Install cnspec
        run: bash -c "$(curl -sSL https://install.mondoo.com/sh)"
      - name: Scan infrastructure as code with Mondoo
        env:
          MONDOO_CONFIG_BASE64: ${{ secrets.MONDOO_CONFIG_BASE64 }}
        # fail the job on critical risks (90 or higher); lower it to fail on less severe risks
        run: |
          cnspec scan iac . \
            --discover auto,terraform \
            --risk-threshold 90 \
            --output junit --output-target cnspec-junit.xml
      - name: Save scan report
        if: always()
        uses: actions/upload-artifact@v7
        with:
          name: cnspec-junit
          path: cnspec-junit.xml

Add opentofu to --discover if the repository uses OpenTofu. The iac provider is experimental; to learn what it detects and how to tune it, read Scan a Whole Infrastructure as Code Repository.

To let engineers request exceptions in the repository, add a mondoo.yml file with an exceptions list and route its review through your owners file. cnspec reports the exceptions on every scan and submits them to Mondoo Platform from pushes to the default branch. To learn how, read Manage Exceptions in Your Code Repositories.

Docker image scanning

This example scans a Docker image in a registry or built in a previous GitHub Action:

.github/workflows/docker-image-scan.yml
name: mondoo-scan

on:
  pull_request:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v7
      - name: Scan with Mondoo
        uses: mondoohq/actions/docker-image@main
        with:
          service-account-credentials: ${{ secrets.MONDOO_CONFIG_BASE64 }}
          image: ubuntu:22.04
          # fail the job on critical risks (90 or higher); lower it to fail on less severe risks
          risk-threshold: 90

Scan a GitHub organization or repository

The github-org action scans a GitHub organization and its repositories, and the github-repo action scans one repository. Both read a GitHub token from the GITHUB_TOKEN environment variable. Store a personal access token in a repository secret and pass it in the step's env. The token needs these scopes:

  • github-org: read:org, admin:org_hook, repo, workflow, and read:packages
  • github-repo: repo, workflow, and read:packages

This example scans the organization that owns the repository once a day. It expects the token in a secret named ORG_SCAN_TOKEN:

.github/workflows/github-org-scan.yml
name: mondoo-github-org-scan

on:
  schedule:
    - cron: '0 6 * * *'
  workflow_dispatch:

jobs:
  scan:
    runs-on: ubuntu-latest

    steps:
      - name: Scan GitHub organization with Mondoo
        uses: mondoohq/actions/github-org@main
        env:
          GITHUB_TOKEN: ${{ secrets.ORG_SCAN_TOKEN }}
        with:
          service-account-credentials: ${{ secrets.MONDOO_CONFIG_BASE64 }}
          organization: ${{ github.repository_owner }}

To scan one repository instead, use mondoohq/actions/github-repo@main with repository: ${{ github.repository }}.

By default, both actions report to the CI/CD view. To report the scanned organization and repositories as regular assets in the space's inventory instead, set is-cicd: false.

Large organizations hit GitHub's API rate limit

Scanning an organization with more than about 20 repositories is likely to exceed GitHub's API rate limit for personal access tokens and fail the job. For a larger organization, generate a GitHub App token in the workflow, as described in Using App Tokens, or scan the organization with a GitHub integration instead.

Show findings in GitHub code scanning

There are two ways to send findings to GitHub code scanning:

  • To scan source code for secrets and vulnerable code, use the workflow on the Code scanning (SARIF) tab of the GitHub Actions integration page in the Mondoo App. It runs xgrep with the mondoohq/actions/xgrep action and needs no Mondoo credentials, so you can skip the GitHub secret. To learn about its options, read xgrep CI Integration.
  • To report cnspec policy results, such as failing Terraform checks, run cnspec with SARIF output as described below.

Report cnspec results

cnspec can write its results as SARIF, which GitHub code scanning shows as alerts on the repository's Security tab and as annotations on pull request diffs. Each alert has a severity based on the check's impact, and a check that keeps failing updates the same alert instead of opening a new one.

Before you start, make sure that:

  • Code scanning is available for the repository. It is for public repositories. Private repositories need GitHub Code Security.
  • The policies you want to report on are enabled in your Mondoo space. A registered scan runs the policies enabled in the space that the service account belongs to.

The scan actions in Available actions don't write a report to a file, so this workflow installs cnspec directly. It scans the Terraform configuration in the repository's terraform directory and uploads the results with GitHub's upload-sarif action:

.github/workflows/mondoo-code-scanning.yml
name: mondoo-code-scanning

on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read
  security-events: write

jobs:
  scan:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v7
      - name: Install cnspec
        run: bash -c "$(curl -sSL https://install.mondoo.com/sh)"
      - name: Scan Terraform with Mondoo
        env:
          MONDOO_CONFIG_BASE64: ${{ secrets.MONDOO_CONFIG_BASE64 }}
        run: cnspec scan terraform terraform --output sarif --output-target cnspec.sarif
      - name: Upload results to GitHub code scanning
        if: always()
        uses: github/codeql-action/upload-sarif@v4
        with:
          sarif_file: cnspec.sarif
          category: mondoo-terraform

A few details of this workflow:

  • security-events: write lets the job upload results. The job needs no other write permissions.
  • if: always() uploads the results even when the scan step fails.
  • category keeps cnspec's alerts apart from CodeQL and other tools that upload SARIF to the same repository. If you run more than one cnspec scan job, give each its own category.
  • Failing Terraform checks point at the file and line of the offending resource, so the alert shows the code to fix.

The workflow doesn't set --risk-threshold, so findings appear as alerts without failing the job. To block pull requests, either add --risk-threshold 90 to the scan command (the upload step still runs, because of if: always()), or keep the job passing and use a repository ruleset that requires code scanning results. To learn about choosing a threshold, read Exit code handling.

Next steps

Last updated on

On this page