How to make a parameterized multibranch pipeline trigger? - jenkins

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?

Related

how to get upstream build information in a script step of jenkins classic ui pipeline

I have an old classic ui jenkins pipeline. now i need this pipeline to be triggered on the completion of other pipelines. And get the upstream pipeline information in this old pipeline.
I know how to set the upstream build trigger in the jenkins pipeline. However i cannot find a way to get the upstream build information (eg, project name, git commit).
When i output the env variables in downstream pipeline, i can only see the BUILD_CAUSE=UPSTREAMTRIGGER which is not useful for me.
Trigger Downstream Job With Parameters
The old job would need to be updated to be parameterised, then you can pass the required information as parameters when you build the downstream job.
Example:
build job: "DOWNSTREAM_JOB_NAME",
parameters: [string(name: 'upstreamJobName', value: env.JOB_NAME),
string(name: 'upstreamJobVar', value: "${upstreamJobVar}"]
Trigger Downstream Job Without Parameters
When parameters are not being send from triggering upstream job, then we can get some of the upstream information in the downstream job like this:
currentBuild.upstreamBuilds[0].projectName
All available methods for upstreamBuilds information can be found here

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 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 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.

Getting the github webhook payload information inside Jenkins pipeline

I need to access the github webhook payload information in my pipeline job through Jenkinsfile. I am able to print the payload using the below snippet:
properties ( [[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'StringParameterDefinition', defaultValue: '', description: '', name: 'payload']]]] )
echo ("This build is built with the payload: $payload")
But this works only when I have defined the webhook for that specific job as below:
http://<jenkins url>/job/Demo_GitHubOrg/job/DemoRepo/branch/master//buildWithParameters
My pipeline job scans the given organization and automatically creates jobs/repo/branch.
Is there a way to write a more generic webhook that will work for all jobs in my pipeline folder instead of writing one for every job as shown above?
Is there a way to write a more generic webhook that will work for all
jobs in my pipeline folder instead of writing one for every job as
shown above?
Yes, the Generic Webhook Trigger Plugin.

Resources