Azure DevOps Pipelines - run YAML pipeline only when previous run was successful

0 votes
I created a build pipeline in Azure DevOps that is defined in YAML.  It has a cron-trigger that is executed at scheduled night times. Sometimes the pipeline run fails and needs to be executed manually to work again.
I wanted to trigger not to fire again if the previous execution was not yet completed. How can I do this?
Apr 19, 2022 in Other DevOps Questions by Kichu
• 19,040 points
1,007 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Just integrate this task into your pipeline and you will be good to go.

  - job: checkpipelinestatejob
    displayName: 'Check Pipeline state'
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Schedule')) # only check this for scheduled runs (by cron-trigger)
    steps:
    - task: PowerShell@2
      name: 'checkPreviousPipelineRun'
      displayName: 'Check previous pipeline run status'
      env:
          AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
      inputs:
        targetType: inline
        script: |

          # Login to Azure DevOps Extension is happening automatically using ENV AZURE_DEVOPS_EXT_PAT which is set above
          # Set default Azure DevOps organization and project
          az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) --use-git-aliases true

          # List last pipeline runs (just for easier debugging)
          echo "*** Last five runs of this pipeline:"
          az pipelines runs list --pipeline-ids $(System.DefinitionId) --status all --top 5 --output table

          # Fetch result of the previous scheduled run (--top 1)
          $previousRunResult = $(az pipelines runs list --pipeline-ids $(System.DefinitionId) --status completed --reason schedule --top 1 --query "[].result" -o tsv)

          echo "*** Previous scheduled run of this pipeline was completed with status: $previousRunResult"

          if($previousRunResult -eq "failed")
          {
            throw "*** Previous scheduled pipeline run did not complete successfully. Not running another pipeline. Please cleanup manually and retry."
          }

I hope this helps.

answered Apr 20, 2022 by narikkadan
• 63,600 points

edited 5 days ago

Related Questions In Other DevOps Questions

0 votes
0 answers
0 votes
1 answer
0 votes
0 answers

OWASP Zed Attack Proxy Scan in DevOps pipeline

I want to do the "Authenticated Scan" ...READ MORE

Mar 15, 2022 in DevOps Tools by Kichu
• 19,040 points
934 views
0 votes
1 answer
0 votes
0 answers

Multiple YAML build pipelines in Azure DevOps

Using the new YAML way I want ...READ MORE

Mar 15, 2022 in DevOps Tools by Kichu
• 19,040 points
1,266 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP