| | |

GitHub Pages 07 ๐Ÿ“„ Environments, Protection Rules & Concurrency โ€” Securing the github-pages environment with branch/tag rules, reviewers, and concurrency

โฑ๏ธ Estimated read: 9 min | |

As GitHub Pages deployments transition from simple branch-synching to complex GitHub Actions pipelines, the need for governance grows. A production website should not be updated by every minor branch push or accidental experiment. GitHub Environments provide a logical abstraction to wrap a deployment target with security policies, allowing teams to gate who can deploy, from which branch they can deploy, and under what conditions.

The github-pages environment is a specialized environment automatically used by GitHub to track the history and status of your site. By configuring protection rules on this environment, you can enforce manual approvals or restrict deployments to specific tags or branches. Coupled with the concurrency keyword in your YAML workflow, you can ensure that multiple developers pushing code simultaneously do not create “race conditions” where an older build overwrites a newer one.

This chapter covers the configuration of the github-pages environment, the implementation of deployment protection rules like required reviewers, and the technical setup of concurrency groups to maintain a stable deployment queue.

Key point: GitHub Environments act as a security gate for deployments; by targeting the github-pages environment in your workflow, you can enforce manual approvals and branch restrictions while using concurrency groups to prevent overlapping deployment runs.


Why X exists

1. The “Race Condition” problem.

If two developers push code seconds apart, two GitHub Actions runs start simultaneously. Without concurrency control, the build that finishes secondโ€”which might be the older versionโ€”could overwrite the newer version on the live server. Concurrency groups ensure that only one deployment happens at a time.

2. The need for human oversight.

Automated CI/CD is efficient, but production sites often require a “final check.” Deployment reviewers allow a lead developer or stakeholder to manually approve a build after it passes testing but before it goes live, providing a crucial safety buffer.

3. Restricting the source of truth.

Without protection rules, any branch in the repository could potentially trigger a deployment if the workflow file allows it. Deployment branch rules restrict the environment so it only accepts deployments from a specific branch (like main) or a specific tag pattern (like v*).

4. Visibility and audit trails.

Environments provide a dedicated “Deployments” dashboard in the GitHub UI. This allows teams to see exactly what version is currently live, who approved the deployment, and a full history of successful or failed attempts, which is far superior to digging through Action logs.

5. Managing secrets per stage.

While GitHub Pages is typically a single-stage environment, GitHub Environments allow you to define secrets that are only accessible to jobs targeting that environment. This ensures that production-only API keys are never exposed to test jobs or pull request previews.


a. Configuring the github-pages Environment

When you set your GitHub Pages source to “GitHub Actions,” GitHub often creates a default environment named github-pages. You can find this in your repository settings under Settings > Environments. Targeting this environment in your YAML workflow is what enables the deployment tracking and protection rules.

jobs:
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Deploy
        id: deployment
        uses: actions/deploy-pages@v4

By explicitly naming the environment, you link the job to the GitHub UI’s deployment timeline. If you have defined “Required Reviewers” in the environment settings, this job will pause automatically and wait for a signature before the deploy-pages action executes.


b. Deployment Protection Rules: Reviewers and Branches

Protection rules are the primary mechanism for securing the github-pages environment. There are two critical types of rules:

  1. Required Reviewers: You can select up to six users or teams who must approve a deployment. This is ideal for ensuring that a QA lead or Product Manager has seen the final build on a staging URL before it hits production.
  2. Deployment Branch Rules: You can specify that only code from main or specific tags can be deployed to the environment. If a developer tries to run a deployment from a feature-branch, GitHub will block the job from accessing the environment.
# Example: This job will fail if run from 'feature-fix' 
# because of a branch rule on the environment.
jobs:
  deploy:
    environment: github-pages
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/deploy-pages@v4

These rules are configured in the GitHub web interface, not the YAML file. This is an intentional security design: it prevents a developer from simply editing the YAML to bypass the protection rules.


c. Managing Race Conditions with concurrency

The concurrency property is a top-level YAML configuration that groups workflow runs. In the context of GitHub Pages, we use a fixed string (like "pages") to ensure that all deployments across all branches are part of the same queue.

