Skip to content

Latest commit

 

History

History
128 lines (94 loc) · 5.12 KB

03-Staged-deployments.md

File metadata and controls

128 lines (94 loc) · 5.12 KB

🔨 Hands-on: Staged deployments

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 private repos 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

  1. Go to Settings | Environments and click New environment
  2. Enter the name Production and click Configure environment
  3. Add yourself as the Required reviewer for this environment:

image

  1. Only allow the mainbranch to be deployed to this environment:

image

  1. Create two more environments. Test and Load-Test without any restrictions.

Adding a input for picking environments to manual workflow trigger

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

Chaining workflow steps and conditional execution

  1. Now add 3 jobs to the workflow file:
  • Test: runs on ubuntu-latest after Build. Only runs when the workflow was triggered manually. Runs on the environment Test. The job writes Testing... to the workflow log.
  • Load-Test: runs on ubuntu-latest after Build. Only runs when the workflow was triggered manually. Runs on the environment Load-Test. The job writes Testing... to the workflow log and sleeps for 15 seconds.
  • Production: runs on ubuntu-latest after Test and Load-Test. Deploys to the environment Production onyl if this was selected as the input parameter. The environment has the URL https://writeabout.net. To simulate deployment, the job will execute 5 steps. Each step with writes Step 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
  1. Trigger the workflow and select Production as the environment:

image

Reviewing and approving deployments

  1. Open the workflow run and start the review.

image

  1. And approve it with a comment:

image

  1. Note the progress bar while deploying...

image

  1. The result looks like this and contains the approval and the URL for the production environment:

image

Summary

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.