In this hands-on lab your will create environments and a staged deployment workflow with approvals.
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
Production
and clickConfigure environment
- Add yourself as the
Required reviewer
for this environment:
- Only allow the
main
branch to be deployed to this environment:
- Create two more environments.
Test
andLoad-Test
without any restrictions.
Add a input of the type environment to the workflow_dispatch
trigger.
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-latest
afterBuild
. Only runs when the workflow was triggered manually. Runs on the environmentTest
. The job writesTesting...
to the workflow log. - Load-Test: runs on
ubuntu-latest
afterBuild
. 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-latest
afterTest
andLoad-Test
. Deploys to the environmentProduction
onyl 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
Production
as 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
keeword.
You can continue with the README.