'Azure Devops Get a repositoryId by using repository name
I am writing a BASH script, where its creating an azure repo and then pushing code. post this step it will go-ahead and create an azure pipeline via azure-pipeline.yaml
file present in the Azure repo.
At this step we need to pass the repository ID in-order to create the pipeline, but issue here is I can't keep it as a user input as it will be getting created within script itself, now I am struck with this.
Is there any way that we can get the repo id from the newly created repo directly within the script?
https://dev.azure.com/{{organization}}/{{project}}/_apis/pipelines?api-version=6.0-preview.1
{
"folder": "Folder-Name",
"name": "Pipeline-Name",
"configuration": {
"type": "yaml",
"path": "azure-pipelines.yml",
"repository": {
"id": "Repo-ID",
"name": "Repo-Name",
"type": "azureReposGit"
}
}
}
Solution 1:[1]
This can be done by taking the output to another file as a variable (As suggested by @Shayki Abramczyk), then with the help of below command we can call the ID variable in the script file
$ jq -r '.id' Repooutput.txt
dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf
Solution 2:[2]
Yes, when you create the repository (with Repositories - Create api) you get in the response the repo id:
{
"id": "5febef5a-833d-4e14-b9c0-14cb638f91e6",
"name": "AnotherRepository",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/5febef5a-833d-4e14-b9c0-14cb638f91e6",
"project": {
"id": "6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"name": "Fabrikam-Fiber-Git",
"url": "https://dev.azure.com/fabrikam/_apis/projects/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"state": "wellFormed"
},
"remoteUrl": "https://dev.azure.com/fabrikam/Fabrikam-Fiber-Git/_git/AnotherRepository"
}
So just save it in a variable and use in the create pipeline api.
Solution 3:[3]
In case you want to get it using Python:
import azure.devops.connection as connection
import msrest.authentication as basic_authentication
PAT = "***" # personal access token
AZURE_DEVOPS_URI = ""
PROJECT = ""
def get_repository_id_by_repo_name(repo_name):
credentials = basic_authentication.BasicAuthentication("", PAT)
connection_to_clients = connection.Connection(base_url=AZURE_DEVOPS_URI , creds=credentials)
clients = connection_to_clients.clients_v5_1
git_client = clients.get_git_client()
repositories = git_client.get_repositories(project=PROJECT)
for repo in repositories:
repository_name = str(repo.name)
if repository_name == repo_name:
return repo.id
return "Repository not found"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Devops-Learner |
Solution 2 | Shayki Abramczyk |
Solution 3 |