Branch of Jenkinsfile - jenkins

I'm using regular (not multibranch) pipeline job which getting Jenkinsfile from repository. And I would like to checkout the same branch in my script. Currently I could use job name as branch name:
checkout(scm: [$class: 'GitSCM', branches: [[name: env.JOB_BASE_NAME]], ...)
But it is not always convenient and it is still 2 places which should be changed - job name and branch name which is error prone. I only found open issue and suggestions to use multibranch pipeline which is overkill in my case.
Is there a way to access branch name used to checkout Jenkinsfile?

If I am getting this right , all you need to do is get the job a parameter and pass that parameter in the checkout step :
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: "${env.PARAM}"
Here PARAM is the name of the parameter you added in the build configuration.

Related

Jenkins save author of commit and listen for pushes

Hi I want to ask if there is possible to jenkins pipeline run every time when there is push into some repository in git. and save the author of commit into variable. My code:
stage('checkout') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxxxxxxxxxxxxx-yyyyyyyy-zzzzzzzzzz', url: 'git#website:group/project.git']]])
}
}
To your 1st question: "is possible to Jenkins pipeline run every time when there is push into some repository in git"
answer: Yes there are various way.
To make use of Webhooks. Many SCM provider like Github , BitBucket has this option
you can make use of Jenkins git plugins to do that. (refer my screen shot)
For your 2nd question: "Save the author of commit into a variable. "
answer: It seems to duplicate question with this

In Jenkins, how can I set the "Branches to build" configuration option of the Pipeline plugin identical to the value of ${BRANCH_NAME}

As you can see in the attached screenshot, I want my pipeline to fetch the Jenkinsfile from the same branch that is passed in as a parameter and saved in the BRANCH_NAME variable:
I had to uncheck "Lightweight checkout" to make Jenkins parse the ${BRANCH_NAME} parameter under the Pipeline SCM settings:
See below:
checkout([$class: 'GitSCM', branches: [[name: "${BRANCH_NAME}"]], [[credentialsId: 'CRED_ID', url: 'https://url.goes.here']]])
To be more explicit, you could use ${params.BRANCH_NAME}

I want to checkout the 2nd Repo in my Jenkins multibranch pipeline

In my Jenkinsfile, the "checkout scm" command will checkout whatever repo I have configured in the configuration panel.
But what if I add a 2nd repo to the Jenkins file - is there any way to check that out to a sepcific directory within the workspace? The catch is that I don't want to hard-code any URLs into my Jenkinsfile. Here's an illustration of what I'm trying to achive:
stage("Checkout") {
checkout scm // Works fine, checks out the 1st consifured repo to workspace.
dir("src") {
checkout scm // Checks out the exact same repo again, but how can I change this to colone the 2nd repo instead?
}
}
Basically - what could I put instead of the 2nd "checkout scm" that would make it pull the 2nd repo configured in the Multibranch pipeline web config?
And supposing this isn't actually possible - what's even the point of allowing users to provide more than one repo in the config-form if there's no way of checking it out in the script?
Use the url found at yourjenkinshostname.com/pipeline-syntax/ to generate a step for "checkout: General SCM". After that, fill out the info for the repo you want, and click "Additional Behaviors" and add one for "Checkout to Subdirectory".
Lastly click "Generate Pipeline Script". The output from that should be useable in your Jenkinsfile. Completed, the process looks like this:
Syntax Generator Example
Alternatively, if you're used to the checkout step, the "RelativeTargetDirectory" extension class can be used to do this. A checkout step with that included looks like this:
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'test-dir']], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/jenkinsci/puppet-jenkins.git']]])
The key part being...
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'test-dir']]
EDIT: According to issues.jenkins-ci.org/browse/JENKINS-32018, the multiple sources of a multibranch job is not for two different repositories, but rather for multiple sources of a single repository.
You'll need to hardcode in URLs, I'm afraid. On approach is to have two multibranch jobs. One has the SCM as repo A and hardcodes a checkout of repo B, the other has repo B as the SCM, and hardcodes a checkout of repo A.

Jenkins - Multiple jobs for a single Project Issue

I am relatively new to Jenkins (using 2.32). So pardon my ignorance.
In my current setup, I have 2 free-style jobs for a single project - One point to production branch (/master) and another to the Dev branch (/dev). Bitbucket is configured to invoke (webhook) Jenkins on changes.
Once the dev is built and it passes all the unit test it gets deployed to Dev Server. Eventually, all dev changes are pushed to Master via pull request. The change in Master branch triggers the Master job and deploys the artifacts to productions.
I don't feel this setup is correct and would like you experts advise on this. Having 2 jobs makes me uncomfortable. What if I want a stage release? I will need another free-style job. Doesn't make much sense.
How do I go about doing this with one job? How do you guys achieve this? Using Pipeline? Any pointers would be greatly appreciated.
TIA.
You are correct, you can manage this better with Jenkins Pipeline
What you can do is the following :
1) Checkout the code from dev branch and put it in one directory in the workspace.
2) Compile and deploy from that directory.
3) Add a manual step for approval to deploy from master branch.
4) Repeat step 1 and 2.
A sample code would look something like this:
node {
// Get code from git repo
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: "origin/dev"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'test-dev-dir']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '<jenkins-github-credential-id>', url: 'https://github.com/test']]]
dir('test-dir') {
// Do your stuff
}
// stage concurrency: 1, name: 'approve'
// input id: 'master-deploy', message: 'Deploy from master?', ok: 'Deploy'
// Get code from git repo
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: "origin/master"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'test-master-dir']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '<jenkins-github-credential-id>', url: 'https://github.com/test']]]
dir('test-master-dir') {
// Preferbably create a tag for future hotfix maybe?
// Do your stuff
}
}

Clean builds with Multibranch Workflow

Using Multibranch Workflow, the command to check out looks like
checkout scm
I can't find a way to tell Jenkins to perform a clean checkout. By "clean," I mean it should remove all files from the workspace that aren't under version control.
I'm not sure if this answers the original question or not (I couldn't tell if the intention was to leave some files in the workspace) but why not just remove the workspace first, this would allow a clean checkout:
stage ('Clean') {
deleteDir()
}
stage ('Checkout') {
checkout scm
}
I run into the same problem and here is my workaround.
I created a new scm object for the checkout and extended the extensions with the CleanBeforeCheckout. But i kept the other configurations like branches and userRemoteConfigs.
checkout([
$class: 'GitSCM',
branches: scm.branches,
extensions: scm.extensions + [[$class: 'CleanBeforeCheckout']],
userRemoteConfigs: scm.userRemoteConfigs
])
It's still not perfect because you have to create a new object :(
First, you can not assume that a workflow job has a workspace as it was for freestyle jobs. Actually, a workflow job can use more than one workspace (one for each node or ws block).
Said that, what I'm going to propose is a kind of hacky: modify the scm object before checkout to set up a CleanCheckout extension (you will have to approve some calls there).
import hudson.plugins.git.extensions.impl.CleanCheckout
scm.extensions.replace(new CleanCheckout())
checkout scm
But I'd prefer Christopher Orr's proposal, use a shell step after checkout (sh 'git clean -fdx').
Behaviors can be added when configuring the source. clean before checkout, clean after checkout and Wipe out repository and force clone. This removes the need to add logic to the declarative / scripted pipelines.
Adding Christopher-Orr's comment as an answer to just do:
stage('Checkout') {
checkout scm
sh 'git clean -fdx'
}
Jenkins currently contains a page to generate groovy pipeline syntax. Selecting the checkout step you should be able to add all the additional options that you're used to.
I generated the following which should do what you want:
checkout poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://repo/location.git']]]

Resources