Skip to content

Trigger a Jenkins job from Bitbucket Pipelines

· 1 min read

First published on the pushpanel.io blog as “How to trigger a Jenkins Remote job using pipelines in BitBucket with parameters” and archived here. The original domain is no longer ours, so links to it were removed and the headline rewritten for this site; the text of the post is unchanged.

The Jenkins and Bitbucket logos joined by a plus sign

So I have a situation where I need to trigger a simple job for calculating data on a package. BitBucket pipelines take too much time to complete so it was a good idea to place that job on external Jenkins server. Information that this job requirement is a ${bitbucket_commit} and this needs to be passed through. Forget Webhooks.

Pre-requirements:

  1. Jenkins Server
  2. Access to a Bitbucket repository

Steps:

  1. Install two plugins to a Jenkins server that would allow triggering anonymously a job:

  2. Create a job inside a Jenkins.

    • Enable a Build Trigger
      Jenkins Build Triggers section with “Trigger builds remotely” ticked and an authentication token filled in
    • Make build parametrised
      Jenkins job made parametrised, with a string parameter named COMMIT_ID
    • Execute shell Commit
      Jenkins Execute shell step running make BB_COMMIT=${COMMIT_ID::7}
      *Downloading repository archive notes:
      Use short (12 characters) version of commit_hash in BUILD script if you need to download zip file etc like https://bitbucket.org/<COMPANY>/<REPOSITORY>/get/<COMMIT_ID::12>.zip (you can use a long hash to download but after unzip folder would be in short hash )
      EDIT: $(echo $COMMIT_ID | cut -c1-12)
  3. Make sure your BitBucket can speak with your Jenkins ( Firewall )

  4. Create a pipeline inside BitBucket /bitbucket-pipelines.yml

pipelines:
default:
   - step:
      name: Trigger Build
      script:
         - curl "https://<YOURJENKINS>.com/jenkins/buildByToken/buildWithParameters?job=<YOUR JOB NAME>&COMMIT_ID=${BITBUCKET_COMMIT}&token=<YOUR TOKEN>"