I Would Like to Use the Github Webhook Json file to Launch Relevant Pipeline Builds

Issue

  • I have a pipeline which uses a feature of the Github plugin which forces a workspace (like “ignore certain users”, etc) but would like to not require a workspace
  • I have a pipeline which I would like to custom trigger based on certain conditions from a Github Webhook

Environment

Resolution

You will need to do some pre-setup before this will work.
The pre-setup consists of first installing 4 plugins on all the masters you want to trigger this way:

The external notification plugin will give you the ability to have a new screen under Manage Jenkins -> Manage Notification Webhook HTTP Endpoints which allows you to configure new webhook endpoints. The Notification API provides the ability to configure multiple event types under that screen and to enable the trigger(combined with the OC notification plugin). Finally the Pipeline event step creates the API used in the pipeline.

To correctly set this up after installing those plugins, is to follow this procedure:
1. Allow webhooks by going to Manage Jenkins > Configure Notification and checking the Allow box
2. Create a new webhook location by going to Manage Jenkins > Manage Notification Webhook HTTP Endpoints and hit the Create New Endpoint button
NewNotificationEndpoint
3. Adjust that webhook to use github and generate a new secret
JenkinsWebhook
3. Configure a new webhook(under Settings> Webhooks) on a github repo to hit that newly created endpoint(Payload URL) with the secret
GithubWebhook
4. Add an eventTrigger step to your pipeline which will do the json parsing you want. Below is an example pipeline:

pipeline {
    agent any
    triggers {
        eventTrigger jmespathQuery("repository.name=='REPO_NAME'&&head_commit.author.username!='test_account'")
    }
    stages {
        stage('Example') {
            steps {
                echo 'received Webhook'
            }
        }
    }
}

The above is for a declarative pipelines and if you want to use scripted then properties([pipelineTriggers([eventTrigger(jmespathQuery("repository.name=='REPO_NAME'&&head_commit.author.username!='test_account'"))])])

The JQ command I am doing there is matching the repo name(REPO_NAME) and ensuring the last commit was not written by test_account. The full Webhook API for github can be found here for what will be included in that json file and can be parsed with JQ

References

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.