Module 4 Lesson 4: Pipeline Triggers and API
·DevOps

Module 4 Lesson 4: Pipeline Triggers and API

Automation beyond Git. Learn how to trigger GitLab pipelines using HTTP requests, custom tokens, and external services like Webhooks or IoT devices.

Module 4 Lesson 4: Pipeline Triggers and API

Sometimes, you want a pipeline to start when something happens Outside of GitLab.

  • Example A: A physical sensor in a server room detects heat and triggers a "Graceful Shutdown" pipeline.
  • Example B: Your legacy Jenkins server finishes a process and needs to signal GitLab to start a deployment.

1. Creating a Trigger Token

  1. Go to Settings -> CI/CD -> Pipeline Triggers.
  2. Add a new trigger (e.g., "Mainframe Trigger").
  3. GitLab will give you a Token.

2. Triggering via cURL

You can now start the pipeline from any terminal in the world:

curl --request POST \
     --form token=YOUR_TRIGGER_TOKEN \
     --form ref=main \
     https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/trigger/pipeline

3. Passing Variables through the API

You can even send data into the pipeline that doesn't exist in Git:

curl --request POST \
     --form token=TOKEN \
     --form "variables[DEPLOY_VERSION]=1.2.3" \
     --form "variables[AUTHORIZED_BY]=Sudeep" \
     https://gitlab.com/api/v4/projects/ID/trigger/pipeline

Inside your .gitlab-ci.yml, you can now access $DEPLOY_VERSION like any other variable!


4. Why Use This?

  1. Slack Integration: Create a custom "Slash Command" in Slack that triggers a production deployment.
  2. Webhooks: Have your bug tracker (like Jira) trigger a "Redeploy" when a fix is marked as resolved.
  3. Scheduled External Tasks: Use a standard cron job on a different server to tell GitLab when to perform a weekly database cleanup.

Exercise: The Remote Control

  1. Setup a Trigger Token in your test project.
  2. Find your Project ID (it's under the title on the project homepage).
  3. Open your terminal and run the curl command from Section 2.
  4. Go to the GitLab dashboard. Did a new pipeline start?
  5. Try to pass a variable called GREETING and have a job in your YAML echo $GREETING.
  6. Why is it dangerous to share your "Trigger Token" with the public?

Summary

Pipeline triggers connect your "Software" world with the "Physical" or "External" world. By using the GitLab API, you transform your CI/CD system from a reactive script into a powerful service that can be controlled by any code, anywhere.

Next Lesson: Managing the giants: Optimizing for large clusters.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn