Lab : Deployment Protection Rules
This lab will guide you through configuring Required Reviewers as a deployment protection rule for a specific environment (e.g., prod) in GitHub. You’ll also learn how to create a workflow to demonstrate this protection rule in action.
Objective
- Understand how to configure deployment protection rules in GitHub environments.
- Set up a GitHub Actions workflow that triggers deployment to environments with and without protection rules.
- Observe and understand how required reviewers affect the workflow execution.
Prerequisites
- A GitHub repository.
- Two pre-created environments:
devandprod. - Admin access to the repository to configure environment settings.
- A GitHub Actions workflow file to deploy to the environments.
Steps
Step 1: Create and Configure Environments
- Navigate to your GitHub repository.
- Go to Settings > Environments.
- Create two environments:
- dev
- prod
- Click on the prod environment and configure the following:
- Protection Rules:
- Enable “Required reviewers”.
- Add specific reviewers (e.g., yourself or other team members).
- Deployment Branches:
- Optionally restrict deployments to specific branches (e.g.,
main).
- Optionally restrict deployments to specific branches (e.g.,
- Protection Rules:
Step 2: Create the Workflow
Set up a workflow to deploy to both dev and prod environments. The workflow includes steps for deploying to both environments, highlighting the approval required for prod.
Create a workflow file under .github/workflows/env-deployment-protection-rule.yml:
name: Deployment Workflow
on:
push:
branches:
- main
jobs:
deploy-to-dev:
runs-on: ubuntu-latest
environment: dev
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Deploy to Dev
run: echo "Deploying to the dev environment."
deploy-to-prod:
needs: deploy-to-dev
runs-on: ubuntu-latest
environment: prod
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Deploy to Prod
run: echo "Deploying to the prod environment."
Step 3: Understand the Workflow
-
Trigger:
- The workflow is triggered on a
pushto themainbranch.
- The workflow is triggered on a
-
Jobs:
deploy-to-dev:- Deploys to the
devenvironment without requiring approval.
- Deploys to the
deploy-to-prod:- Deploys to the
prodenvironment, but requires approval due to the protection rule.
- Deploys to the
-
Approval Process:
- When the workflow reaches the
deploy-to-prodstep, it pauses and requires the assigned reviewers to approve the deployment before proceeding.
- When the workflow reaches the
Step 4: Commit and Push the Workflow
- Save the workflow file.
-
Commit and push it to the
mainbranch:git add .github/workflows/env-deployment-protection-rule.yml git commit -m "Add deployment workflow with protection rule" git push origin main
Step 5: Trigger the Workflow
- Push changes to the
mainbranch or manually trigger the workflow from the Actions tab. - Observe the workflow execution:
- The
deploy-to-devjob completes without interruptions. - The
deploy-to-prodjob pauses and waits for approval.
- The
Step 6: Approve the Deployment
- Navigate to the Actions tab in your repository.
- Select the running workflow.
- Click Review deployments under the paused
deploy-to-prodstep. - Approve the deployment as a required reviewer.
Expected Outcome
- The workflow successfully deploys to
devwithout interruption. - The workflow pauses at
produntil required reviewers approve the deployment. - Once approved, the deployment to
prodresumes and completes.
Key Discussion Points
-
Why Use Protection Rules?
- Ensures critical environments like
prodare accessed and modified only with proper oversight. - Helps enforce change management processes.
- Ensures critical environments like
-
Difference Between
devandprodin This Lab:devallows direct deployment without approval.prodrequires approval to simulate a real-world production environment.
-
Scenarios for Using Required Reviewers:
- Critical production releases.
- Compliance or security-sensitive environments.
- Multi-stakeholder sign-offs.
Lab Extensions
-
Simulate a Failed Approval:
- Reject the approval and observe how the workflow fails to proceed.
-
Add Conditional Deployment:
- Use
ifconditions to skip deployment toprodbased on certain criteria, such as branch name or pull request labels.
- Use
-
Expand to Multi-Environment Workflow:
- Add more environments (e.g.,
staging,qa) with varying levels of protection.
- Add more environments (e.g.,
Conclusion
This lab demonstrates how to configure and use deployment protection rules in GitHub environments, specifically using required reviewers for production deployments. This setup is critical for maintaining oversight and quality control in CI/CD pipelines, especially when managing sensitive or high-risk deployments.