How to promote one job from other in jenkins? - jenkins

I want to promote one job(A) from another job(B) in jenkins once job B is successful. Was checking out if there is some "Promote Another Job Plugin" exist or not to achieve the same.

You might want to best use this plugin:
Jenkins Parameterized Trigger plugin
*You will need to use this in Post-build Actions in Jenkins job(B) which will trigger job(A)

As answered in Allow promotion of job in Jenkins pipeline there is a Promoted Builds Plugin which does not support delclarative pipelines. So if you do not use a declarative pipeline it could work for you.

Related

How to write jenkins pipeline for Jira Release Version parameter

I have freestyle Jenkins job with Jira Release Version Parameter:
I need to migrate this job to use Jenkins pipeline instead of freestyle job. Can somebody give me a piece of pipeline code (in declarative language) which does that?
After some investigation, we figured out that at this moment it is not possible to implement this plugin in the pipeline job.

Jenkins Multibranch Job configuration as a pipeline Job

I have mutibranch job in jenkins. Is there any way i can do the multibranch job configurations in a Jenkinsfile? like branchsource , behaviours etc in multibranch job i want to store as a code
Short answer is NO.
But if you still want it, you can store job xml configuration in Git, and then update job through Jenkins API using a local script or even another Jenkins job. Don't recommend though.

How to get the Generic Webhook Trigger Plugin to work with multibranch pipelines in Jenkins?

I'm trying to set up a scenario where a pull request is created on github that triggers a Jenkins multibranch pipeline, and where that multibranch pipeline uses the Generic Webhook Plugin to extract values from the POST request sent from github to jenkins to be used in the script.
Unfortunately, as described on the Generic Webhook Trigger Plugin wiki:
Note: When configuring from pipeline, that pipeline needs to run once, to apply the plugin trigger config, and after that this plugin will be able to trigger the job. This is how Jenkins works, not something implemented in this plugin. You can avoid this by using Job DSL and have Job DSL create pipeline jobs with the plugin configured in that DSL.
This would be OK using a normal pipeline since it would just be a one off on creation of the Jenkins job. The problem however is that a multibranch pipeline will create a new job whenever a new branch/PR is created, and that means that for each pull request I create on github (which triggers my multibranch pipeline script), I have to then run it twice to get the generic webhook functionality working. Having to resubmit for each PR would be tedious for long-run projects.
It seems to me like there are two possible approaches to solving/improving on this problem. One is to try and play around with DSL Jobs (as suggested by the wiki); but I tried this and couldn't get it to work (it was adding a huge amount of complexity to the set up, so I've abandoned it for now).
The second possible solution is as follows: when a PR is created in github, the Generic Webhook will cause a new job to be created in the multibranch pipeline corresponding to that PR; the first time the multibranch pipeline runs the first build of this newly created job will fail for the reason given in the quote above; but then a solution might involve testing that the first job failed and somehow telling Jenkins to try rebuilding for that job again.
So my question relates to this second approach: how can I most neatly run a rebuild for this multibranch pipeline upon the creation of a PR on github?
Any advice/suggestions would be appreciated!
For triggering multibranch pipline by webhook you can use this plugin:
"Multibranch Scan Webhook Trigger"
https://plugins.jenkins.io/multibranch-scan-webhook-trigger/
Actually that is not true for multibranch pipelines. Just ordinary pipelines needs to run twice.
I updated the docs like:
When configuring from pipeline (not multibranch pipeline)...

Is it possible to trigger Multibranch pipeline job from a regular Jenkins job under "Build other Projects"?

I have 2 jobs. One is a regular freestyle job on Jenkins that is supposed to trigger another job which is a Multibranch pipeline job.
The issue is whenever I enter the name of the Multibranch job in the "Projects to Build column", I get an error - "x is not buildable". But the Multibranch job works perfectly well on its own and there are no problems with it.
Is "Build other projects" post build action (downstream project) not compatible with Jenkins pipelines? What am I missing here?
I have found a temporary solution, where it is required that I need to mention 'Multibranch_Pipeline_Job/Sub_Job_Name', where Sub_Job_Name is the name of one of the many jobs in the Multibranch job.
Although this works out well, it is not a very feasible solution. If a Multibranch job has many branches containing Jenkinsfiles, it is required that all these sub-jobs be mentioned separately in "Projects to Build" column.
If there is more viable solution where it is possible to execute the Multibranch pipeline job entirely (all the sub-jobs), please answer here. It would be much appreciated.

How to enable SCM polling with the Jenkins Pipeline plugin

This is a question related to How to make SCM polling work with the Jenkins Workflow plugin. That thread answers how to use SCM polling in a pipeline script once SCM polling is enabled but does not cover how to enable SCM polling.
For example, if you wanted to use the mulit-branch pipeline plugin to create jobs automatically using a Jenkinsfile there is not a way I know of to have the "Poll SCM" option enabled in the job. This makes it difficult to provision on-demand environments such as creating a docker container that has the jobs setup from the beginning. Because you would have to sign-in to Jenkins and go to the configuration and select the "Poll SCM" option once the container was started. Cloudbees offers a template plugin to help solve this problem.
However this is not available to Jenkins using the free version. Is there any workaround or solution for users on the free version of Jenkins?
if you wanted to use the multi-branch pipeline plugin to create jobs automatically using a Jenkinsfile there is not a way I know of to have the "Poll SCM" option enabled in the job
Nor is any needed. Multibranch projects have a configurable polling interval for the branch indexing as a whole, which also serves as a per-branch build trigger, and will also receive webhooks automatically.
To answer the question how to enable SCM polling, you need to do the following.
Using the Pipeline Syntax generator and "properties: Set job properties" you can generate the following which will enable SCM polling.
properties([pipelineTriggers([pollSCM('H * * * *')])])
However as Jesse Glick points out for Multibranch pipelines you don't need to enable SCM polling.
I am thinking about the same problem.
If you are using a online Git service like Github or Bitbucket, I think you could use their Webhooks features to solve it. I have not been able to test the solution yet, but it should work.
In your Multibranch Pipeline configuration, enable the Trigger builds remotely option.
Then you need to enable your Github/Bitbucket Webhook on your repository, using the path (as described in the Jenkins configuration descrition): JENKINS_URL/job/test/build?token=TOKEN_NAME
In order to get my Bitbucket to connect to the web hook, I had to add the following to my declarative pipeline:
pipeline {
stages {
stage('Initialize') {
steps {
//enable remote triggers
script {
properties([pipelineTriggers([pollSCM('')])])
}
//define scm connection for polling
git branch: BRANCH_NAME, credentialsId: 'my-credentials', url: 'ssh://git#stash.server.fqdn/stash/my-project.git'
}
}
}
}
This allows to rebuild a branch, without scanning the entire multibranchiverse. This is especially valuable, when using Bitbucket Project /Github Team-multibranch projects. There a scan can take a few minutes, once you have a few repos/branches.
By directly being able to hook into the branch, you can get a build result much faster, and without any side effects.
NB: In a declarative pipeline the properties call has to be wrapped by a script-block.

Resources