GitHub Pages 07 ๐ Environments, Protection Rules & Concurrency โ Securing the github-pages environment with branch/tag rules, reviewers, and concurrency
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:
- 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.
- Deployment Branch Rules: You can specify that only code from
mainor specific tags can be deployed to the environment. If a developer tries to run a deployment from afeature-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
| Property | Value | Effect |
|---|---|---|
group | "pages" | Forces all runs into one queue |
cancel-in-progress | true | Drops older runs for newer ones |
group | ${{ github.ref }} | Segregates queues by branch |
Protection Rules Comparison
| Rule | Best For | Requirement |
|---|---|---|
| Reviewers | Human Audit | GitHub Pro/Team/Enterprise |
| Wait Timer | Propagation Delay | Delay before deploy |
| Branch Rules | Version Control | Restrict to main |
Environment Permissions
| Permission | Role |
|---|---|
id-token: write | Required for OIDC / Pages |
pages: write | Required to update content |
contents: read | Required 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
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Overwritten Content | No concurrency group | Add concurrency: pages |
| Approval Request Ignored | Notification fatigue | Assign a Team, not just individuals |
| Job Skipped Unfairly | cancel-in-progress on slow builds | Monitor build time vs push frequency |
| Permissions Error | id-token missing in environment | Ensure YAML has correct permissions block |
| Rule Bypassed | Rules set on wrong environment | Confirm 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
| Item | Value |
|---|---|
| Env Name | github-pages |
| Concurrency Target | Fixed string (e.g., "pages") |
| Strategy | cancel-in-progress: true |
| Protection 1 | Required Reviewers |
| Protection 2 | Deployment Branch Rules |
| Dashboard | Repository -> Deployments |
| Secret Scope | Environment-level |
Key takeaways:
- Gating is essential for production. Use the
github-pagesenvironment 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-progresssaves 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.
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!