Saturday, 3 July 2021

Manual Approval in GitHub Actions

Before deploying to an environment with GitHub actions, you may need to implement a manual approval, especially in production environment. If it is a infrastructure deployment pipeline you may need even implement approvals for development environments to avoid unstable environments due to infrastructure failures. You may want to check what happens if you deploy Bicep or Terraform script, and then approve the execution to the target environment. Let's try to understand how to implement a manual approval step in GitHub actions

As the first step in your repository settings you have to define an environment. Click on New environment.


Provide a name for the environment, we have to use the same environment name in the pipeline later to enable approval. Click on Configure environment.

You need to add reviewers for the environment as shown below. Save the protection rules.

Once we have setup the environment we can start setting up a GitHub action pipeline. The first job is setup as pre dev environment. An example would be terraform plan step, or Bicep --what-if to check what happens.
nameManual Approval Demo

on:
  workflow_dispatch:
    
jobs:
  Pre-Dev-Steps:
    runs-onubuntu-latest
    
    steps:
      - name'Run some predev steps'
        shellpwsh
        run|
          Write-Host 'Done some pre dev env deploy work'

The next job dev needs first job as a prerequisite making it wait for pre dev to finish. To enable waiting note the environment name is set as exactly we have specified in repo settings environments.
  Dev-Env:
      runs-onubuntu-latest
      needsPre-Dev-Steps
      environment:
        nameDevEnv
      steps:
        - name'Run some dev env steps'
          shellpwsh
          run|
            Write-Host 'Done some dev env deploy work'

Once we execute this pipeline pre dev steps will run and wait for approval for the dev env. You can click on review deployments as you can see environment is waiting for review with a clock icon.

Once you click review deployment there will be a popup allowing you to approve or reject the deployment with an optional comment.

Once approved the deployment will complete for the dev environment. You can see the approval and the comment.
The entire pipeline code is below.
nameManual Approval Demo

on:
  workflow_dispatch:
    
jobs:
  Pre-Dev-Steps:
    runs-onubuntu-latest
    
    steps:
      - name'Run some predev steps'
        shellpwsh
        run|
          Write-Host 'Done some pre dev env deploy work'

  Dev-Env:
      runs-onubuntu-latest
      needsPre-Dev-Steps
      environment:
        nameDevEnv
      steps:
        - name'Run some dev env steps'
          shellpwsh
          run|
            Write-Host 'Done some dev env deploy work'


No comments:

Popular Posts