Jenkins groovy pipeline - git checkout error - jenkins

I have defined a simple Jenkins pipeline using the Groovy DSL. My intention is that it will simply checkout my git repository. However I am getting an error.
Given my groovy DSL defintion:
stage 'build'
node
{
git branch: '*/mybranch', credentialsId: 'my credentials', url: 'git#git.servername:pathto/myrepo.git'
}
I'm expecting the Jenkins pipeline to simply checkout my git repository.
However I'm getting the following error:
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE
Can anyone help me in resolving this issue please?

The more verbose version works:
checkout([$class: 'GitSCM', branches: [[name: '*/mybranch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'myCredentials', url: 'git#git.servername:pathto/myrepo.git']]])
However it would be nice to use the short-hand version.

Without the */ on the branch specifier works for me.
Try this:
git branch: 'mybranch', credentialsId: 'my credentials', url: 'git#git.servername:pathto/myrepo.git'

Related

how to override default Jenkins Git plugin checkout with pipeline code?

I have jenkins multibranch pipeline with jenkins git plugin.
When the new pull requested is created a new PR job starts, and checkout of the repository is done automatically. The problem is sometimes it hits timeout (networking).
I try to do retry in pipeline by using GitSCM code with some conditionals:
checkout([
$class: 'GitSCM',
branches: scm.branches,
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
submoduleCfg: [],
userRemoteConfigs: scm.userRemoteConfigs
])
}
It repeats the checkout just fine, but I still need to disable the first default checkout from the plugin(if it fails the job fails). How do I do that? How do I override the built-in checkout?
skipDefaultCheckout option should disable default checkout. E.g.:
options { skipDefaultCheckout() }
Read more here about it: https://www.jenkins.io/doc/book/pipeline/syntax/#available-options

Jenkins Pipeline checkout branch which can parameterised

I am trying to write a stage in Jenkinsfile where I'll pass the branch name from the Jenkins job to checkout the code at a specific location.
stage("Prepare") {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '*/master' ]],
extensions: scm.extensions,
userRemoteConfigs: [[
url: 'https://gitlab.example.com/user/example_repo.git',
credentialsId: 'my-gitlab-repo-creds'
]]
])
}
}
Also, how can we define the location where to checkout the project.
You're missing the power of the SCM checkout step. In turn, your pipeline is missing some important configuration to get what you want:
pass the branch name from the Jenkins job to checkout the code
define the location where to checkout the project
This can all be done. I'll explain the options used to accomplish this.
1) Pass the branch name to checkout step
You can achieve this with the BRANCH_NAME environment variable.
2) Define project checkout location
Add the following extension and get rid of the scm.extensions value.
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'myrepo']]
Specify a local directory (relative to the workspace root) where the Git repository will be checked out. If left empty, the workspace root itself will be used.
For the branches option,
The safest way is to use the refs/heads/ syntax. This way the expected branch is unambiguous.
For example:
branches: [[name: 'refs/heads/${env.BRANCH_NAME}']]
Piecing it all together,
checkout(
[
$class: 'GitSCM',
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'example_repo']],
branches: [[name: 'refs/heads/${env.BRANCH_NAME}']],
userRemoteConfigs: [
[
url: 'https://gitlab.example.com/user/example_repo.git',
credentialsId: 'my-gitlab-repo-creds',
name: 'origin'
]
]
]
)
The above code will checkout the ${env.BRANCH_NAME} branch of https://gitlab.example.com/user/example_repo.git to the $WORKSPACE_DIR/example_repo folder.

Checkout a specific folder from git using jenkins groovy "checkout" command

I'm pretty new to jenkins and groovy and I'm trying to do a sparse checkout in my jenkins file. Currently I simply do this:
stage('Check out branch from Gitlab'){
echo 'Pulling...' + env.BRANCH_NAME
checkout scm
}
I wish to execute a sparse checkout from a Jenkins Groovy script and I'm struggling to find a good way of doing this. Is there a way of using the "checkout" command to do this?
You should configure a set of parameters for the GitSCM more info here
A basic configuration is presented as an example below:
pipeline {
agent any
stages {
stage ("Git Checkout"){
steps {
script {
checkout([
$class: 'GitSCM',
branches: [[name: "devel"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: "/tmp/jenkins/devel"
]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: 'jenkinsCredentialsId',
url: 'https://git.example.com/git/example'
]]
])
}
}
}
}
}
I attached a fully working Jenkins pipeline of one stage. It checks out the branch devel of the repository https://git.example.com/git/example on directory /tmp/jenkins/devel. Also please note that you should add (if not already done) the credentials of the repository in Jenkins Credentials (/jenkins/credentials/), in the above example is under id jenkinsCredentialsId
You can read the link for GitSCM to find out more details and attributes that you can configure.

