How to trigger pipeline in Jenkins with Github Webhook - jenkins

I would like to triggered one pipeline with Github webhook. This pipeline is connected with my Github Repository and a Github webhook.
I want to trigger this pipeline with informations in the webhook as the ID_commit, Github branch, or other things.
For example : if BRANCH == master : Build Pipeline
I try to use 2 plugins but it doesn't works :
Generic Webhook Trigger Plugin
Jenkins github webhook build trigger plugin

The webhook being sent from GitHub on push looks like this:
https://developer.github.com/v3/activity/events/types/#pushevent
...
{
"ref": "refs/heads/changes",
"before": "9049f1265b7d61be4a8904a9a27120d2064dab3b",
"after": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
"created": false,
...
To get the branch (ref) into a variable you need to configure a JSONPath parameter. Like maby variable ref and expression $.ref
To trigger only if branch is master you need to configure a filter. In Optional filter, specify regexp as master and text as $ref
Also, as the wiki suggests, troubleshooting this with curl is probably easiest.

Related

Jenkins with Jenkinsfile Pipeline - Trigger Builds Remotely

I have a Jenkinsfile setup for our CI/CD pipeline, and it runs through the pipeline on git actions like Pull Requests, Branch Creation, Tag Pushes, etc..
Prior to this setup, I was used to setting up Jenkins build jobs in the Jenkins UI. The advantage of this, was that I could setup dedicated build jobs that I could trigger remotely, and independently of git webhook actions. I could do a POST to the job endpoint with parameters to trigger various actions.
Documentation for this process would be referenced here - see "Trigger Builds Remotely"
I could also hit the big button that says "Build", or "Build with Parameters" in the UI, which was super nice.
How would one do this with a Jenkinsfile? Is this even possible to define build jobs in a pipeline definition within a Jenkinsfile? I.E. define functions / build jobs that have dedicated URLs that could be called on the Jenkins URL independent of webhook callbacks?
What's the best practice here?
Thanks for any tips, references, suggestions!
I would recommend starting with Multibranch pipelines. In general you get all the things you mentioned, but a little better. Because thhe paramteres can be defined within your Jenkinsfile. In short just do it like this:
Create a Jenkinsfile an check this into a Git Repository.
To create a Multibranch Pipeline: Click New Item on Jenkins home page.
Enter a name for your Pipeline, select Multibranch Pipeline and click OK.
Add a Branch Source (for example, Git) and enter the location of the repository.
Save the Multibranch Pipeline project.
A declarative Jenkinsfile can look like this:
pipeline {
agent any
parameters {
string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
}
stages {
stage('Example') {
steps {
echo "${params.Greeting} World!"
}
}
}
}
A good tutorial with screenshhots can be found here: https://www.jenkins.io/doc/book/pipeline/multibranch/

The difference of token in build configuration and in jenkinsfile triggers block

Preconditions: Add a webhook in my repository:
http://JenkinsURL:Port/multibranch-webhook-trigger/invoke?token=myToken
Go to Build Configuration -> Scan Multibranch Pipeline Trigger, Tick "Scan by webhook" and add "myToken" for "Trigger token"
Push from repository -> build triggered -> work as expected
Untick "Scan by webhook" from Build Configuration
Add a trigger block like below
triggers {
GenericTrigger(
genericVariables:[
//some variables
],
token: 'myToken',
//some configurations
)
}
stages{
// ...
}
Push from repository -> not build -> not expected behavior, seems token not work here
I wonder what the difference of these 2 tokens and how I should use token in jenkinsfile
I get the explations, see my answer below. If any one knows how we can use Multibranch webhook in triggers
I got the point:
There're 2 webhook plugins:
Generic Webhook Trigger Plugin, receives any HTTP request as JENKINS_URL/generic-webhook-trigger/invoke?token=GenericToken. It can be used in Build Trigger in Freestyle project, Pipeline or other project... It also can be added in triggers block in jenkinsfile.
Multibranch Scan Webhook Trigger, receives any HTTP request as JENKINS_URL/multibranch-webhook-trigger/invoke?token=MultibranchToken and can be configured in Build Configuration in Multibranch Pipeline.
I don't know if we can use Multibranch Scan Webhook Trigger in jenkinsfile.
Back to my question, I use Generic Webhook Trigger in triggers block, but send Multibranch webhook trigger from HTTP request. That's why it doesn't work.

Jenkins Multibranch pipeline pull request

I am using multibranch pipeline for pullrequest. When a pr is created it triggers the pullrequest job. Is there anyway to trigger the job only on specific pull requests instead of all.
example: I have three branches develop, fb and master. I want to trigger the job only when I create a pull request from develop to master, not when I create a pullrequest from fb to develop or fb to master.
In this case you may want to run your pipeline, analyze the base branch, and stop if the base branch is not to your liking.
The environment variable CHANGE_ID is set by the github branch source plugin in case the branch is a pull request.
If it's set, you can explore the global object named pullRequest, e.g. like this:
if (env.CHANGE_ID) {
echo("Looking for PR: PR detected, change id is ${env.CHANGE_ID}")
def prBase = pullRequest.base
if (prBase != 'master') {
currentBuild.result = 'ABORTED'
error("This PR is over ${prBase} branch, not 'master'. Aborting.")
}
}

How do I access the payload from a generic webhook in my Jenkinsfile?

I have a multibranch job using a generic webhook and I want to access the JSON payload the Jenkins receives. I unfortunately cannot seem to access it, I cannot define parameters for the multibranch job and I am at a loss.
I would like to determine the cause of the trigger, whether it be from a pull request, a push, a commit, etc. Multibranch pipelines don't allow for me to specify any variables in Jenkins, so I'm a bit confused.
Configure a JSONPath variable with the JSONPath $ and it will be resolved to the entire received JSON.
See also: https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd/jsonpath.feature
And to do this in a multibranch pipeline, your pipeline can look something like this:
properties([
pipelineTriggers([
[$class: 'GenericTrigger',
genericVariables: [
[key: 'everything', value: '$']
],
...
]
])
])
The readme has complete examples on how to use it with Multibranch.
You can access the payload by configuring build triggers in the configure section of the jenkins job.enter image description here

How to make a parameterized multibranch pipeline trigger?

Can you please give me an example of how should the Jenkinsfile config look to be triggered by a curl http trigger-buildwithparameters? I have tried:
properties([parameters(
[string(defaultValue: 'nothing',
description: 'gitcommit',
name: 'somecommit')])])
but it seems that this pipeline won't be triggered by buildwithparameters with a #somecommit parameter. Can bitbucket branch source/multibranch pipelines be triggered by a remote http calls with passed parameters?

Resources