Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a manual intervention step in Azure Pipelines yaml

How do you add a manual intervention step into a multi-stage Azure Devops YAML pipeline?

In jenkins you can do some thing like:

stage ('approve-prod') {
    steps {
        input "Approve deployment to production?"
    }
}

I am looking for the equivalent in Azure Devops YAML.

Note: this is for the newly released multi-stage Azure Devops pipelines, not the old style release pipelines. Related announcement here https://devblogs.microsoft.com/devops/whats-new-with-azure-pipelines/

like image 604
cfbd Avatar asked May 16 '19 21:05

cfbd


People also ask

How do you add stages in Azure DevOps pipeline?

In Azure DevOps Server 2019, pools can only be specified at job level. This version of TFS doesn't support YAML pipelines. To add a stage to your release pipeline, select the release pipeline in Releases page, select the action to Edit it, and then select the Pipeline tab.

How do I edit YAML file in Azure DevOps pipeline?

Edit a YAML pipeline Sign in to your organization ( https://dev.azure.com/{yourorganization} ). Select your project, choose Pipelines > Pipelines, and then select the pipeline you want to edit. Choose Edit. Make edits to your pipeline using Intellisense keyboard shortcuts and the task assistant for guidance.


4 Answers

Microsoft have now made available a brand new official Manual Validation task that allows for manual intervention to be added into a YAML pipeline.

Quick example of how to use this task is as follows:

  jobs:  
  - job: waitForValidation
    displayName: Wait for external validation  
    pool: server    
    timeoutInMinutes: 4320 # job times out in 3 days
    steps:   
    - task: ManualValidation@0
      timeoutInMinutes: 1440 # task times out in 1 day
      inputs:
        notifyUsers: |
          [email protected]
          [email protected]
        instructions: 'Please validate the build configuration and resume'
        onTimeout: 'resume'

Some key constraints to be aware of:

  • This task is only supported in YAML pipelines
  • Can be used only in an agentless job of a YAML pipeline.
like image 194
user14786410 Avatar answered Oct 05 '22 05:10

user14786410


This doesn't appear to be available yet, but there is a GitHub Issue tracking this: https://github.com/MicrosoftDocs/vsts-docs/issues/4241

From the issue:

So what I heard from the product team is that this "approval per stage" policy isn't available yet but is on their backlog.

There is also a Roadmap work item tracking it: https://dev.azure.com/mseng/AzureDevOpsRoadmap/_workitems/edit/1510336/

like image 34
Alex Herring Avatar answered Oct 05 '22 05:10

Alex Herring


Azure DevOps/Pipelines now has a feature called Environments which supports approvals. https://docs.microsoft.com/en-us/azure/devops/pipelines/process/environments?view=azure-devops#approvals

We are using them as a workaround. Basically we have specified two environments ApprovalNotRequired and ApprovalRequired in Azure DevOps. On the latter we have specified who can approve deployments. Then in the pipeline we reference the environment like this.

- stage: 'Approval not required'
  jobs:
  - deployment: 'MyDeployment'
    displayName: MyDeployment
    environment: 'ApprovalNotRequired'
    strategy:
      runOnce:
        deploy:
          # whatever

- stage: 'Approval required'
  jobs:
  - deployment: 'MyDeployment2'
    displayName: MyDeployment2
    environment: 'ApprovalRequired'
    strategy:
      runOnce:
        deploy:
          # whatever

The first stage will run without interference and the second will pause until it's approved.

like image 24
Alert101 Avatar answered Oct 05 '22 03:10

Alert101


Because there's a long time since Microsoft is ignoring this, and because this is a critical missing functionality , I will add an workaround here (for the moment, it's working only to ignore the entire step for all machines in case of a multi stage YAML but I think this can be solved also, but I am not looking into it for the moment).

Unfortunately, there is a task that needs to be added before each task. This can be solved also by iterative insertion (https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops).

Shortly, in order to be able to ignore a specific task:

  • T1 is checking the build run for "IgnoreStep"tag. If found, it will set IgnoreStep variable to true and remove the tag
  • T2 is running only if previous IgnoreStep is on false When something is failing and I want to ignore the step, I will add "IgnoreStep" tag for the run and retry.

For adding tags, I am using the API because there is no task to do it yet. For request details, F21 in Chrome and check what it sent to server after you will add a tag, and export the request to power shell.

Below you have the YAML:

trigger: none

jobs:
  - deployment: Dev
    environment: 
        name: Dev
        resourceType: virtualMachine
        tags: online
    strategy:                  
          runOnce:
            deploy:
              steps:
              - task: PowerShell@2
                displayName: CheckIfWeShouldIgnoreStep
                name: CheckIfWeShouldIgnoreStep
                inputs:
                  targetType: 'inline'
                  script: |
                    $user = "user"
                    $pass= "pass"
                    $secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
                    $credential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)

                    $response = Invoke-RestMethod -Uri "https://server/tfs/collection/projectId/_apis/build/builds/$(Build.BuildId)/tags" `
                      -Method "GET" `
                      -Headers @{
                        "accept"="application/json;api-version=6.0;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true"
                      } `
                      -ContentType "application/json" `
                      -Credential $credential -UseBasicParsing

                      Write-Host "##vso[task.setvariable variable=IgnoreStep]false"

                      Write-Host "Tags: $response"
                      foreach($tag in $response)
                      {
                          if($tag -eq "IgnoreStep")
                          {
                            Write-Host "##vso[task.setvariable variable=IgnoreStep]true"

                            
                            Invoke-RestMethod -Uri "https://server/tfs/collection/projectId/_apis/build/builds/$(Build.BuildId)/tags/IgnoreStep" `
                                -Method "DELETE" `
                                -Headers @{
                                  "accept"="application/json;api-version=6.0;excludeUrls=true;enumsAsNumbers=true;msDateFormat=true;noArrayWrap=true"
                                }`
                            -Credential $credential -UseBasicParsing
                          }
                      }
              - task: PowerShell@2
                displayName: Throw Error
                condition: eq (variables.IgnoreStep, false)
                inputs:
                  targetType: 'inline'
                  script: |   
                    throw "Error"
like image 34
Silviu Luca Avatar answered Oct 05 '22 05:10

Silviu Luca