azure-devopsyamlazure-pipelinesazure-pipelines-yaml

Way to run bash script before predeployment hook with agentless server in azure devops


I am creating deployment yaml script in azure devops.

I have a requirement to use a manual validation task to confirm such changes. But before that, we have to set variable to one of our flags as conditions to run the said manual validation task.

Here is the code now:

    strategy:
      runOnce:
        preDeploy:        
          pool: server       
          steps:
          - script: |
                if [[ "$(clusterTags)" == *"HOTFIX"* ]]; then
                  echo "##vso[task.setvariable variable=hotfix]true"
                else
                  echo "##vso[task.setvariable variable=hotfix]false"
                fi
            name: SetHotfixVariable

          - task: ManualValidation@0
            timeoutInMinutes: 1440 # task times out in 1 day
            condition: eq(variables.hotfix, true)
            inputs:
              notifyUsers: ''
              instructions: 'Please validate the build configuration and resume'
              onTimeout: 'resume'
        deploy:
           #usual deployment script here

I tried this approach but encountered error as I have a shell script within an agentless job. 'SIT' is a server job, but contains task 'CmdLine' which can only run on agents.

Is it possible to set these variables before the manual validation task? The cluster tag came from Pull Request Tags from azure repository.

Thank you very mych


Solution

  • You don't need to create an output variable named hotfix.

    Instead, check the value of pipeline variable clusterTags directly in the job condition.

    Example

    The following sample pipeline uses the function contains in a condition to check if variable clusterTags contains string HOTFIX:

    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      - name: clusterTags
        value: 'fooHOTFIXbar' # <---------- change value to test the condition
    
    jobs:
      - job: waitForValidation
        displayName: Wait for external validation
        pool: server
        timeoutInMinutes: 4320 # job times out in 3 days
        condition: contains(variables['clusterTags'], 'HOTFIX') # <-------- set job condition here
        steps:
        - task: ManualValidation@0
          timeoutInMinutes: 1440 # task times out in 1 day
          inputs:
            notifyUsers: ''
            instructions: 'Please validate the build configuration and resume'
            onTimeout: 'resume'
    
      - job: doSomething
        displayName: Do something
        dependsOn: waitForValidation
        steps:
        - checkout: none
        - script: echo "Hello, world!"