Skip to content

Latest commit

 

History

History
107 lines (78 loc) · 4.35 KB

07-Staged-deployments.md

File metadata and controls

107 lines (78 loc) · 4.35 KB

🔨 Hands-on: Staged deployments

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

  1. Got 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.

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 contaisn 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 keeword.

You can continue with the README.