Aditya
Aditya

Reputation: 38

Create a new Azure CI pipeline by invoking existing YAML file in the repository via REST API through Python script

I have an existing azure-pipelines.yml file in my branch. I want to invoke this file via Azure RestAPI and let Azure CI Pipelines create. I need to do it by python code.

something I have tried like this but getting some error related 203. It seems ...... 203 Non-Authoritative Information Return Issue when attempting to perform any action (GET/POST/etc) through the Azure DevOps API. ..Main focus is create pipelines by code. If any existing/working examples, it would be helpful..


import json

api_url = "https://dev.azure.com/DevOps/Ops/_apis/pipelines?api-version=6.0-preview.1"


json_data = {

    "folder": "/",

    "name": "My Pipeline",

    "configuration": {

        "type": "yaml",

        "path": "/Boot/{{ project_name }}/pipelines/azure-pipelines.yaml",

        "repository": {

            "name": "Boot",

            "type": "azureReposGit"

        }

    }

}

headers =  {"Content-Type":"application/json"}

response = requests.post(api_url, data = json.dumps(json_data), headers=headers)

#print(response.json())

print(response.status_code)```

Upvotes: 1

Views: 769

Answers (1)

Bowman Zhu
Bowman Zhu

Reputation: 7146

Write a Python demo for you here:

import requests
import json

def create_pipeline_basedon_yaml(Organization, Project, Repository, Yaml_File, Pipeline_Folder, Pipeline_Name, Personal_Access_Token):
    ##########get repo id##########

    url_repoapi = "https://dev.azure.com/"+Organization+"/"+Project+"/_apis/git/repositories/"+Repository+"?api-version=4.1"

    payload_repoapi={}
    headers_repoapi = {
    'Authorization': 'Basic '+Personal_Access_Token,
    }

    response_repoapi = requests.request("GET", url_repoapi, headers=headers_repoapi, data=payload_repoapi)

    repo_id = response_repoapi.json()['id']

    ##########create pipeline##########

    url_pipelineapi = "https://dev.azure.com/"+Organization+"/"+Project+"/_apis/pipelines?api-version=6.0-preview.1"

    payload_pipelineapi = json.dumps({
    "configuration": {
        "path": Yaml_File,
        "repository": {
        "id": repo_id,
        "type": "azureReposGit"
        },
        "type": "yaml"
    },
    "folder": Pipeline_Folder,
    "name": Pipeline_Name
    })
    headers_pipelineapi = {
    'Authorization': 'Basic '+Personal_Access_Token,
    'Content-Type': 'application/json'}

    requests.request("POST", url_pipelineapi, headers=headers_pipelineapi, data=payload_pipelineapi)


Organization = "xxx"
Project = "xxx"
Repository = "xxx"
Yaml_File = "xxx.yml"
Pipeline_Folder = "test_folder"
Pipeline_Name = "Pipeline_basedon_yaml"
Personal_Access_Token = "xxx"

create_pipeline_basedon_yaml(Organization, Project, Repository, Yaml_File, Pipeline_Folder, Pipeline_Name, Personal_Access_Token)

I can successfully create the pipeline based on the specific yaml file:

enter image description here

Upvotes: 1

Related Questions