DEV Community

Cover image for How to set up automated code quality gates in GitHub Actions in under 5 minutes
Muhammad Usman Shabir
Muhammad Usman Shabir

Posted on

How to set up automated code quality gates in GitHub Actions in under 5 minutes

Every developer has shipped code they knew wasn't ready. Maybe it was a Friday afternoon merge, maybe the reviewer was in a rush. Either way, that shortcut cost the team hours — or worse, a production incident.

What if your CI pipeline could catch that before it ever reaches main?

That's exactly what a code quality gate does. And you can set one up in under five minutes.

What Is a Quality Gate?

A quality gate is a minimum quality score your code must pass before it's allowed to merge. Think of it as a bouncer for your codebase — if a pull request doesn't meet the threshold, the merge is blocked automatically.

It evaluates things like code complexity, readability, maintainability, and potential bugs, then returns a score. If the score falls below your configured minimum, the GitHub check fails and the PR can't be merged until the issues are fixed. No human gatekeeper needed. No bad code slipping through during a busy sprint.

Why You Need One

Bugs are cheap to fix when you catch them early. A logic error spotted during code review might take ten minutes to resolve. That same bug in production? It could mean rollback deployments, customer complaints, incident postmortems, and hours of debugging under pressure.

IBM's Systems Sciences Institute found that fixing a defect in production costs roughly six times more than fixing it during implementation. Other industry research puts the multiplier even higher.

The problem isn't that teams don't review code — it's that manual reviews are inconsistent. Reviewers get fatigued, context gets lost, and standards drift over time. An automated quality gate removes that variability. Every PR gets evaluated against the same criteria, every time.

It also takes pressure off senior developers who often end up as the bottleneck in review cycles. Instead of manually gatekeeping every merge, they can focus on architecture and mentorship while the gate handles the baseline checks.

Step-by-Step Setup

Here's how to get automated quality gates running on your repo using GetCodeReviews and GitHub Actions.

Step 1: Sign Up and Get Your API Key

Head to getcodereviews.com and create an account. Once you're in, navigate to your dashboard and grab your API key. You'll need this to authenticate the GitHub Action with the service.

Step 2: Add Your API Key to GitHub Secrets

Go to your GitHub repository, then Settings → Secrets and variables → Actions → New repository secret.

  • Name: CODESCAN_API_KEY
  • Value: Paste your API key from Step 1

This keeps your key secure and out of your codebase.

Step 3: Add the Workflow File

Create a new file at .github/workflows/code-quality-gate.yml and paste the following:

name: Code Quality Gate

on:
  pull_request:
    branches: [main, develop]

jobs:
  quality-check:
    runs-on: ubuntu-latest
    name: Run Code Quality Review

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get changed files
        id: changed
        run: |
          FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- '*.js' '*.ts' '*.jsx' '*.tsx' '*.py' '*.go' '*.rb' | head -20)
          echo "files=$FILES" >> $GITHUB_OUTPUT

      - name: Run GetCodeReviews Quality Scan
        id: review
        run: |
          RESPONSE=$(curl -s -X POST https://getcodereviews.com/api/review \
            -H "Authorization: Bearer ${{ secrets.CODESCAN_API_KEY }}" \
            -H "Content-Type: application/json" \
            -d "{
              \"repo\": \"${{ github.repository }}\",
              \"pr_number\": ${{ github.event.pull_request.number }},
              \"files\": \"${{ steps.changed.outputs.files }}\"
            }")
          SCORE=$(echo $RESPONSE | jq -r '.score')
          echo "score=$SCORE" >> $GITHUB_OUTPUT
          echo "## Code Quality Report" >> $GITHUB_STEP_SUMMARY
          echo "**Score: $SCORE / 100**" >> $GITHUB_STEP_SUMMARY
          echo "$RESPONSE" | jq -r '.summary' >> $GITHUB_STEP_SUMMARY

      - name: Enforce Quality Gate
        run: |
          SCORE=${{ steps.review.outputs.score }}
          MINIMUM=70
          if [ "$SCORE" -lt "$MINIMUM" ]; then
            echo "❌ Quality gate failed. Score: $SCORE (minimum: $MINIMUM)"
            exit 1
          else
            echo "✅ Quality gate passed. Score: $SCORE"
          fi
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Your Minimum Score

In the workflow above, the MINIMUM variable is set to 70. This is a good starting point — strict enough to catch genuinely problematic code but forgiving enough that it won't block every minor style issue.

As your team gets comfortable, you can raise it to 80 or even 85. You can also make it configurable per-branch if you want stricter enforcement on main than on develop.

Step 5: Test It With a Bad PR

Push a branch with some intentionally sloppy code — deeply nested logic, no error handling, duplicated blocks. Open a pull request against main and watch the action run. You should see the check fail with a score below your threshold, along with a summary of what needs fixing.

This confirms the gate is working before your team relies on it.

What Happens When a PR Fails the Gate?

When a pull request scores below the minimum threshold, the GitHub check turns red and blocks the merge. The developer sees a clear summary in the PR timeline showing their score, what went wrong, and where to focus their fixes.

There's no guessing involved. The report highlights specific issues — things like high cyclomatic complexity, missing error handling, or code duplication — so the developer knows exactly what to address. They fix the flagged issues, push again, and the gate re-evaluates automatically.

This feedback loop is fast. Most developers get their PR passing on the second push. Over time, you'll notice that first-push quality starts improving on its own because the team internalizes the standards the gate enforces.

It also eliminates those awkward code review conversations where a senior dev has to tell someone their code isn't up to standard. The gate delivers the feedback objectively, and the team just focuses on fixing it.

Wrapping Up

Setting up a quality gate takes five minutes but saves your team hours every week. Bad code gets caught before it merges, reviews become faster, and your codebase stays consistent without relying on manual discipline.

If you want to explore advanced configurations — custom rulesets, team dashboards, or VS Code integration — check out the full documentation at getcodereviews.com/docs/api.

Your future self (and your on-call rotation) will thank you.

Top comments (0)