azureazure-pipelinescicd

Check and Download if an artifact exists on the second attempt upon rerun of jobs in Azure pipelines


I have a multi templated - multi staged Azure pipeline configured where templates when successfully executed publishes an artifact with a unique name. During executions, some templates fail in any of the stage, I have a requirement to rerun only the failed jobs. But on "Rerun failed jobs" on azure, it triggers the subsequent stage completely and runs both successful and failed jobs in the dependent stage

 stages:
  - stage: Stage1
    jobs:
    - template: execution_template1.yml
      parameters:
        value: value1
    - template: execution_template2.yml
      parameters:
        value: value1

  - stage: Stage2
    condition: not(canceled())
    jobs:
    - template: execution_template3.yml
      parameters:
        value: value1
    - template: execution_template4.yml
      parameters:
        value: value1

  - stage: Stage3
    condition: not(canceled())
    jobs:
    - template: execution_template5.yml
      parameters:
        value: value1
    - template: execution_template6.yml
      parameters:
        value: value1

  - stage: Stage4
    condition: not(canceled())
    jobs:
    - template: consolidate.yml
      parameters:
        result: value1

I wish to achieve this, by first checking on rerun that if its a second attempt. And only on the second attempt, I check if the artifact exists, and if it does not exists then only rerun the job. This will help in not triggering the successful jobs again.

Also if the job was successful on the first attempt, instead of skipping it. I want the status on the Azure board to be displayed as Successful instead of Skipped to avoid confusion.


Solution

  • Related to your other question, there is an API endpoint for artifacts.

    Similiar to my previous answer, you could add a job that queries the existence of the artifact and set variables to control dependent job execution.

    $url = (“{0}/{1}/_apis/build/builds/{2}/artifacts?api-version=7.1” -f $org, $prj, $buildId)
    $artifacts = Invoke-RestApi -Uri $url -Method Get -Headers $headers
    $artifact = $artifacts | where { $_.name -eq “artifact” }
    if ($artifact) {
      # set variables to skip execution of dependent jobs
    }