Azure DevOps:仅在上一个任务运行后运行任务

3
我希望PublishTestResults@2任务仅在前一个任务script(运行单元测试)实际运行时才运行。
  • 如果使用condition: succeededOrFailed(),则即使上一步未运行,PublishTestResults@2也会运行 - 我认为这就是condition: always()的作用。
  1. 如何使一个任务在前一个任务失败的情况下也依赖于前一个任务?
  2. always()succeededOrFailed()之间有什么区别?

参考

    # 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

更新:我怀疑“前端测试结果”发布步骤正在运行,因为前面的两个步骤未运行,但是之前的一个步骤成功了:

enter image description here

1个回答

6

succeededOrFailed对于一个步骤来说等同于in(variables['Agent.JobStatus'], 'Succeeded', 'SucceededWithIssues', 'Failed'),这就是为什么你的前端测试结果被执行的原因。

如果你只想在执行Jest单元测试时发布,你可以使用日志命令来设置变量,然后在条件中使用该变量:

 # 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

好的,那么always()和succeededOrFailed()之间没有区别吗? - Marc
@Marc,always()即使运行被取消也会执行,而succeededOrFailed()将在管道失败或成功时运行,但如果它被取消,则不会运行。 - Facundo Santiago

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接