如何在Azure DevOps中运行条件步骤?

huangapple go评论60阅读模式
英文:

how to run a conditional step in Azure devOps?

问题

我正在在 Microsoft DevOps Forge 中构建我的流水线。我想要使测试流水线将覆盖率导出为一个工件。我在 Python 版本的矩阵上运行我的流水线。我想只为 Python 3.8 运行导出。我该如何指定步骤为条件性的?

这是我的 test.yaml 文件:

英文:

I'm building my pipelines in the Microsoft devOps forge. I would like to make the test pipeline to export the coverage as an artifact. I run my pipeline on a matrix of python version. I would like to run the exportation only for Python 3.8. How can I specify that a step is conditional ?

Here is my test.yaml file:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: windows-latest
strategy:
  matrix:
    Python37:
      python.version: "3.7"
    Python38:
      python.version: "3.8"
    Python39:
      python.version: "3.9"
    Python310:
      python.version: "3.10"
    Python311:
      python.version: "3.11"

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: "$(python.version)"
    displayName: "Use Python $(python.version)"
  - script: python -m pip install nox
    displayName: "Install dependencies"
  - script: nox -s test
    displayName: "Test with pytest"
  - task: PublishBuildArtifacts@1 # this one should be executed only for Python 3.8
    displayName: "Publish coverage"
    inputs:
      pathToPublish: "./htmlcov/"
      artifactName: "coverage"

答案1

得分: 1

为了实现这一目标,建议利用 ADO 表达式来仅在变量版本为 3.8 时加载任务。这与条件不同,因为条件将始终加载任务,并在条件不满足时跳过它。

  • ${{ if eq(variable.python.version, '3.8')}} :
    • task: PublishBuildArtifacts@1
      displayName: "发布覆盖率"
      inputs:
      pathToPublish: "./htmlcov/"
      artifactName: "coverage"

这里有一篇博客文章详细介绍了两者之间的区别

英文:

To accomplish this would recommend leveraging an ADO expression to load the task only if the variable version is 3.8. This is different then a condition as the condition will always load the task and skip it if the condition is not met.

- ${{ if eq(variable.python.version, '3.8')}} :     
  - task: PublishBuildArtifacts@1 
    displayName: "Publish coverage"
    inputs:
       pathToPublish: "./htmlcov/"
       artifactName: "coverage"

Here is a blog post going into the details between the two.

答案2

得分: -2

GitHub操作中的“if”语句等效于“condition”,它可以用来添加作业、任务或阶段的执行条件。

在这个示例中,要仅在版本3.8上发布构建工件,请使用以下语句设置条件:

- task: PublishBuildArtifacts@1 
    condition: eq(variable["python.version"], "3.8")
    displayName: "Publish coverage"
    inputs:
      pathToPublish: "./htmlcov/"
      artifactName: "coverage"

更多信息可以在Azure条件文档中找到。

请注意,变量使用$[]语法在运行时进行解释。有关变量语法的更多信息可以在这里找到。

英文:

The equivalent of the GitHub action "if" statement is "condition". it can be placed to add execution conditions on jobs, tasks or stages.

in this example to publish the artefact only for version 3.8 use the condition with the following statement:

- task: PublishBuildArtifacts@1 
    condition: eq(variable["python.version"], "3.8")
    displayName: "Publish coverage"
    inputs:
      pathToPublish: "./htmlcov/"
      artifactName: "coverage"

more information can be found in Azure condition documentation.

Be aware that the variable is called using the $[]syntax to be interpreted at run-time. more information about variable syntax can be found here

huangapple
  • 本文由 发表于 2023年7月7日 02:59:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定