azure-devops-python-api icon indicating copy to clipboard operation
azure-devops-python-api copied to clipboard

No documentation on how to trigger pipeline build on 'master' branch.

Open BlueInstinct312 opened this issue 6 years ago • 8 comments

import os from azure.devops.connection import Connection from msrest.authentication import BasicAuthentication

Fill in with your personal access token and org URL

PERSONAL_ACCESS_TOKEN = os.getenv("PERSONAL_ACCESS_TOKEN") ORGANIZATION_URL = 'xxxxxx' PROJECT_NAME = "xxxxxx"

def create_pipeline_client(): # Get connection from devOps & create pipeline client credentials = BasicAuthentication('', PERSONAL_ACCESS_TOKEN) connection = Connection(base_url=ORGANIZATION_URL, creds=credentials) pipeline_client = pipeline_client = connection.clients_v5_1.get_pipelines_client() return pipeline_client

def build_pipeline(): pipeline_client = create_pipeline_client() run_params = {'sourceBranch' : "master"} pipe_id = 2098 pipeline_client.run_pipeline( run_parameters=run_params, project=PROJECT_NAME, pipeline_id=pipe_id, pipeline_version=None) print("Pipeline run successfully activated.")

build_pipeline()

BlueInstinct312 avatar Oct 31 '19 23:10 BlueInstinct312

@SamuelDeveloper3 are you providing an example, or are you having issues with this endpoint?

This pipeline client is in early preview, and is not documented yet.

tedchamb avatar Nov 01 '19 18:11 tedchamb

I need help figuring out how to specify to DevOps I want my pipeline to run on master branch. It always runs on release.

I keep trying to pass this dictionary into the run_params parameter of pipeline_client.run_pipeline()

run_params = {'branch/tag' : "master"}

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication

#pip install azure-devops


# Fill in with your personal access token and org URL
PERSONAL_ACCESS_TOKEN = ''"
ORGANIZATION_URL = ''"
PROJECT_NAME = ""
def main():
    print("Automation script to run pipelines on DevOps.")
    run_params = {'branch/tag' : "master"}
    build_pipeline("3011", run_params, None)

def create_pipeline_client():
    # Get connection from devOps & create pipeline client
    credentials = BasicAuthentication('', PERSONAL_ACCESS_TOKEN)
    connection = Connection(base_url=ORGANIZATION_URL, creds=credentials)
    pipeline_client = pipeline_client = connection.clients_v5_1.get_pipelines_client()
    return pipeline_client

def build_pipeline(pipe_id, run_params, pipe_version=None):
    
    pipeline_client = create_pipeline_client()
    print(run_params)
    print("Running Pipeline with id " + str(pipe_id))
    #try:
    pipeline_client.run_pipeline(
        run_parameters=run_params,
        project=PROJECT_NAME,
        pipeline_id=pipe_id,
        pipeline_version=pipe_version)
    print("Pipeline run successfully activated.")
    #except Exception as ex:
    #print("Pipeline run failed with exception: " + str(ex))

def get_pipeline(pipe_id, pipe_version=None):
    pipeline_client = create_pipeline_client()
    pipeline = pipeline_client.get_pipeline(
        project=PROJECT_NAME,
        pipeline_id=pipe_id,
        pipeline_version=pipe_version)
    print(pipeline)

def list_pipelines():
    pipeline_client = create_pipeline_client()
    pipeline_list = pipeline_client.list_pipelines(PROJECT_NAME)
    for x in dir(pipeline_client):
        print(x)

def get_run(pipe_id, runid):
    pipeline_client = create_pipeline_client()
    run = pipeline_client.get_run(project=PROJECT_NAME, pipeline_id=pipe_id, run_id=runid)
    print(run)
    
# Create a connection to the org
if __name__ == '__main__':
    main()

Thanks for all the help!

BlueInstinct312 avatar Nov 01 '19 19:11 BlueInstinct312

@SamuelDeveloper3 are you sure the pipeline id 3011 is of build pipeline and not a release pipeline?

pranaysahith avatar Dec 10 '19 16:12 pranaysahith

I am also trying to use the run_pipeline method to trigger a pipeline. I am able to trigger a pipeline run without trouble, but I can't seem to figure out how to trigger the pipeline on a branch other than master. Any guidance?

nickkomen avatar Apr 30 '20 05:04 nickkomen

I'll answer my own question. In order to run a pipeline using a particular source branch, you can use the queue_build method on the build client (instead of the pipelines client). When doing this, you should use the pipeline id as the build definition id.

nickkomen avatar Apr 30 '20 15:04 nickkomen

I haven’t tried to do this yet, but I would recommend trying to solve it using the same approach I used to solved my first problem:

  1. Install the azure cli
  2. Run the command “azure pipelines run” to run a pipeline, passing in all the options needed for branches, variables, etc
  3. Turn on debug mode for the cli
  4. Watch what REST calls it makes
  5. See what SDK method corresponds to the REST calls it is making

Since it is possible to send in variables and a custom branch via the CLI, I’m confident there is a way to do it through the SDK, even if it is not obvious.

Good Luck!

-Nick

From: venutm [email protected] Reply-To: microsoft/azure-devops-python-api [email protected] Date: Sunday, May 3, 2020 at 10:19 AM To: microsoft/azure-devops-python-api [email protected] Cc: Nick Komen [email protected], Comment [email protected] Subject: Re: [microsoft/azure-devops-python-api] No documentation on how to trigger pipeline build on 'master' branch. (#290)

MS pointed me to your post and I am glad you figured out a work around. Do you have any suggestions on, how would we send pipeline variables It seems to have a parameters :param parameters: The parameters for the build of type string.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fazure-devops-python-api%2Fissues%2F290%23issuecomment-623134530&data=02%7C01%7Cnick.komen%40churchofjesuschrist.org%7C551e67f9fbc94c23f4ba08d7ef7db5d4%7C61e6eeb35fd74aaaae3c61e8deb09b79%7C0%7C0%7C637241195516731709&sdata=7R0tU2Ph8H3BRHNaaDugUAm934xCOpIGXCMY3WiwgAA%3D&reserved=0, or unsubscribehttps://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAMUSH4LPFXPE7TH4FKDGM43RPWKPZANCNFSM4JHT5Z7Q&data=02%7C01%7Cnick.komen%40churchofjesuschrist.org%7C551e67f9fbc94c23f4ba08d7ef7db5d4%7C61e6eeb35fd74aaaae3c61e8deb09b79%7C0%7C0%7C637241195516741704&sdata=%2Bhvxd7uVYN%2BT%2FAzP18dbHdikHilt5Q4ZKT%2BFEiF9ofE%3D&reserved=0.

nickkomen avatar May 04 '20 15:05 nickkomen

Thanks fr the suggestions. I am able to pass all the variables as a json string against the parameters: "{}". Now I have a different challenge. How would I pass the template parameters? I tried adding templateParameters to the run_parameters in pipeline_client as well. None of these seem to work.

venutm avatar May 10 '20 17:05 venutm

I'll answer my own question. In order to run a pipeline using a particular source branch, you can use the queue_build method on the build client (instead of the pipelines client). When doing this, you should use the pipeline id as the build definition id.

@nickkomen can you please provide a code example? I'm trying like below and unfortunately getting errors

a = build_client.get_build(project='MyProject', build_id=1769, property_filters=None)
response = build_client.queue_build(build=a, project='MyProject')

knopka avatar Oct 29 '20 13:10 knopka