5

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"
        }
    }
}
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Devops-Learner
  • 451
  • 1
  • 4
  • 16

4 Answers4

4

You can use az repos list with a JMESPath query to find the id. For example:

az repos list \
  --organization "{{organization}}" \
  --project "{{project}}" \
  --query "[?name == '{{respository_name}}'].id" \
  --output tsv

Just use that in a bash subshell. For example result=$(cli_command).

Michelle Welcks
  • 3,513
  • 4
  • 21
  • 34
2

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"
AviAtia
  • 21
  • 4
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 06 '22 at 02:31
1

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.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • I have made that change and is able to store this output to a file, but to get this ID to the api. this is where I am struck. Can you guide me with a reference if possible. Thanks in advance – Devops-Learner May 11 '21 at 10:42
  • I'm not understand... can you share your whole code? – Shayki Abramczyk May 11 '21 at 10:56
  • while creating the repo, I am storing the output to repooutput.txt. No want to get only ID of the repo from that file to my script file, Please find the below code – Devops-Learner May 11 '21 at 11:12
  • "Repo_Creation_Status=$(curl --write-out "%{http_code}\n" -X POST \ -u :$PAT "https://dev.azure.com/"${OrganizationName}"/"${projectId}"/_apis/git/repositories?api-version=6.1-preview.1" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -d '{ "name": "'"$RepoName"'", "project": { "id": "'"$projectId"'" } }' --output ***Repooutput.txt*** --silent)" – Devops-Learner May 11 '21 at 11:13
  • 1
    Thank you @shayki for the help, I have answered as how it can be done. Appreciate your help here – Devops-Learner May 11 '21 at 13:39
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

Devops-Learner
  • 451
  • 1
  • 4
  • 16