Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps: Run task only if previous task ran

I want the PublishTestResults@2 task to run ONLY if the previous task script (which runs unit tests) actually ran.

  • If I use condition: succeededOrFailed() then PublishTestResults@2 runs even if the previous step did not run - I thought this was what condition: always() was for.
  1. How does one make a task contingent on the previous task even if the previous task failed?
  2. What is the difference between always() and succeededOrFailed()?

Reference

    # This step only runs if the previous step was successful - OK
    - script: |
        cd $(System.DefaultWorkingDirectory)/application/src
        yarn test:unit --silent --ci --reporters=jest-junit
      displayName: 'Jest Unit Tests'
      env:
        JEST_JUNIT_OUTPUT_DIR: $(System.DefaultWorkingDirectory)/testresults
        JEST_JUNIT_OUTPUT_NAME: 'jest-junit.xml'

    # This step ALWAYS runs - NO
    #  This step should ONLY run if the previous step RAN (success OR fail)
    - task: PublishTestResults@2
      displayName: 'Frontend Test results'
      condition: succeededOrFailed()
      inputs:
        testResultsFormat: JUnit
        searchFolder: $(System.DefaultWorkingDirectory)/testresults
        testResultsFiles: 'jest-junit.xml'
        testRunTitle: 'Frontend Test Results'
        mergeTestResults: false
        failTaskOnFailedTests: true

UPDATE: I suspect the Publish step "Frontend Test results" is running because the 2 previous steps were NOT run but the one before that was successful:

enter image description here

like image 858
Marc Avatar asked Oct 20 '25 17:10

Marc


1 Answers

succeededOrFailed for a setp is equivalent to in(variables['Agent.JobStatus'], 'Succeeded', 'SucceededWithIssues', 'Failed') and this is why your Frontend Test results ran.

If you want to publish only when Jest Unit Tests was executed you may use logging commands to set variable and then use this variable in condition:

 # This step only runs if the previous step was successful - OK
    - script: |
        echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes" #set variable doThing to Yes
        cd $(System.DefaultWorkingDirectory)/application/src
        yarn test:unit --silent --ci --reporters=jest-junit
      displayName: 'Jest Unit Tests'
      name: JestUnitTests
      env:
        JEST_JUNIT_OUTPUT_DIR: $(System.DefaultWorkingDirectory)/testresults
        JEST_JUNIT_OUTPUT_NAME: 'jest-junit.xml'

    # This step ALWAYS runs - NO
    #  This step should ONLY run if the previous step RAN (success OR fail)
    - task: PublishTestResults@2
      displayName: 'Frontend Test results'
      condition: and(succeededOrFailed(), eq(variables['JestUnitTests.doThing'], 'Yes'))
      inputs:
        testResultsFormat: JUnit
        searchFolder: $(System.DefaultWorkingDirectory)/testresults
        testResultsFiles: 'jest-junit.xml'
        testRunTitle: 'Frontend Test Results'
        mergeTestResults: false
        failTaskOnFailedTests: true
like image 196
Krzysztof Madej Avatar answered Oct 23 '25 09:10

Krzysztof Madej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!