concurrency:
  group: "pages"
  cancel-in-progress: true

The cancel-in-progress: true setting is vital. If Run #1 is currently building and Run #2 starts, GitHub will automatically kill Run #1. This saves billable minutes and ensures that the most recent code is always the one that eventually finishes and goes live.

Without this, if you pushed five times in a minute, you would have five overlapping deployment attempts, potentially leading to the “old code overwriting new code” scenario if the build times vary.


Complete Example Session

# ============================================
# PART 1: WORKFLOW IDENTITY
# ============================================
name: Secure Production Deploy
on:
  push:
    branches: ["main"]

# ============================================
# PART 2: CONCURRENCY (Race Condition Defense)
# ============================================
concurrency:
  group: "pages"
  cancel-in-progress: true

# ============================================
# PART 3: OIDC PERMISSIONS
# ============================================
permissions:
  contents: read
  pages: write
  id-token: write

# ============================================
# PART 4: THE BUILD JOB
# ============================================
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build Site
        run: npm install && npm run build
      - name: Upload Artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: './dist'

# ============================================
# PART 5: THE SECURED DEPLOY JOB
# ============================================
  deploy:
    # --------------------------------------------
    # PART 6: ENVIRONMENT TARGETING
    # This triggers 'Required Reviewers' rules
    # --------------------------------------------
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
  
    runs-on: ubuntu-latest
    needs: build
  
    steps:
      # --------------------------------------------
      # PART 7: FINAL DEPLOY
      # --------------------------------------------
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

# ============================================
# PART 8: DEPLOYMENT BRANCH CHECK
# ============================================
# Note: Usually enforced in GitHub Settings, 
# but good to have in YAML for clarity.

# ============================================
# PART 9: MANUAL APPROVAL
# ============================================
# When this job runs, it will pause here and 
# notify reviewers if configured in Settings.

# ============================================
# PART 10: AUDIT LOG
# ============================================
# The result appears in the 'Deployments' sidebar.

The parts covered the workflow identity, concurrency management, permission escalation, and the connection to the GitHub Environment for manual approval gating.


Quick Reference

Concurrency Settings

PropertyValueEffect
group"pages"Forces all runs into one queue
cancel-in-progresstrueDrops older runs for newer ones
group${{ github.ref }}Segregates queues by branch

Protection Rules Comparison

RuleBest ForRequirement
ReviewersHuman AuditGitHub Pro/Team/Enterprise
Wait TimerPropagation DelayDelay before deploy
Branch RulesVersion ControlRestrict to main

Environment Permissions

PermissionRole
id-token: writeRequired for OIDC / Pages
pages: writeRequired to update content
contents: readRequired to fetch source

Best Practices

โœ… Do This:

# Use a static concurrency group for Pages               # โœ…
concurrency: { group: "pages", cancel-in-progress: true }   # โœ…
# Target the 'github-pages' environment explicitly         # โœ…
environment: github-pages                                   # โœ…
# Restrict deployments to the 'main' branch or tags       # โœ…
if: github.ref == 'refs/heads/main'                         # โœ…
# Set up a team as a reviewer for production              # โœ…

โŒ Don’t Do This:

# Don't deploy without a concurrency group               # โŒ
# Don't use different group names for the same site      # โŒ
# Don't store production secrets in Repo Secrets
# (Use Environment Secrets instead)                       # โŒ
# Don't bypass manual approval for critical sites         # โŒ

Common Pitfalls

PitfallWhy It HappensFix
Overwritten ContentNo concurrency groupAdd concurrency: pages
Approval Request IgnoredNotification fatigueAssign a Team, not just individuals
Job Skipped Unfairlycancel-in-progress on slow buildsMonitor build time vs push frequency
Permissions Errorid-token missing in environmentEnsure YAML has correct permissions block
Rule BypassedRules set on wrong environmentConfirm job targets github-pages

Real-World Examples

1. Global Concurrency

concurrency: pages

2. Environment with URL

environment:
  name: github-pages
  url: https://example.com

3. Tag-based Deployment

