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.
Related
I have created job in jenkins to trigger a build whenever changes happen in github. But i want to know how to get the details of the user who made the changes to configure email notification.
Could anyone help me with the solution please?
You can use git show command to fetch the author ID and email using below command. Assign this to a variable and use it anywhere in the pipeline.
To Fetch Author ID: git show -s --pretty=%an
To Fetch Author email: git show -s --pretty=%ae
Pipeline script can be written as like below.
pipeline {
agent any
options {
timestamps()
}
stages {
stage('Test Stage') {
steps {
checkout changelog: true, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/Sample_branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'TestCredentials', url: '']]]
script {
Author_ID=sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
Author_Name=sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
}
echo "${Author_ID} and ${Author_Name}"
}
}
}
}
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.
I'm creating this new Job based on pipeline on jenkins. I want my jenkinsfile to be on bitbucket reposiotry : Let's say my config file is on bitbucket.org/config.git
The job mission is to clean install a project bitbucket.org/myProject.git
How can I configure the pipeline so it will trigger if any push is made in bitbucket.org/myProject.git and following the steps defined in bitbucket.org/config.git?
I do not want to create multi-branch pipeline and I do not want my jenkins file to be on the same repository than my project to compile.
My current config is:
pipeline {
agent any
parameters {
string(defaultValue: '', description: 'URL', name: 'GIT_URL')
string(defaultValue: '', description: 'Credential', name: 'CREDENTIAL_ID')
}
stages {
stage ('Initialize') {
steps {
git branch: 'develop', credentialsId: "${params.CREDENTIAL_ID}", url: "${params.GIT_URL}"
}
}
stage ('Build') {
steps {
sh 'mvn clean install '
echo 'build'
}
}
}
You can use shared Libraries in Jenkins. you would still need a Jenkinsfile in your code, but that would not contain any logic. It would simply refer the shared library and pass any params like git repo path.
For more information on shared libraries please refer this link https://jenkins.io/doc/book/pipeline/shared-libraries/.
For triggering the build, you can define a trigger in your pipeline. Example :
triggers {
pollSCM('H/5 * * * *')
}
or use webhooks if you don't want to poll.
Actually, i managed to make it work.
In my jenkins pipeline, i activated "Build when a change is pushed to BitBucket".
node {
checkout([$class: 'GitSCM',
branches: [[name: 'feature/test-b']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption', disableSubmodules: false,
parentCredentials: false, recursiveSubmodules: true, reference: '',
trackingSubmodules: false]], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'admin',
url: 'http://localhost:7990/scm/bout/boutique-a.git']]])
}
When a change is made in boutique-a in the branch 'feature/test-b' my job is triggered which is cool.
Now i have this other issue, how can i trigger when change are made in feature/*
It looks like i cannot access to env.BRANCH_NAME when im not in a 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
I'm exploring Jenkins 2.0 pipelines. So far my file is pretty simple.
node {
stage "checkout"
git([url:"https://github.com/luxengine/math.git"])
stage "build"
echo "Building from pipeline"
}
I can't seem to find any way to set the directory that git will checkout to. I also can't find any kind of documentation related to that. I found https://jenkinsci.github.io/job-dsl-plugin/ but it doesn't seem to match what I see on other tutorials.
Clarification
Looks like you are trying to configure Pipeline job (formerly known as Workflow). This type of job is very distinct from Job DSL.
The purpose of Pipeline job is to:
Orchestrates long-running activities that can span multiple build slaves. Suitable for building pipelines (formerly known as workflows) and/or organizing complex activities that do not easily fit in free-style job type.
Where as Job DSL:
...allows the programmatic creation of projects using a DSL. Pushing job creation into a script allows you to automate and standardize your Jenkins installation, unlike anything possible before.
Solution
If you want to checkout your code to specific directory then replace git step with more general SCM checkout step.
Final Pipeline configuration should look like that:
node {
stage "checkout"
//git([url:"https://github.com/luxengine/math.git"])
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'checkout-directory']],
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://github.com/luxengine/math.git']]])
stage "build"
echo "Building from pipeline"
}
As a future reference for Jenkins 2.0 and Pipeline DSL please use built-in Snippet Generator or documentation.
This can be done by using the directive of dir:
def exists = fileExists '<your target dir>'
if (!exists){
new File('<your target dir>').mkdir()
}
dir ('<your target dir>') {
git url: '<your git repo address>'
}
First make clear that you are using Jenkins Job DSL.
You can do this like this:
scm {
git {
wipeOutWorkspace(true)
shallowClone(true);
remote {
url("xxxx....")
relativeTargetDir('checkout-folder')
}
}
}
https://jenkinsci.github.io/job-dsl-plugin/
This above address gives you the chance simply to type in upper left aread for example 'scm' and than it will show in which relationships 'scm' can be used. Than you can select 'scm-freestylejob' and afterwards click on the '***' than you can see the details.
The general start point for Jenkins Job DSL is here:
https://github.com/jenkinsci/job-dsl-plugin/wiki
You can of course ask here on SO or on Google Forum:
https://groups.google.com/forum/#!forum/job-dsl-plugin
pipeline {
agent any
stages{
stage("Checkout") {
steps {
dir('def exists = fileNotExists \'git\'') {
bat label: '', script: 'sh "mkdir.sh'
}
dir ('cm') {
git branch: 'dev',
credentialsId: '<your credential id>',
url: '<yours git url>'
}
}
} //End of Checkout stage
stage("TestShellScript") {
steps {
bat label: '', script: 'sh "PrintNumber.sh"'
}
}
}//End of stages
} // End of pipeline
Note: cat mkdir.sh
#!/bin/bash
#Create a directory
mkdir git
You are using the Pipeline Plugin, not the Job DSL Plugin. In the Pipeline Plugin, if you want to define something, where there is not yet a function available in the Pipeline syntax, you can define it yourself.