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

Mar*_*arc 3 azure-devops azure-pipelines azure-pipelines-yaml

我希望该PublishTestResults@2任务仅在前一个任务script(运行单元测试)实际运行时才运行。

  • 如果我使用condition: succeededOrFailed()thenPublishTestResults@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
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

Krz*_*tof 6

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
Run Code Online (Sandbox Code Playgroud)