Trigger pipelines by using the API | GitLab (2024)

  • Create a pipeline trigger token
  • Trigger a pipeline
    • Use cURL
    • Use a CI/CD job
    • Use a webhook
      • Access webhook payload
    • Pass CI/CD variables in the API call
  • Revoke a pipeline trigger token
  • Configure CI/CD jobs to run in triggered pipelines
  • See which pipeline trigger token was used
  • Troubleshooting
    • 403 Forbidden when you trigger a pipeline with a webhook
    • 404 Not Found when triggering a pipeline
    • The requested URL returned error: 400 when triggering a pipeline

Tier: Free, Premium, UltimateOffering: GitLab.com, Self-managed, GitLab Dedicated

To trigger a pipeline for a specific branch or tag, you can use an API callto the pipeline triggers API endpoint.

If you are migrating to GitLab CI/CD, you cantrigger GitLab CI/CD pipelines by calling the API endpoint from the other provider’s jobs.For example, as part of a migration from Jenkins or CircleCI.

When authenticating with the API, you can use:

  • A pipeline trigger token to trigger a branch or tag pipelinewith the pipeline triggers API endpoint.
  • A CI/CD job token to trigger a multi-project pipeline.
  • Another token with API access to create a new pipelinewith the project pipeline API endpoint.

Create a pipeline trigger token

You can trigger a pipeline for a branch or tag by generating a pipeline trigger token and using itto authenticate an API call. The token impersonates a user’s project access and permissions.

Prerequisites:

  • You must have at least the Maintainer role for the project.

To create a trigger token:

  1. On the left sidebar, select Search or go to and find your project.
  2. Select Settings > CI/CD.
  3. Expand Pipeline trigger tokens.
  4. Select Add new token
  5. Enter a description and select Create pipeline trigger token.
    • You can view and copy the full token for all triggers you have created.
    • You can only see the first 4 characters for tokens created by other project members.

It is a security risk to save tokens in plain text in public projects, or store themin a way that malicious users could access them. A leaked trigger token could beused to force an unscheduled deployment, attempt to access CI/CD variables,or other malicious uses. Masked CI/CD variableshelp improve the security of trigger tokens. For more information about keeping tokens secure,see the security considerations.

Trigger a pipeline

After you create a pipeline trigger token, you can use it to triggerpipelines with a tool that can access the API, or a webhook.

Use cURL

You can use cURL to trigger pipelines with the pipeline triggers API endpoint.For example:

  • Use a multiline cURL command:

    curl --request POST \ --form token=<token> \ --form ref=<ref_name> \ "https://gitlab.example.com/api/v4/projects/<project_id>/trigger/pipeline"
  • Use cURL and pass the <token> and <ref_name> in the query string:

    curl --request POST \ "https://gitlab.example.com/api/v4/projects/<project_id>/trigger/pipeline?token=<token>&ref=<ref_name>"

In each example, replace:

  • The URL with https://gitlab.com or the URL of your instance.
  • <token> with your trigger token.
  • <ref_name> with a branch or tag name, like main.
  • <project_id> with your project ID, like 123456. The project ID is displayedon the project overview page.

Use a CI/CD job

You can use a CI/CD job with a pipeline triggers token to trigger pipelines when another pipelineruns.

For example, to trigger a pipeline on the main branch of project-B when a tagis created in project-A, add the following job to project A’s .gitlab-ci.yml file:

trigger_pipeline: stage: deploy script: - 'curl --fail --request POST --form token=$MY_TRIGGER_TOKEN --form ref=main "https://gitlab.example.com/api/v4/projects/123456/trigger/pipeline"' rules: - if: $CI_COMMIT_TAG environment: production

In this example:

  • 1234 is the project ID for project-B. The project ID is displayed on theproject overview page.
  • The rules cause the job to run every time a tag is added to project-A.
  • MY_TRIGGER_TOKEN is a masked CI/CD variablethat contains the trigger token.

Use a webhook

To trigger a pipeline from another project’s webhook, use a webhook URL like the followingfor push and tag events:

https://gitlab.example.com/api/v4/projects/<project_id>/ref/<ref_name>/trigger/pipeline?token=<token>

Replace:

  • The URL with https://gitlab.com or the URL of your instance.
  • <project_id> with your project ID, like 123456. The project ID is displayedon the project overview page.
  • <ref_name> with a branch or tag name, like main. This value takes precedence over the ref_name in the webhook payload.The payload’s ref is the branch that fired the trigger in the source repository.You must URL-encode the ref_name if it contains slashes.
  • <token> with your pipeline trigger token.

Access webhook payload

If you trigger a pipeline by using a webhook, you can access the webhook payload withthe TRIGGER_PAYLOAD predefined CI/CD variable.The payload is exposed as a file-type variable,so you can access the data with cat $TRIGGER_PAYLOAD or a similar command.

Pass CI/CD variables in the API call

You can pass any number of CI/CD variables in the trigger API call.These variables have the highest precedence,and override all variables with the same name.

The parameter is of the form variables[key]=value, for example:

