Reputation: 91
How can we create a Azure Pipeline(yaml approach) using Azure Devops rest apis. Basically I am trying to create a new pipeline programmatically and not through Azure Devops portal I referred to this link below: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/create?view=azure-devops-rest-6.0 but this does not provide the exact json body format required to create and configure a new pipline pointing to code repo. Kindly help me here
Upvotes: 0
Views: 2261
Reputation: 30313
You can first use the Pipelines - Get rest api to check the definition json of the pipeline, and change the fields accordingly.
You can define the request body json as below, when calling Pipelines - Create
rest api:
$body = @{
configuration=@{
variables=@{
customVariable=@{
value="value"
}
};
path="azure-pipelines.yml";
repository=@{
id= "repository-id";
name="repository-name"
type= "azureReposGit"
};
type= "yaml"
};
name= "pipeline-name";
folder= "\\"
}
The variables
field define the pipeline Variables in the UI Page:
The path
field points to pipeline yaml file in the code repo.
The repository
field defines the code repo this pipeline targets to.
The folder
field defines which folder the pipeline is placed:
If you use Build Definitions - Create rest api to create the yaml pipeline. You can check below request body json example:
$body='{ "variables": {
"customVariable": {
"value": "customValue",
"allowOverride": true
}
},
"process": {
"yamlFilename": "azure-pipelines.yml",
"type": 2
},
"repository": {
"id": "repo-id",
"type": "TfsGit",
"name": "repo-Nanme",
"defaultBranch": "refs/heads/master",
"clean": null,
"checkoutSubmodules": false
},
"name": "pipeline-name",
"path": "\\",
"type": "build",
"queueStatus": "enabled",
"project": {
"id": "project-id",
"name": "project-name"
}
}'
Update:
If you code repo is Githbub. You will have to create a github service connection in your azure devops project. And then pass the connection id in your api resquest body.
$body = @{
configuration=@{
variables=@{
customVariable=@{
value="value"
}
};
path="azure-pipelines.yml";
repository=@{
FullName="githubAccount/repoName";
type= "gitHub";
Connection= @{
id= "github service connection id"
}
};
type= "yaml"
};
name= "pipeline-name";
folder= "\\"
}
You can get the service connection id in the address bar. See below:
Upvotes: 2