Triggering GitLab CI Pipelines via API and Passing Parameters
This article demonstrates how to set up a GitLab CI pipeline trigger token, use curl or Postman to invoke the pipeline via API, embed the trigger in .gitlab-ci.yml, and pass custom variables to the job, with example scripts and best‑practice tips.
The preparation steps include configuring the project, CI/CD settings, and creating a dedicated authentication token for pipeline triggers.
To trigger a pipeline via API, you can use a curl command such as:
curl -X POST \
-F token=b2ba95b229f5fd0eb834454bc9aad8 \
-F ref=main \
http://192.168.1.200/api/v4/projects/31/trigger/pipelinePostman can also be used for testing the same endpoint.
Within a GitLab CI job, the trigger can be invoked directly:
script:
- "curl -X POST -F token=TOKEN -F ref=REF_NAME http://192.168.1.200/api/v4/projects/31/trigger/pipeline"For better security, store the token as a CI/CD variable and reference it in the script:
stages:
- build
build-job:
tags:
- build
stage: build
script:
- "curl -X POST -F token=${CITOKEN} -F ref=main http://192.168.1.200/api/v4/projects/29/trigger/pipeline"
- echo "Compile complete."
- sleep 1Parameters can be passed to the pipeline by adding them as variables in the API call:
curl -X POST \
-F token=TOKEN \
-F "ref=main" \
-F "variables[BUILD_TOOL]=mavenandmaven" \
http://192.168.1.200/api/v4/projects/31/trigger/pipelineA corresponding job can retrieve and use the variable:
ciTriggerTest:
tags:
- build
stage: build
script:
- echo ${BUILD_TOOL}The article concludes with a note that the content is excerpted from the third DevOps practice training camp and invites readers to follow the DevOps Cloud Academy for more resources.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.