curl --request POST \ --form token=TOKEN \ --form ref=main \ --form variables[UPLOAD_TO_S3]="true" \ "https://gitlab.example.com/api/v4/projects/123456/trigger/pipeline"

CI/CD variables in triggered pipelines display on each job’s page, but onlyusers with the Owner and Maintainer role can view the values.

Revoke a pipeline trigger token

To revoke a pipeline trigger token:

  1. On the left sidebar, select Search or go to and find your project.
  2. Select Settings > CI/CD.
  3. Expand Pipeline triggers.
  4. To the left of the trigger token you want to revoke, select Revoke ().

A revoked trigger token cannot be added back.

Configure CI/CD jobs to run in triggered pipelines

To configure when to run jobs in triggered pipelines, you can:

  • Use rules with the $CI_PIPELINE_SOURCE predefined CI/CD variable.
  • Use only/except keywords, though rulesis the preferred keyword.
$CI_PIPELINE_SOURCE valueonly/except keywordsTrigger method
triggertriggersIn pipelines triggered with the pipeline triggers API by using a trigger token.
pipelinepipelinesIn multi-project pipelines triggered with the pipeline triggers API by using the $CI_JOB_TOKEN, or by using the trigger keyword in the CI/CD configuration file.

Additionally, the $CI_PIPELINE_TRIGGERED predefined CI/CD variable is set to truein pipelines triggered with a pipeline trigger token.

See which pipeline trigger token was used

You can see which pipeline trigger token caused a job to run by visiting the single job page.A part of the trigger token displays on the right of the page, under the job details:

In pipelines triggered with a trigger token, jobs are labeled as triggered inBuild > Jobs.

Troubleshooting

403 Forbidden when you trigger a pipeline with a webhook

When you trigger a pipeline with a webhook, the API might return a {"message":"403 Forbidden"} response.To avoid trigger loops, do not use pipeline events to trigger pipelines.

404 Not Found when triggering a pipeline

A response of {"message":"404 Not Found"} when triggering a pipeline might be causedby using a personal access tokeninstead of a pipeline trigger token. Create a new trigger tokenand use it instead of the personal access token.

The requested URL returned error: 400 when triggering a pipeline

If you attempt to trigger a pipeline by using a ref that is a branch name thatdoesn’t exist, GitLab returns The requested URL returned error: 400.

For example, you might accidentally use main for the branch name in a project thatuses a different branch name for its default branch.

Another possible cause for this error is a rule that prevents creation of the pipelines when CI_PIPELINE_SOURCE value is trigger, such as:

rules: - if: $CI_PIPELINE_SOURCE == "trigger" when: never

Review your workflow:rules to ensure a pipeline can be created when CI_PIPELINE_SOURCE value is trigger.

Trigger pipelines by using the API | GitLab (2024)
Top Articles
How Gen Z Is Giving the Old-School Stick Shift a Second Life
4.8. Converting Decimal Numbers to Binary Numbers — Problem Solving with Algorithms and Data Structures
Hotels Near 6491 Peachtree Industrial Blvd
Fat Hog Prices Today
Amc Near My Location
Brendon Tyler Wharton Height
Sprague Brook Park Camping Reservations
Umn Pay Calendar
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
MADRID BALANZA, MªJ., y VIZCAÍNO SÁNCHEZ, J., 2008, "Collares de época bizantina procedentes de la necrópolis oriental de Carthago Spartaria", Verdolay, nº10, p.173-196.
The Binding of Isaac
Skylar Vox Bra Size
Drago Funeral Home & Cremation Services Obituaries
Nalley Tartar Sauce
Gdp E124
Abortion Bans Have Delayed Emergency Medical Care. In Georgia, Experts Say This Mother’s Death Was Preventable.
Conan Exiles: Nahrung und Trinken finden und herstellen
Exterior insulation details for a laminated timber gothic arch cabin - GreenBuildingAdvisor
Blue Rain Lubbock
Vegas7Games.com
Bòlèt Florida Midi 30
Craigslistodessa
Understanding Gestalt Principles: Definition and Examples
When Does Subway Open And Close
Chamberlain College of Nursing | Tuition & Acceptance Rates 2024
The Boogeyman (Film, 2023) - MovieMeter.nl
Craigslist Ludington Michigan
Xpanas Indo
Ardie From Something Was Wrong Podcast
Cosas Aesthetic Para Decorar Tu Cuarto Para Imprimir
Jailfunds Send Message
Desales Field Hockey Schedule
Vip Lounge Odu
Tire Pro Candler
3400 Grams In Pounds
Craigslist Pets Plattsburgh Ny
Xxn Abbreviation List 2023
Jamesbonchai
Craigslist Central Il
Citymd West 146Th Urgent Care - Nyc Photos
Professors Helpers Abbreviation
Nope 123Movies Full
Lebron James Name Soundalikes
Abigail Cordova Murder
FactoryEye | Enabling data-driven smart manufacturing
Sitka Alaska Craigslist
F9 2385
Deshuesadero El Pulpo
The Goshen News Obituary
Taterz Salad
Vt Craiglist
Emmi-Sellers
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6595

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.