Day 77 โ€“ Remote Triggers in Jenkins

On Day 77 of my DevOps Journey, I explored how to trigger Jenkins jobs remotely. This is a crucial feature in CI/CD pipelines when you want external systems (like GitHub, GitLab, or custom scripts) to start Jenkins jobs automatically without manual intervention.

Today, Iโ€™ll document the step-by-step guide for setting up remote job triggers using tokens, API tokens, and Jenkins Crumb issuers.


๐Ÿ”ง Step 1 โ€“ Enable Remote Trigger in Job

  1. Go to Jenkins Job โ†’ Configure.

  2. Under Build Triggers, check โœ… Trigger builds remotely.

  3. Provide a token name (example: testtoken).

  4. Save the configuration.

  5. Jenkins generates a Job URL with token:

     http://<JENKINS_IP>:8080/job/<JOB_NAME>/build?token=testtoken
    

๐Ÿ”ง Step 2 โ€“ Generate API Token for User

  1. Click your username (top right corner in Jenkins).

  2. Select Configure โ†’ API Token โ†’ Generate.

  3. Save the generated API token securely.

     Example: admin:116ce8f1ae914b477d0c74a68ffcc9777c
    

๐Ÿ”ง Step 3 โ€“ Generate Jenkins Crumb

Jenkins requires a crumb (CSRF protection token) for API requests.

  1. Install wget (on Git Bash for Windows).
    Place the binary in c:/program files/Git/mingw64/bin.

  2. Run this command (replace with your username, password, Jenkins URL):

     wget -q --auth-no-challenge --user username --password password \
     --output-document - \
     'http://<JENKINS_IP>:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
    
  3. Example output:

     Jenkins-Crumb:8cb80f4f56d6d35c2121a1cf35b7b501
    

๐Ÿ”ง Step 4 โ€“ Trigger Jenkins Job via cURL

Now we have:

  • Jenkins Job URL with token

  • API Token

  • Crumb

Use this command:

curl -I -X POST \
  http://username:APITOKEN@<JENKINS_IP>:8080/job/<JOB_NAME>/build?token=TOKENNAME \
  -H "Jenkins-Crumb:CRUMB"

โœ… Example:

curl -I -X POST \
  http://admin:110305ffb46e298491ae082236301bde8e@52.15.216.180:8080/job/vprofile-Code-Analysis/build?token=testtoken \
  -H "Jenkins-Crumb:8cb80f4f56d6d35c2121a1cf35b7b501"

๐ŸŽฏ Why This Is Important

  • Enables automation between services (GitHub โ†’ Jenkins, monitoring alerts โ†’ Jenkins, etc.).

  • Provides security with tokens and crumbs.

  • Makes Jenkins more integrated with external systems.


๐Ÿ“Œ Key Takeaways

  • Always secure your tokens โ€“ never expose them in plaintext.

  • Remote triggers are powerful for event-driven CI/CD pipelines.

  • API tokens + crumbs ensure both authentication and protection from CSRF attacks.

0
Subscribe to my newsletter

Read articles from Shaharyar Shakir directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shaharyar Shakir
Shaharyar Shakir