In this hands-on lab your will create environments and a staged deployment workflow with approvals.
Note: you can only do this on public repos for free accounts. Adding environments for
privaterepos is only available for Organizations with a Team account (or higher) and users with GitHub Pro accounts.
This hands on lab is based on My first workflow and adds the following steps:
- Creating and protecting environments
- Adding a input for picking environments to manual workflow trigger
- Chaining workflow steps and conditional execution
- Reviewing and approving deployments
- Go to Settings | Environments and click New environment
- Enter the name
Productionand clickConfigure environment - Add yourself as the
Required reviewerfor this environment:
- Only allow the
mainbranch to be deployed to this environment:
- Create two more environments.
TestandLoad-Testwithout any restrictions.
Modify the workflow yml file you created in hands on lab 1 ('My first workflow').
Add an input of the type environment to the workflow_dispatch trigger that you created previously.
Solution
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
type: environment
required: true- Now add 3 jobs to the workflow file:
- Test: runs on
ubuntu-latestafterBuild. Only runs when the workflow was triggered manually. Runs on the environmentTest. The job writesTesting...to the workflow log. - Load-Test: runs on
ubuntu-latestafterBuild. Only runs when the workflow was triggered manually. Runs on the environmentLoad-Test. The job writesTesting...to the workflow log and sleeps for 15 seconds. - Production: runs on
ubuntu-latestafterTestandLoad-Test. Deploys to the environmentProductiononyl if this was selected as the input parameter. The environment has the URLhttps://writeabout.net. To simulate deployment, the job will execute 5 steps. Each step with writesStep x deploying...to the workflow log and sleeps for 10 seconds.
Solution
Test:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
needs: Build
environment: Test
steps:
- run: echo "🧪 Testing..."
Load-Test:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
needs: Build
environment: Load-Test
steps:
- run: |
echo "🧪 Testing..."
sleep 15
Production:
runs-on: ubuntu-latest
needs: [Test, Load-Test]
environment:
name: Production
url: https://writeabout.net
if: github.event.inputs.environment == 'Production'
steps:
- run: |
echo "🚀 Step 1..."
sleep 10
- run: |
echo "🚀 Step 2..."
sleep 10
- run: |
echo "🚀 Step 3..."
sleep 10
- run: |
echo "🚀 Step 4..."
sleep 10
- run: |
echo "🚀 Step 5..."
sleep 10- Trigger the workflow and select
Productionas the environment:
- Open the workflow run and start the review.
- And approve it with a comment:
- Note the progress bar while deploying...
- The result looks like this and contains the approval and the URL for the production environment:
In this lab you have learned to create and protect environments in GitHub and use them in a workflow. You have also learned to conditionally
execute jobs or steps and to chain jobs using the needs keyword.
You can continue with the Reusable workflows.






