Azure DevOps:运行 任务仅当上一个任务 运行

Azure DevOps: Run task only if previous task ran

我希望 PublishTestResults@2 任务 运行 仅当前一个任务 script(运行s 单元测试)实际上 运行。

  1. 即使前一个任务失败了,如何使任务取决于前一个任务?
  2. always()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

更新:我怀疑发布步骤“前端测试结果”是 运行ning 因为前面的 2 个步骤不是 运行 但之前的步骤是成功的:

succeededOrFailed 对于 setp 等价于 in(variables['Agent.JobStatus'], 'Succeeded', 'SucceededWithIssues', 'Failed') 这就是为什么你的 Frontend Test results 运行.

如果你想只在Jest Unit Tests执行时发布你可以使用日志命令设置变量,然后在条件中使用这个变量:

 # 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