tasks/pipeline.py
def trigger_child_pipeline(_, git_ref, project_name, variables="", follow=True):
    """
    Trigger a child pipeline on a target repository and git ref.
    Used in CI jobs only (requires CI_JOB_TOKEN).

    Use --variables to specify the environment variables that should be passed to the child pipeline, as a comma-separated list.

    Use --follow to make this task wait for the pipeline to finish, and return 1 if it fails. (requires GITLAB_TOKEN).

    Examples:
    inv pipeline.trigger-child-pipeline --git-ref "master" --project-name "DataDog/agent-release-management" --variables "RELEASE_VERSION"

    inv pipeline.trigger-child-pipeline --git-ref "master" --project-name "DataDog/agent-release-management" --variables "VAR1,VAR2,VAR3"
    """

    if not os.environ.get('CI_JOB_TOKEN'):
        raise Exit("CI_JOB_TOKEN variable needed to create child pipelines.", 1)

    if not os.environ.get('GITLAB_TOKEN'):
        if follow:
            raise Exit("GITLAB_TOKEN variable needed to follow child pipelines.", 1)
        else:
            # The Gitlab lib requires `GITLAB_TOKEN` to be
            # set, but trigger_pipeline doesn't use it
            os.environ["GITLAB_TOKEN"] = os.environ['CI_JOB_TOKEN']

    gitlab = Gitlab(project_name=project_name, api_token=get_gitlab_token())

    data = {"token": os.environ['CI_JOB_TOKEN'], "ref": git_ref, "variables": {}}

    # Fill the environment variables to pass to the child pipeline.
    for v in variables.split(','):
        data['variables'][v] = os.environ[v]

    print(f"Creating child pipeline in repo {project_name}, on git ref {git_ref} with params: {data['variables']}")

    res = gitlab.trigger_pipeline(data)

    if 'id' not in res:
        raise Exit(f"Failed to create child pipeline: {res}", code=1)

    pipeline_id = res['id']
    pipeline_url = res['web_url']
    print(f"Created a child pipeline with id={pipeline_id}, url={pipeline_url}")

    if follow:
        print("Waiting for child pipeline to finish...")

        wait_for_pipeline(gitlab, pipeline_id)

        # Check pipeline status
        pipeline = gitlab.pipeline(pipeline_id)
        pipestatus = pipeline["status"].lower().strip()

        if pipestatus != "success":
            raise Exit(f"Error: child pipeline status {pipestatus.title()}", code=1)

        print("Child pipeline finished successfully")

    Trigger a child pipeline on a target repository and git ref.
    Used in CI jobs only (requires CI_JOB_TOKEN).

    Use --variables to specify the environment variables that should be passed to the child pipeline, as a comma-separated list.

    Use --follow to make this task wait for the pipeline to finish, and return 1 if it fails. (requires GITLAB_TOKEN).

    Examples:
    inv pipeline.trigger-child-pipeline --git-ref "master" --project-name "DataDog/agent-release-management" --variables "RELEASE_VERSION"

    inv pipeline.trigger-child-pipeline --git-ref "master" --project-name "DataDog/agent-release-management" --variables "VAR1,VAR2,VAR3"
    
Trigger a child pipeline on a target repository and git ref.
Used in CI jobs only (requires CI_JOB_TOKEN).

-variables to specify the environment variables that should be passed to the child pipeline, as a comma-separated list.

