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

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.

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

How to control downstream pipeline's interactive input in upstream pipeline

I have an upstream pipeline which is calling another downstream pipeline
build job: "/org/projectA/master",
parameters: [[$class: 'StringParameterValue', name: 'variable', value: 'value']],
wait: true
In my downstream pipeline, there is a step to ask for approve
input "Deploy to prod?"
Currently the job is paused in the downstream pipeline waiting for approve, but in my main job (upstream pipeline), it is just waiting for sub pipeline to finish, doesn't show any message for approver. So is it possible to display the interactive input in my main pipeline? then the approver doesn't need to click to the sub pipeline to check the status.
BTW, I cannot move the input to main pipeline, cause there are other steps after it in the sub pipeline.
Thanks in advance for any suggestion
I really wouldn't recommend it, but there's a way via Jenkins Remote API -
Jenkins input pipeline step filled via POST with CSRF - howto?
curl -X POST -H "Jenkins-Crumb:${JENKINS_CRUMB}" -d json='{"parameter": {"name": "${PARAMETER_NAME}", "value": "${PARAMETER_VALUE}"}}' -d proceed='${SUBMIT_CAPTION}' 'http://j${JENKINS_URL}/job/${JOB_NAME}/${BUILD_ID}/input/${INPUT_ID}/submit'
The question would be how would you run this? A new input in the upstream job? Run when?
It might be more useful to divide the downstream job into two and run the actual deploy only when user accepts the input in the upstream job.

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.

Resources