How to write 'Git LFS pull after checkout' settings in the Jenkins pipeline

I created a configuration file with Jenkins file to configure the Jenkins pipeline.
An error occurred when doing git pull.
The cause was that the groovy file did not have the Git LFS pull after checkout setting.
I do not know how to write Git LFS pull after checkout setting to groovy.
git(
 url: git#...,
 branch: "master",
 credentialsId:"abcdefg"
)
// Git LFS pull after checkout setting??
Here's how I was able to use the Git plugin in the pipeline. Refer to the documentation here for more info:
checkout([ $class: "GitSCM",
branches: [[name: "refs/heads/${your branch name}"]],
extensions: [
[$class: "GitLFSPull"]
],
userRemoteConfigs: [
[credentialsId: "${your git credential ID}",
url: "${your git URL}"]
]
])
Considering you are trying to download a large file, you may want to increase the time out limit too (default is set to 10 minutes):
checkout([ $class: 'GitSCM',
branches: [[name: 'refs/heads/'+"${branch_or_tag}"]],
extensions: [[$class: 'GitLFSPull']]
+[[$class: 'CloneOption', timeout: 30]],
userRemoteConfigs: [
[credentialsId: "${your git credential ID}",
url: "${your git URL}"]
]
])

Jenkins : Copy artifacts from Multibranch pipeline

I am new to Jenkin's and I have 4 repo's in Bitbucket say A,B,C,D.
I have to fetch the A,B & C repos, build them using gradle build which will generate wars.
Now I have to copy those wars in D\warsFolder
I have created Multibranch pipeline and generated the pipeline syntax which fetches A,B & C from git and builds them. Looks some thing like this
node {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'A']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../A.git']]])
dir('A') {
bat 'gradle build -i --info --stacktrace --debug'
}
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'B']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../B.git']]])
dir('B') {
bat 'gradle build -i --info --stacktrace --debug'
}
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'C']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../C.git']]])
dir('C') {
bat 'gradle build -i --info --stacktrace --debug'
}
}
added the above script in Jenkinsfile which I placed in A repo.
Now I have created a Multibranch pipeline Fetch_all and in branch sources -> Single repository & branch -> Repository URL I have added http://.../A.git (which has Jenkinsfile).
Upto here everything is working fine I am able to fetch the sources and build them.
I have created new job of Freestyle where in Source Code Management -> Git -> Repository URL will be http://.../D.git.
I am trying to copy the wars generated in the Fetch_all pipeline but in Build -> Copy artifacts from another project the Project Name is not accepting the Multibranch pipeline. It is throwing error like
ERROR: Unable to find project for artifact copy:
This may be due to incorrect project name or permission settings; see help for project name in job configuration.
Any help is appreciated.
Finally got it, when I gave pipeline_name/branchname i.e., Fetch_all/%00 it worked fine.
It took some time to find out the correct syntax. The documentations of the Coyartifact Plugin is a little bit confusing, as it mentions the encoding of special characters. Actually spaces don't have to be encoded, but slashes have to.
The Jenkinsfile which copies artifacts is located at 'Other-folder/Multi branch Pipeline Test/', put in this content to copy the artifact of the last successfull build of the 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy' project
copyArtifacts(
projectName: 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy',// the name of project, as you find it from the root of jenkins
selector: lastSuccessful(), // selector to select the build to copy from. If not specified, latest stable build is used.
filter: 'projects/Output/myzip.zip', // ant-expression to filter artifacts to copy, Attention! Filter is case sensitive
target: 'sources/deploy/', // target directory to copy to, intermediate folders will be created
flatten: true, // ignore directory structures of artifacts, Artifact will be placed at 'sources/deploy/myzip.zip'. Is the option false, you find it at 'projects/Outpu/myzip.py'
optional: false, // do not fail the step even if no appropriate build is found.
fingerprintArtifacts: true, // fingerprint artifacts to track builds using those artifacts
)
And don't forget to allow artifact copy in the project you want to take the artifact from. Add this to the Jenkinsfile of 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy'. Use absolute paths, to avoid issues if you move some projects around.
options {
disableConcurrentBuilds()
timeout(time: 30, unit: 'MINUTES')
copyArtifactPermission('/Other-folder/Multi branch Pipeline Test/*, /second Folder/*') // allow all the projects or branches of 'Other-folder/Multi branch Pipeline Test' and 'second Folder' to copy artifacts of this job
} // end of options

Resources