How to Fail CI on Stray console.log Statements in GitHub Actions
A forgotten console.log leaks into production and clutters logs; a tiny CI check stops it at the PR.
Grep the source tree for console.log (excluding tests and vendored code) and exit non-zero when any match is found.
Steps
- Restrict the search to source directories so tests and node_modules are skipped.
- Run
grep -rn "console.log" srcand invert the exit code so a match fails. - Prefer the ESLint
no-consolerule for a real codebase; grep is the zero-config fallback.
Workflow
.github/workflows/no-console.yml
name: No console.log
on: [pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Fail on console.log
run: |
if grep -rn --include='*.ts' --include='*.tsx' 'console\.log' src; then
echo "::error::Remove console.log before merging"
exit 1
fiNotes
- The
::error::workflow command marks the run with a clear message in the checks UI. - On Latchkey managed runners these guard checks run cheaper and self-heal if a runner drops.
Related guides
How to Run ESLint and Annotate PRs in GitHub ActionsRun ESLint in GitHub Actions and surface violations as inline PR annotations so reviewers see each problem on…
How to Run a Secrets Scan on PRs in GitHub ActionsScan pull requests for leaked credentials in GitHub Actions with gitleaks so an accidentally committed key fa…