Search code examples
azure-pipelinesazure-cliazure-pipelines-yaml

Azure DevOps YAML Pipelines programmatically change settings


Within Azure DevOps, we have a standardized process to create new a repo and associated YAML pipeline. We have a YAML pipeline that clones and renames a template repo, then uses az pipelines to create a new YAML pipeline associated with the new repo. This process works great so far.

After the new pipeline is created, someone (me) has to remember to go into the pipeline and edit some of the settings, specifically we want to turn on Work Item linking, set the Tag Sources option to Always, and set the default agent pool.

The deployment pipelines that are created all point back to the same Stage and Job template files that ultimately contain all the actual code/logic for pushing changes out. The Job template is defined as a Deployment job.

I have looked at the documentation for az pipelines create but there does not seem to be any way to manage settings through it.

For the default agent pool, we set the vmImage value for the pool in the Stage template file, which according to the documentation that should just use the default. However, when I go into the settings from the Triggers menu option, under the YAML tab the Default agent pool for YAML is blank and requires setting.

I also tried to see if I could set up a template pipeline and programmatically copy it and update it, but that does not seem to be possible either. I did find some options on adding a step to the deployment pipeline to programmatically handle tagging of the repos, which I am looking at as an option instead of the setting, but would love to set the Work Item Linking option at a minimum. I realize there Marketplace extensions that might handle this, I was hoping to be able to do it either in the YAML template files or during the creation process.


Solution

  • Based on your description, you need to update the YAML Pipeline definition: Set tag to Always, change Agent Pool to default and set Work Item Linking option.

    There is no out-of-box Azure DevOps CLI to achieve it.

    To meet your requirement, you need to use the Rest API: Definitions - Update

    Rest API URL:

    PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=7.1
    

    Request Body:

    {
      "options": [
        {
          "enabled": true,
          "definition": {
            "id": "5d58cc01-7c75-450c-be18-a388ddb129ec"
          },
          "inputs": {
            "branchFilters": "[\"+refs/heads/{BranchName}\"]"
          }
        }
      
      ],
    
      "tags": [],
      "process": {
        "yamlFilename": "azure-pipelines.yml",
        "type": 2
      },
      "repository": {
        "properties": {
          "labelSources": "46",
          "labelSourcesFormat": "$(build.buildNumber)"
        },
        "id": "{RepoID}",
        "type": "TfsGit"
      },
      "quality": "definition",
      
      "queue": {
     
        "id": {PoolID},
        "name": "Default"
    
      },
      "id": {PiplineID},
      "name": "{PipelineName}",
      "revision": {RevisionID}
    
    }
    

    In the Request Body, there are the following key points:

    {
      "enabled": true,
      "definition": {
        "id": "5d58cc01-7c75-450c-be18-a388ddb129ec"
      },
      "inputs": {
        "branchFilters": "[\"+refs/heads/{BranchName}\"]"
      }
    }
    

    This Part is used to set the Work item Linking Option, you can set the link branch name based on requirement.

     "queue": {
    
    "id": {PoolID},
    "name": "Default"
    
      },
    

    This Part is used to set the Agent Pool to default. You can get the Pool ID in Project Settings -> Agent Pools -> Default Pool URL(queueID).

        "properties": {
          "labelSources": "46",
          "labelSourcesFormat": "$(build.buildNumber)"
        },
    

    This Part is used to set the tag to Always. The value 46 means always and you can set the tag format in the field: labelSourcesFormat.

    "revision": {revisionID} You need to input the valid revision. This is very important.

    To get the correct revision, you need to Navigate to Azure Pipelines -> Target Build Definition -> History.

    enter image description here

    You need to count how many Update records. The correct revision is the total number + 1.

    For example: In my screenshot, the correct revision is 10 (9+1 =10).