Add Variables to Pipelines using the Azure DevOps REST API

56 views Asked by At

I am currently using the pipeline REST endpoint documented here to create my pipelines and associate them with a YAML file, but there is no setting for adding variables in the docs.

For reference the setting I am looking for is the one you add here in the portal UI:

enter image description here

I'm not looking to set up variable groups which can be done using distributed task endpoint here nor am I looking to add them to pipeline runs or build definitions which also have endpoints available documented here and here.

I just want to add them to the pipeline itself so that triggers tied to branch check-ins or manual runs have them available.

Unless I am not using them properly none of these endpoints mentioned are able to do the trick for me and I don't see any other options in the documents.

2

There are 2 answers

1
Kevin Lu-MSFT On BEST ANSWER

To use the Rest API: Pipelines - Create to create the YAML Pipeline and set the Variable in Pipeline UI, you can refer to the following Request Body:

Rest API:

POST https://dev.azure.com/{organization}/{project}/_apis/pipelines?api-version=7.2-preview.1

Request Body:

You can define the Pipeline variables in configuration field.

{
    "configuration": {
        "variables": {
            "variable1": {
                "value": "test1"
            },
            "variable2": {
                "value": "test2"
            }
        },
        "path": "azure-pipelines.yml",
        "repository": {
            "id": "{RepoID}",
            "type": "azureReposGit"
        },
        "type": "yaml"
    },
    "name": "{PipelineName}",
    "folder": "\\"
}

Result:

enter image description here

0
wenbo On

I do think Run Pipeline rest api helps (link). $RunPipelineVariables is acting the same as the variables you pass when run the pipeline in UI.

below code I have used for a long time. It works well.

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [string]$OrganizationName,
    [Parameter(Mandatory)]
    [string]$ProjectName,
    [Parameter(Mandatory)]
    [string]$PipelineId,
    [Parameter(Mandatory)]
    [string]$BranchName,
    [Parameter(Mandatory)]
    [hashtable]$RunPipelineVariables,
    [Parameter(Mandatory)]
    [hashtable]$Headers
)
$uri = "https://dev.azure.com/{0}/{1}/_apis/pipelines/{2}/runs?api-version=7.0" -f $organizationName, $ProjectName, $PipelineId

$body = @{
    resources = @{
        repositories = @{
            self = @{
                refName = "refs/heads/" + $BranchName
            }
        }
    }
    variables = $RunPipelineVariables
} | ConvertTo-Json -Depth 10

Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType 'application/json' -Headers $headers -Body $body

a simple code to get header help you to test.

[CmdletBinding()]
param (
    [Parameter()]
    [string]$UserName,
    [string]$PersonalAccessToken
)

$basicAuth = ("{0}:{1}" -f $UserName, $PersonalAccessToken)
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization = ("Basic {0}" -f $basicAuth) }
return $headers