如何使用 Python 脚本退出代码作为 Azure Pipeline 中以下任务的条件?
How to use a Python script exit code as a condition for the following task in Azure Pipeline?
我有一个将 Azure 看板工作项导出到 csv 文件的管道。
过程如下:
从上次成功的管道 运行.
下载构建工件(csv 文件和 timestamp.txt)
运行 一个 Python 脚本,用于查询自上次成功管道 运行 以来更新的工作项(上次导出时间在 timestamp.txt 中)。如果有更新的工作项,则使用这些工作项更新 csv 文件,更新 timestamp.txt 并使用 0 退出程序。如果未找到更新的工作项,则使用非零值退出程序。
将更新的或“未更新的”csv 文件和timestamp.txt发布为构建工件。
将更新的 csv 文件上传到 SharePoint 网站。
我要实现的:
无论是否更新 csv 文件,管道 运行 都应该成功结束。
我需要定义一个bool变量,可以根据Python脚本的退出代码设置(或者直接在Python脚本中设置)。
使用该 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: 'XXXXX@XXXXXX.com'
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'))
我有一个将 Azure 看板工作项导出到 csv 文件的管道。
过程如下:
从上次成功的管道 运行.
下载构建工件(csv 文件和 timestamp.txt)运行 一个 Python 脚本,用于查询自上次成功管道 运行 以来更新的工作项(上次导出时间在 timestamp.txt 中)。如果有更新的工作项,则使用这些工作项更新 csv 文件,更新 timestamp.txt 并使用 0 退出程序。如果未找到更新的工作项,则使用非零值退出程序。
将更新的或“未更新的”csv 文件和timestamp.txt发布为构建工件。
将更新的 csv 文件上传到 SharePoint 网站。
我要实现的:
无论是否更新 csv 文件,管道 运行 都应该成功结束。
我需要定义一个bool变量,可以根据Python脚本的退出代码设置(或者直接在Python脚本中设置)。
使用该 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: 'XXXXX@XXXXXX.com'
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'))