AI Hints
CI/CD with GitHub Actions · Lesson 69 of 138

Secret Scanning

Keys that slip into code.

⏱ 2 min read▶ Video at 1:19:02🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Sometimes a real password, or a cloud key, gets committed by accident. Bots scan public repositories for keys within minutes. GitHub's secret scanning watches for this, and you can add your own check to CI as well.

Secret Scanning
From the video: Secret Scanning
  • Keys get committed by accident
  • Bots find them in minutes
  • Scan for them

A simple check

This step searches the repository for text shaped like an Amazon cloud key. AKIA, then sixteen capital letters or digits. If grep finds one, the step prints it and fails. Real tools, like GitHub's push protection, or gitleaks, know hundreds of key shapes.

.github/workflows/secrets.ymlyaml
name: Secret check
on: [push, pull_request]
jobs:
  secrets:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Look for cloud keys
        run: |
          if grep -rnE "AKIA[0-9A-Z]{16}" --exclude-dir=.git .; then
            echo "Found something that looks like a key"; exit 1
          fi
          echo "No keys found"

Catching a key

First, the check passes. No keys. Then someone pastes a key into a config file, by accident. The check finds it, shows the file and line, and fails. If a real key is ever pushed, removing it isn't enough. Revoke it immediately, because it may already have been copied.

Terminal
run-steps .github/workflows/secrets.yml | grep -E "✓|No keys"
  ✓ Look for cloud keys
      No keys found
echo 'aws_key = AKIAABCDEFGHIJKLMNOP' > config.py
run-steps .github/workflows/secrets.yml | grep -vE "^Workflow|↷"
Job: secrets
  ✗ Look for cloud keys
      ./config.py:1:aws_key = AKIAABCDEFGHIJKLMNOP
      Found something that looks like a key
      Error: Process completed with exit code 1.
  Job failed

Summary

Secret scanning catches keys before or soon after they're pushed. If one leaks, revoke it straight away. Next, protecting the main branch.

Key takeaways
  • Push protection
  • Your own checks
  • Leaked? Revoke it
Next up: Branch Protection — guarding main