-follow to make this task wait for the pipeline to finish, and return 1 if it fails.
Trigger a child pipeline on a target repository and git ref . Used in CI jobs only ( requires CI_JOB_TOKEN ) . - variables to specify the environment variables that should be passed to the child pipeline as a comma - separated list . - follow to make this task wait for the pipeline to finish and return 1 if it fails .
def trigger_child_pipeline(_, git_ref, project_name, variables="", follow=True):
    if not os.environ.get('CI_JOB_TOKEN'):
        raise Exit("CI_JOB_TOKEN variable needed to create child pipelines.", 1)
    if not os.environ.get('GITLAB_TOKEN'):
        if follow:
            raise Exit("GITLAB_TOKEN variable needed to follow child pipelines.", 1)
        else:
            os.environ["GITLAB_TOKEN"] = os.environ['CI_JOB_TOKEN']
    gitlab = Gitlab(project_name=project_name, api_token=get_gitlab_token())
    data = {"token": os.environ['CI_JOB_TOKEN'], "ref": git_ref, "variables": {}}
    for v in variables.split(','):
        data['variables'][v] = os.environ[v]
    print(f"Creating child pipeline in repo {project_name}, on git ref {git_ref} with params: {data['variables']}")
    res = gitlab.trigger_pipeline(data)
    if 'id' not in res:
        raise Exit(f"Failed to create child pipeline: {res}", code=1)
    pipeline_id = res['id']
    pipeline_url = res['web_url']
    print(f"Created a child pipeline with id={pipeline_id}, url={pipeline_url}")
    if follow:
        print("Waiting for child pipeline to finish...")
        wait_for_pipeline(gitlab, pipeline_id)
        pipeline = gitlab.pipeline(pipeline_id)
        pipestatus = pipeline["status"].lower().strip()
        if pipestatus != "success":
            raise Exit(f"Error: child pipeline status {pipestatus.title()}", code=1)
        print("Child pipeline finished successfully")
def trigger_child_pipeline ( _ , git_ref , project_name , variables = "" , follow = True ) : if not os . environ . get ( 'CI_JOB_TOKEN' ) : raise Exit ( "CI_JOB_TOKEN variable needed to create child pipelines." , 1 ) if not os . environ . get ( 'GITLAB_TOKEN' ) : if follow : raise Exit ( "GITLAB_TOKEN variable needed to follow child pipelines." , 1 ) else : os . environ [ "GITLAB_TOKEN" ] = os . environ [ 'CI_JOB_TOKEN' ] gitlab = Gitlab ( project_name = project_name , api_token = get_gitlab_token ( ) ) data = { "token" : os . environ [ 'CI_JOB_TOKEN' ] , "ref" : git_ref , "variables" : { } } for v in variables . split ( ',' ) : data [ 'variables' ] [ v ] = os . environ [ v ] print ( f"Creating child pipeline in repo {project_name}, on git ref {git_ref} with params: {data['variables']}" ) res = gitlab . trigger_pipeline ( data ) if 'id' not in res : raise Exit ( f"Failed to create child pipeline: {res}" , code = 1 ) pipeline_id = res [ 'id' ] pipeline_url = res [ 'web_url' ] print ( f"Created a child pipeline with id={pipeline_id}, url={pipeline_url}" ) if follow : print ( "Waiting for child pipeline to finish..." ) wait_for_pipeline ( gitlab , pipeline_id ) pipeline = gitlab . pipeline ( pipeline_id ) pipestatus = pipeline [ "status" ] . lower ( ) . strip ( ) if pipestatus != "success" : raise Exit ( f"Error: child pipeline status {pipestatus.title()}" , code = 1 ) print ( "Child pipeline finished successfully" )
Trigger a child pipeline on a target repository and git ref.
Trigger a child pipeline on a target repository and git ref .
"""
    Trigger a child pipeline on a target repository and git ref.
    Used in CI jobs only (requires CI_JOB_TOKEN).

    Use --variables to specify the environment variables that should be passed to the child pipeline, as a comma-separated list.

    Use --follow to make this task wait for the pipeline to finish, and return 1 if it fails. (requires GITLAB_TOKEN).

    Examples:
    inv pipeline.trigger-child-pipeline --git-ref "master" --project-name "DataDog/agent-release-management" --variables "RELEASE_VERSION"

    inv pipeline.trigger-child-pipeline --git-ref "master" --project-name "DataDog/agent-release-management" --variables "VAR1,VAR2,VAR3"
    """ # The Gitlab lib requires `GITLAB_TOKEN` to be # set, but trigger_pipeline doesn't use it # Fill the environment variables to pass to the child pipeline. # Check pipeline status
_
git_ref
project_name
variables
follow
examples
None