Trigger a Jenkins Pipeline using GitHub Actions

2 minute read

You’ve moved to using GitHub Enterprise Cloud, but you have a significant investment in Jenkins. Your Jenkins server is behind the firewall, which makes executing webhooks to trigger a Jenkins pipeline on, say, creation of a pull request, not feasible. How can we get around this?

GitHub Actions to the rescue. Using a combination of GitHub Actions and a self-hosted runner, you can trigger a Jenkins pipeline based off any event thrown in GitHub, for example, the creation of a pull request, a push to a branch, or even something non-CI/CD related, such as creation of an issue. This post is going to show you one way to accomplish this.

Ultimately, you will be triggering the Jenkins pipeline using the Jenkins API. While you can write all the curl commands yourself to do this, why do that when you can take advantage of the GitHub Marketplace and see if someone has written a GitHub Action to help with this. Turns out, there a several actions created by the community to do this. What I found with most of these actions though, is that they are container actions, which require you to have Docker installed on your runner.

So I decided to create my own custom action: Trigger Jenkins Job and Return Result. This action is still a little raw and new, and needs some enhancements, but it gets the job done, and shows you what is possible. Also, it was my first attempt at creating a bash script. Want to contribute back and add some functionality to it? Send me a PR! Make sure to read the README, as there are some caveats to using it in its current form:

  • Your self-hosted runner must be Linux, and must have curl installed
  • Currently the action does no support parameters. That will be coming in a future version

The action itself is pretty easy to use. Here is a reference workflow file for you.

name: Test Jenkins Action

on:
  workflow_dispatch:


jobs:

  testaction:
    
    runs-on: ubuntu-latest
    
    steps:
      
      - id: testjenkinsaction
        uses: mickeygoussetorg/trigger-jenkins-job@v1
        with:
          jenkins-server:  # URL of the jenkins server
          jenkins-job: # The name of the jenkins job to run
          jenkins-username:  # user name for accessing jenkins
          jenkins-pat: # personal Access token for accessing Jenkins
          poll-time: # how often (in seconds) to poll the jenkins server for results
          timeout-value: # How long (in seconds) to poll before timing out the action
          verbose: # true/false - turns on extra logging

Go give this action a spin and let me know what you think.