I had three repos with pre-commit hooks and the same checks copied between them. I wanted one public library of GitHub Actions workflows that every repo calls: secret scanning, Python static analysis, and Terraform checks. Each repo’s CI file shrinks to a few lines, and a scanner upgrade happens in one place.

Why CI when pre-commit already runs

pre-commit runs on my machines, and it’s easy to skip. git commit --no-verify bypasses it, and a machine without the hooks installed never runs them. CI runs on GitHub for every push and pull request, whoever made the commit and wherever they made it. Defense in depth: the hook is the first net, CI is the second.

Keeping the checks in one library also keeps versions consistent. The CI jobs use the same Gitleaks, Bandit and Checkov versions as the pre-commit hooks, so a commit that passes locally doesn’t fail in CI for a version difference.

The library

The library repo holds three reusable workflows, each triggered by workflow_call:

Workflow What it runs
secrets-scan.yml Gitleaks over the full git history
python-sast.yml Bandit (medium and high severity), plus pytest when a repo has tests
terraform-scan.yml terraform fmt -check, init -backend=false and validate, optional tflint, then Checkov

None of them need cloud credentials. Plan and apply stay in each repo, because they need that repo’s own cloud login.

Two habits run through every workflow, both from GitHub’s security guidance [1]:

  • Every action is pinned to a full commit SHA, with the version in a comment. A tag can be moved or deleted if someone gains access to the action’s repo.
  • Inputs go through environment variables, never pasted straight into a script. That’s GitHub’s preferred way to handle input in inline scripts, and it closes off script injection:
- name: Bandit
  env:
    SOURCE_DIR: $
  run: '"$RUNNER_TEMP/venv/bin/bandit" -r "$SOURCE_DIR" -ll'

Calling it from a repo

Each repo’s CI file only calls the library, pinned to the library’s own commit SHA:

name: CI

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  secrets:
    uses: <github-user>/<library-repo>/.github/workflows/secrets-scan.yml@<library-commit-sha> # v1.1.0
    permissions:
      contents: read
      pull-requests: read

  terraform:
    uses: <github-user>/<library-repo>/.github/workflows/terraform-scan.yml@<library-commit-sha> # v1.1.0
    with:
      working_directory: "."
      var_file: "prod.tfvars"
      checkov_frameworks: "terraform cloudformation"

That pull-requests: read line came from two failures. First, Gitleaks failed on a pull request in a private repo with “Resource not accessible by integration”, because it lists the pull request’s commits through the GitHub API, which needs that permission. I added it to the library. Then every caller failed with startup_failure, because permissions can only be kept or reduced down a chain of reusable workflows, never raised [2]. The library asked for more than the callers granted. Granting it in each caller fixed it.

Two Gitleaks settings are worth copying [3]: GITLEAKS_ENABLE_COMMENTS: "false", so it doesn’t need write access to comment on pull requests, and GITLEAKS_ENABLE_UPLOAD_ARTIFACT: "false", because the findings report would contain the secret it just caught.

Keeping pins fresh with Dependabot

SHA pins don’t update themselves. Dependabot does it: in each repo, a .github/dependabot.yml for the github-actions ecosystem opens a pull request when the library or an action gets a newer version. For Python dependencies, a groups entry combines every update into a single pull request [4], so one weekly PR replaces a pile of one-package PRs:

version: 2
updates:
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"
  - package-ecosystem: "pip"
    directory: "/"
    schedule:
      interval: "weekly"
    groups:
      python-deps:
        patterns: ["*"]

The file goes in .github/, not in .github/workflows/. I put it in the wrong folder once, and GitHub tried to run it as a workflow.

Protecting the library itself

If every repo trusts this library, the library is now a target. It’s protected with two rulesets, both with no bypass list:

  • protect-main: no deletion, no force push, and changes only through a pull request.
  • protect-version-tags (on v* tags): no deletion, no update, no force push. A version tag like v1.1.0 always points at the same commit.

Rulesets can target branches or tags, and they’re available on public repos on the Free plan, while private repos need Pro or Team [5]. Secret scanning push protection is also on, so a push that contains a secret is blocked before it reaches the repo [6].

References

  1. GitHub Docs: Secure use reference
  2. GitHub Docs: Creating a reusable workflow
  3. Gitleaks GitHub Action
  4. GitHub Docs: Dependabot options reference
  5. GitHub Docs: About rulesets
  6. GitHub Docs: About push protection

Comment section

Sorry babes, you have to sign into Github to comment. Feel free to let me know what's on your mind!