Search code examples
azure-powershellazure-pipelines-yaml

In Azure pipeline, how can Powershell make a task fail?


I am working on Azure Pipelines on a Windows Self hosted Agent. I need my pipeline to run a PowerShell script and if the script is successful, the next stage do the deployment, else the task fails, we have to fix something and resume the task

I'll describe what the pipeline does since the start as it might help understand.

First, the pipeline calls a template with parameters:

stages:
  - template: release.yml@templates
    parameters:
      dbConnectionString: ''

The template release.yml@templates is below:

parameters:
- name: 'dbConnectionString'
  default: ''
  type: string

There is the first stage that simply builds the project, works fine

stages:
  - stage: Build
      - job: Build_Project
        steps:
          - checkout: none
          - template: build.yml          

The second stage depends on the result of the previous one. For some cases of the template, there is no DB to check so I run the job only a parameter is provided. Then, I want to run the CompareFile script only if the DBCheck was successful or if there was no parameter.

  - stage: Deploy
    dependsOn: 
    - Build
    condition: eq( dependencies.Build.result, 'Succeeded' )
    jobs:
    - job: CheckDb
      condition: ne('${{ parameters.dbConnectionString }}', '')
      steps:
        - checkout: none
        - template: validate-db.yml@templates
          parameters:
            ConnectionString: '${{ parameters.dbConnectionString }}'

    - job: CompareFiles
      dependsOn: CheckDb
      condition: or( eq( dependencies.CheckDb.result, 'Succeeded' ), eq('${{ parameters.dbConnectionString }}', '') )
      steps:
        - checkout: none
        - task: PowerShell@2
          name: compareFiles
          inputs:
            targetType: filePath
            filePath: 'compareFile.ps1'

    - deployment: Deploy2
      dependsOn: CompareFiles
      environment: 'Env ST'
      strategy:
           runOnce:
             deploy:
               steps:
                - task: PowerShell@2
                  inputs:
                    targetType: filePath
                    filePath: 'File.ps1'

The next job is to compare file the files, the CompareFile.ps1 file is below. The file compareFileContent.ps1 tries to make the task fail or succeed but I don't know PowerShell enough. I have found somewhere that $host.SetShouldExit(10) could make the task fail so I tried 10 for failure and 0 for success, I also tried exit values but for now, testing with $equal = $true the stage "Deploy2" is skipped so I am blocked

[CmdletBinding()]
param ()

    ***

    $equal = $true

    if($equal) {
        # make pipeline to succeed
        $host.SetShouldExit(0)
    exit 0
    }
    else {
        # make pipeline to fail
        $host.SetShouldExit(10)
    exit 10
    }

Would you have ideas why the deployment job is skipped?


Solution

  • I was able to use these exit values to make the pipeline task succeed or failed:

    if($equal) {
        # make pipeline to succeed
        exit 0
    }
    else {
        exit 1
    }
    

    I used the PowerShell script in its own stage instead of in a job and it worked, when the task fails, I can do required manual actions and run the task again.

    Cheers, Claude