简体   繁体   中英

How to use a Python script exit code as a condition for the following task in Azure Pipeline?

I have a pipeline exporting Azure boards work items to csv file.

Here is the process:

  1. Download the build artifact(csv file and timestamp.txt) from the last successful pipeline run.

  2. Run a Python script to query work items updated since last successful pipeline run(last export time is in timestamp.txt). If there are updated work items, then update csv file with these work items, update the timestamp.txt and exit program with 0. If no updated work items found, exit program with a non-zero value.

  3. Publish the updated or "un-updated" csv file and timestamp.txt as build artifacts.

  4. Upload the updated csv file to SharePoint site.

What I want to implement:

  1. Whether csv file is updated or not, the pipeline run should end as successful.

  2. I need to define a bool variable that can be set depends on the exit code of Python script(or it can be directly set in the Python script).

  3. Use that bool variable as condition for the upload-to-SharePoint task.

How to create the YAML file to implement this? Thanks.

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'

Inside the python script you can assign a new variable with the value according to your logic (instead of exit code).

For example, if the code did what he should and you want to run the upload-to-SharePoint task, add the following logging command :

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

And in the upload-to-SharePoint task add a custom condition :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM