python - 如何使用 Python 脚本退出代码作为 Azure Pipeline 中以下任务的条件?

标签 python azure azure-devops azure-pipelines azure-pipelines-yaml

我有一个管道将 Azure 板工作项导出到 csv 文件。

流程如下:

  1. 从上次成功的管道运行中下载构建工件(csv 文件和时间戳.txt)。

  2. 运行 Python 脚本来查询自上次成功的管道运行以来更新的工作项(上次导出时间位于 timestamp.txt 中)。如果有更新的工作项,则使用这些工作项更新 csv 文件,更新 timestamp.txt 并使用 0 退出程序。如果未找到更新的工作项,则使用非零值退出程序。

  3. 将更新或“未更新”的 csv 文件和 timestamp.txt 发布为构建工件。

  4. 将更新的 csv 文件上传到 SharePoint 网站。

我想要实现的内容:

  1. 无论 csv 文件是否更新,管道运行都应成功结束。

  2. 我需要定义一个 bool 变量,该变量可以根据 Python 脚本的退出代码进行设置(或者可以直接在 Python 脚本中设置)。

  3. 使用该 bool 变量作为上传到 SharePoint 任务的条件。

如何创建 YAML 文件来实现此目的?谢谢。

YAML:

# - master
trigger: none

schedules:
- cron: "0 */12 * * 1,2,3,4,5"
  displayName: Hourly Azure All WorkItems Export
  branches:
    include:
    - master
  always: true
   
pool: 'Windows-VS2017DEV'


steps:

# - task: InstallPython@1
#   inputs:
#     version: 'python'

- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.x'
    addToPath: true
    architecture: 'x64'

- task: DownloadBuildArtifacts@0
  inputs:
    buildType: 'specific'
    project: 'XXX'
    pipeline: 'xxx'
    buildVersionToDownload: 'latest'
    downloadType: 'single'
    artifactName: 'Azure_Reporting'
    downloadPath: '$(System.DefaultWorkingDirectory)'


- task: PythonScript@0
  inputs:
    scriptSource: 'filePath'
    scriptPath: 'Python/azure_workitems.py'
    arguments: '$(azure_workitems_option)'   

- task: CopyFiles@2
  inputs:
    contents: |
      *csv
      timestamp.txt
    targetFolder: $(Build.ArtifactStagingDirectory)
    
- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: $(Build.ArtifactStagingDirectory)
    artifactName: Azure_Reporting

- task: UploadFilesToSPDocLib@1
  inputs:
    spUrl: 'https://XXXX.sharepoint.com/sites/XXXX'
    targetFolder: 'Shared%20Documents/Status%20dashboards/Azure_Reporting'
    login: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c59d9d9d9d9d859d9d9d9d9d9deba6aaa8" rel="noreferrer noopener nofollow">[email protected]</a>'
    password: $(XXX.Credential)
    filesToUpload: ' $(Build.ArtifactStagingDirectory)/azure_workitems.csv'

最佳答案

在 python 脚本中,您可以根据您的逻辑分配一个新变量的值(而不是退出代码)。

例如,如果代码做了他应该做的事情,并且您想要运行上传到 SharePoint 任务,请添加以下 logging command :

print('##vso[task.setvariable variable=uploadSP]true')

并在上传到 SharePoint 任务中添加 a custom condition :

and(succeeded(), eq(variables['uploadSP'], 'true'))

关于python - 如何使用 Python 脚本退出代码作为 Azure Pipeline 中以下任务的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67784275/

相关文章:

python - 使用 BLAS 实现更快的 python 内积

python - 如何检查列表是否只包含某个项目

python - Gtk/python 和可移植性

azure - 如何连接到 Azure Synapse?

azure - 我们可以从Azure无服务器数据库存储过程调用REST api吗

azure-devops - Azure DevOps 服务 Hook 和服务连接之间有什么区别

c# - 在 Azure Function 中访问 VSTS 构建服务器版本

python - 向用户询问优先级列表,然后根据该列表对列表进行排序

azure - Cosmos DB 警报

azure - 如何通过 VSTS 将 Azure WebJob 和应用程序部署到同一应用程序服务?