Jenkins with parameterized build - jenkins

I am configuring a post commit hook and would like to be able to trigger a build on the branch that has been committed.
So far i have set up the post-commit hook file
curl http://jenkins.local:8080/git/notifyCommit?url=GITHUB_URL/REPO_NAME.git
Within Jenkins i have set
This build is parameterized
**String Paramenter**
Name: branch
Branches to build: $branch
How can i get jenkins to build the branch that has just been committed?

See http://<Your Jenkins>/job/<Your job's name>/api/:
Perform a build
If the build has parameters, post to this URL [http://<Your Jenkins>/job/<Your job's name>/buildWithParameters] and provide the parameters as form data.

Related

How to use PollOnController in Jenkins PlasticSCM

I am running my builds in Kubernetes agents, so poll SCM won't work since the agents will be removed once the build is completed.
I am using plastic SCM and I found there is an option called poll on the controller for it in the readme.
I am using the cm commands in my Jenkins file for the SCM environment variables.
Eg:-
scmvars = cm(
branch: "${Branch_Name}",
changeset: "${Change_Set}",
repository: "${Repo_Name}",
server: "${Plastic_Server_Url}",
cleanup: 'DELETE'
)
Later I print the variables as:
println scmvars.PLASTICSCM_BRANCH, scmvars.PLASTICSCM_CHANGESET_ID
But I didn't see the Poll on controller option in the pipeline syntax and in the documentation.

Jenkins multibranch pipeline with Jenkinsfile from different repository

I have a Git repository with code I'd like to build but I'm not "allowed" to add a Jenkinsfile in its root (it is a Debian package so I can't add files to upstream source). Is there a way to store the Jenkinsfile in one repository and have it build code from another repository? Since my code repository has several branches to build (one for each Debian release) this should be a multibranch pipeline. Commits in either the code or Jenkinsfile repositories should trigger a build.
Bonus complexity: I have several code/packaging repositories like this and I'd like to reuse the same Jenkinsfile for all of them. Thus it should somehow dynamically fetch the right Git URL to use. The branches to build have the same names across all repositories.
Short answer is : you cannot do that with a multibranch pipeline. Multibranch pipelines are only designed (at least for now) to execute a specific pipeline in Pipeline script from SCM style, with a fixed Jenkinsfile at the root of the project.
You can however use the Multi-Branch Project plugin made for multibranch freestyle projects. First, you need to define your multibranch freestyle configuration just like you would with a multibranch pipeline configuration.
Select this new item like shown below :
This type of configuration will behave exactly same as the multibranch pipeline type, i.e. it will create you a folder with the name of your configuration and a sub-project for each branch it automatically detected.
The implementation should then be a piece of cake :
Specify your SCM repository in the multibranch configuration
Call another build as part of your build/post-build as you would do in a standard freestyle project, except that you have to call a parameterized job (let's call it build-job) and give it your repository information, i.e. Git URL and current branch (you can use the pre-defined variables $GIT_URL and $GIT_BRANCH for this purpose)
In your build-job, just define either an inline pipeline or a pipeline script checked out from SCM, and inside this script do a SCM checkout and go on with the steps you need to build. Example of build-job pipeline content :
.
node() {
stage 'Checkout'
checkout scm: [$class: 'GitSCM', branches: [[name: '*/${GIT_BRANCH}']], userRemoteConfigs: [[url: '${GIT_URL}']]]
stage 'Build'
// Build steps...
}
Of course if your different multibranches projects need to be treated a bit differently, you could also use intermediate projects (let's say build-project-A, build-project-B, ...) that would in turn call the generic build-job pipeline)
The one, major drawback of this solution is that you will only have one job responsible for all of your builds, making it harder to debug. You would still have your multibranch projects going blue/red in case of success/error but you will have to go back to called build-job to find the real problem of your build.
The best way I have found is to use the Remote Jenkinsfile Provider plugin. https://plugins.jenkins.io/remote-file/
This will add an option "by Remote Jenkinsfile Provider plugin" under Build Configuration>Mode then you can point to another repo where the Jenkinsfile is. I find this to be a much better solution than the Pipeline Multibranch Defaults Plugin, which makes you store the Jenkins file in Jenkins itself, rather than in source control.
U can make use of this plugin
https://github.com/jenkinsci/pipeline-multibranch-defaults-plugin/blob/master/README.md
Where we need to configure the jenkinsfile on jenkins rather than having it on each branch of your repo
I have version 2.121 and you can do this two ways:
Way 1
In the multibranch pipeline configuration > Build Configuration > Mode > Select "Custom Script" and put in "Marker File" below the name of a file you will use to identify branches that you want to have builds for.
Then, below that in Pipeline > Definition select "Pipeline Script from SCM" and enter the "SCM" information for how to find the "Jenkinsfile" that holds the script you want to run. It can be in the same repo you are finding branches in to create the jobs (if you put in the same GitHub repo's info) but I can't find a way to indicate that you just use the same branch for the file.
Way 2
Same as above, in the multibranch pipeline configuration > Build Configuration > Mode > Select "Custom Script" and put in "Marker File" below the name of a file you will use to identify branches that you want to have builds for.
Then, below that in Pipeline > Definition select "Pipeline Script" and put a bit of Groovy in the text box to load whatever you want or to run some script that already got loaded into the workspace.
In my case, i have an escenario whith a gitlab project based on gradle who has dependencies on another gitlab preject based on gradle too (same dashboard, but differents commits, differents developers).
I have added the following lines into my Jenkinsfile (the one which depends)
stage('Build') {
steps {
git branch: 'dev', credentialsId: 'jenkins-generated-ssh-key', url: 'git#gitlab.project.com:root/coreProject.git'
sh './gradlew clean'
}
}
Note: Be awark on the order on the sentences.
If you have doubt on how to create jenkins-generated-ssh-key please ask me

How can I pass “build-step parameters” to a downstream project (freestyle project with parameterized triggers)?

I have a Jenkins setup with two freestyle-projects linked via Jenkins upstream/downstream post-build triggers (Jenkins Parameterized Trigger plugin). The upstream project builds something that is uploaded somewhere. And the downstream project picks that new thing up and tests it.
At the moment the downstream project finds the build by date by looking for “the latest”. I want to change that.
I would like to change upstream to generate a random string as filename in the build step and pass that to downstream as parameter. How can I do that?
(I know how to generate a random string – the point is: how to pass build-step information down to the “downstream job”?)
In your upstream pipeline script, add this:
build job: 'your-downstream-job', parameters: [[$class: 'StringParameterValue', name: 'YOUR_STRING_PARAM', value: "${yourRandomGeneratedStringVariable}" ]]
And your your-downstream-job should be e.g. a normal job which is parameterized (check the tick at This project is parameterized), with the StringParameter YOUR_STRING_PARAM.
Then just read the value of this environment variable and you can go on.

how to configure job in jekins if first project is build successful then trigger another project and so on with "Multi-configuration project" job

I want to trigger project in jekins if first project is build successful then trigger another project and so on.. how can do it. I hava project1 , project2, and project3, project4, project5 so on. here I am configuring "Multi-configuration project" job.
for exp:- project1->success then trigger project2->success then trigger project3
if any stage failed then doesn't trigger other project.This is "Multi-configuration project" job, because this single job, I am trigger on multiple slaves remotely.
A simple way to do this would be to define this build pipeline using the Pipeline plugin.
This pipeline script should give you the desired behaviour:
build job: 'project1'
build job: 'project2'
build job: 'project3'
build job: 'project4'
build job: 'project5'
i.e. A build of each job will be started in sequence; if any one of them fails, then the subsequent jobs will not be started.
Even with "Multi-configuration project" you should be able to set a post-build action "Trigger parameterized build on other projects". Then you can specify condition which will define when this project should be build, and add parameters, including "build on the same node".

Jenkins: Post build actions on conditions

I want to run some post build actions in my Jenkins job on condition if a string or a regular expression is present in the console log. Any plugin available to do this?
One solution is to use the LogParser plugin and to create some regexp rules to parse your log.
This plugin can change the build status to Unstable:
Next, you create a downstream job and you will use the Parameterized Trigger plugin to pass the build status from the upstream to the downstream job.
If the status is unstable (= the LogParser find something), then you will execute some specific post build actions.
You can use the Conditional BuildStep plugin to condition the downstream build step according to the upstream build status.

Resources