if: startsWith(github.ref, 'refs/tags/v')

4. Team Reviewer Setting

// GitHub Settings UI:
// Reviewers -> @my-org/web-leads

5. Deployment Branch Rule

// GitHub Settings UI:
// Deployment Branches -> Selected Branches -> main

6. Accessing Env Secrets

env:
  API_KEY: ${{ secrets.PROD_API_KEY }} # Env-specific

7. Wait Timer

// GitHub Settings UI:
// Wait Timer -> 5 minutes (for cache clearing)

8. Conditional Build

on:
  push:
    branches: [main, develop]

9. Production URL Mapping

url: ${{ steps.deployment.outputs.page_url }}

10. Manual Trigger

on: [workflow_dispatch]

Visual

The Deployment Gate

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  GITHUB ACTIONS โ”‚       โ”‚  GITHUB ENVIRONMENT     โ”‚       โ”‚  LIVE SITE  โ”‚
โ”‚  (Worklow Run)  โ”‚       โ”‚  (github-pages)         โ”‚       โ”‚  (Pages)    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                             โ”‚                           โ”‚
         โ”‚   1. Request Deployment     โ”‚                           โ”‚
         โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚   2. Validate Rules       โ”‚
         โ”‚                             โ”‚   (Branch? Reviewer?)     โ”‚
         โ”‚                             โ”‚             โ”‚             โ”‚
         โ”‚   3. Pause for Approval     โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜             โ”‚
         โ”‚   (Human clicks 'Approve')  โ”‚                           โ”‚
         โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค                           โ”‚
         โ”‚                             โ”‚                           โ”‚
         โ”‚   4. Execute deploy-pages   โ”‚                           โ”‚
         โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚
         โ”‚                                                         โ”‚

Concurrency Stack Logic

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ CONCURRENCY GROUP: "pages"                                  โ”‚
โ”‚                                                             โ”‚
โ”‚  [RUN 1]  Started 12:00  -->  (BUILDING)                    โ”‚
โ”‚                                                             โ”‚
โ”‚  [RUN 2]  Started 12:01  -->  [NEW RUN DETECTED]            โ”‚
โ”‚                                                             โ”‚
โ”‚  [RESULT] Run 1: CANCELLED (Red dot)                        โ”‚
โ”‚           Run 2: PROCEEDING (Green spinning)                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Environment Audit Trail

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ REPOSITORY > DEPLOYMENTS                                    โ”‚
โ”‚                                                             โ”‚
โ”‚  github-pages                                               โ”‚
โ”‚  ---------------------------------------------------------  โ”‚
โ”‚  v1.0.4  Active      by @alice (Approved by @bob)           โ”‚
โ”‚  v1.0.3  Inactive    2 hours ago                            โ”‚
โ”‚  v1.0.2  Failed      5 hours ago                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Env Namegithub-pages
Concurrency TargetFixed string (e.g., "pages")
Strategycancel-in-progress: true
Protection 1Required Reviewers
Protection 2Deployment Branch Rules
DashboardRepository -> Deployments
Secret ScopeEnvironment-level

Key takeaways:

  • Gating is essential for production. Use the github-pages environment to ensure that deployments are not automatic and require oversight for high-stakes projects.
  • Concurrency is your insurance policy. By grouping runs under a single concurrency label, you eliminate the risk of race conditions and out-of-order deployments.
  • Rules are external to YAML. Deployment protection rules (reviewers, branch limits) are managed in GitHub Settings to prevent unauthorized changes via PR.
  • cancel-in-progress saves time. Automatically stopping older, irrelevant builds ensures that your pipeline is always working on the most recent code.
  • Use the Audit Trail. Environments give you a historical record of every deployment, providing better debugging and accountability than simple Action logs.
  • Secrets belong in Environments. For production-sensitive variables, move them from general repository secrets to environment-specific secrets for better isolation.

Remember: A secure GitHub Pages site requires both a solid YAML configuration (for concurrency and OIDC) and a properly configured Environment in the GitHub UI (for protection rules and reviewers). These two halves work together to create a professional, enterprise-grade deployment pipeline.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!