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

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.

Related

Jenkins Pipeline not providing Bitbucket Server environment variables

How can I configure my Jenkins Pipeline project to provide the CHANGE_* variables related to a Bitbucket Server commit? The project's Pipeline definition is Pipeline script from SCM (Bitbucket Server Integration).
I have checked Bitbucket Server trigger build after push made available from the Bitbucket Server Integration Jenkins plugin and the build does get triggered, but the variables related to the commit/change message, author, author email, etc. are all missing.
pipeline {
agent any
stages {
stage("Hello variables") {
steps {
sh 'printenv'
}
}
}
}
The only Bitbucket related env variables are GIT_BRANCH, GIT_COMMIT, and GIT_URL.
The Bitbucket webhook (trigger) plugin does not provide a json payload.
If you want to get Bitbucket trigger json payload (and query it inside the the pipeline) you'll need to use the Generic Webhook Trigger

Jenkins job should receive deployment status from Spinnaker pipeline (success/error)

I am new to use the spinnaker. I have set up the spinnaker with the helm chart in my k8s cluster. Everything is working fine. Now I have a new requirement to set up the notification for the spinnaker pipeline (Success/failure). But the requirement is tricky.
I read out about Spinnaker and Jenkins to post the success/failure status. Spinnaker has a notification mechanism to notify for each stage/pipeline about success/failure. We can set up this notification. Jenkins has the same mechanism. But the question is when the spinnaker pipeline successfully completed then it triggers the Jenkins job to post the spinnaker pipeline status on the portal. I have created a Jenkins stage that is successful triggers the Jenkins job but it is not catching the status or events from the spinnaker pipeline. In case, job spinnaker pipeline failed, it will not trigger the Jenkins to post the failure message. I tried to find out the solution but unable to do it. If anyone knows how to fix this issue, Please guide me or write me the solution in detail here.
Thanks
You do not need to create Jenkins stage in the same pipeline which you are monitoring. You can create additional Pipeline in Spinnaker which receive trigger from target pipeline. All you need is configure trigger in that new pipeline:
"triggers": [
{
"application": "demo-app",
"enabled": true,
"pipeline": "demo-pipe",
"status": [
"successful",
"failed"
],
"type": "pipeline"
}
]
You can extract required information (e.g. status) about upstream pipeline from this trigger.
If post the pipeline status on the internal portal in your terms means to send an HTTP Request than you should consider a Webhook Stage.
In runtime you have whole parent pipeline context in your downstream and can get it status with expression like ${trigger.parentExecution.status} to provide it in your request.

How to add webhooks in gitlab for multibranch pipeline jenkins

I want to trigger multi-branch pipeline for every push, can anyone please let me know can how can we configure web-hooks in gitlab for multi-branch pipeline.
If you were wondering where the trigger setting is in Multibranch pipeline job settings, this will answers it:
Unlike other job types, there is no 'Trigger' setting required for a Multibranch job configuration; just create a webhook in GitLab for push requests which points to the project's webhook URL.
Source: https://github.com/jenkinsci/gitlab-plugin#webhook-url
You can also provide Gitlab triggers within the Jenkinsfile. You can see examples within the link provided above. This is how I got it work:
pipeline {
agent {
node {
...
}
}
options {
gitLabConnection('GitLab')
}
triggers {
gitlab(
triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All',
addVoteOnMergeRequest: true)
}
stages {
...
}
}
Then in your Gitlab Project go to Settings -> Integrations and type in the Jenkins Job project url in 'URL'. URL should take either form:
http://JENKINS_URL/project/PROJECT_NAME
http://JENKINS_URL/project/FOLDER/PROJECT_NAME
Notice that the url does not contain "job" within it and instead uses "project".
Make sure under Triggers, you have "Push Events" checked as well if you want the job to trigger whenever someone pushes a commit.
Finally, run a build against your Jenkinsfile first before testing the webhook so Jenkins will pick-up the trigger settings for Gitlab.

How to trigger pipeline in Jenkins with Github Webhook

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.

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