Jenkins property to clear previous SVN checkout - jenkins

Looking for the setting that makes jenkins remove previously checked out code, and check out a fresh / latest version of the code before the build.

yes, that's possible. i generated the following step from the "Pipeline Syntax" link in the left nav of each of my pipeline jobs in jenkins. Sample step is "Checkout: General SCM", SCM is "Subversion" and from there you can select a "Check-out strategy" of "Always check out a fresh copy".
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[credentialsId: '196ff1ff-c481-4d2e-922b-e32410f8ee13', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'https://mycompany.example.com/svn/MYREPO/crs/trunk']], workspaceUpdater: [$class: 'CheckoutUpdater']])
the key part is:
workspaceUpdater: [$class: 'CheckoutUpdater']
If you're not familiar with declaring how your jobs will build using a Jenkinsfile yet (pipeline functionality), check out https://jenkins.io/doc/book/pipeline/. Here's what that "Pipeline Syntax" link looks like in the nav:

Related

Jenkins checkout scm step

I'm new to Jenkins and I'm trying to understand the following step in Jenkins pipeline line by line:
checkout scm
dir("some_directory") {
checkout(
changelog: false,
poll: false,
scm: [
$class : 'GitSCM',
branches : [[name: SOME_BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
extensions : [[$class: 'CloneOption', depth: 0, honorRefspec: true, reference: '', shallow: false]],
submoduleCfg : [],
userRemoteConfigs : [[url: SOME_URL]]
]
)
sh 'pwd; ls'
}
From the research I've done I understood that
checkout scm dir("some_directory")
'dir' creates a folder in workspace if it doesn't exist, and git project gets checked out into this directory
checkout(
changelog:false,
poll:false,
scm: [...]
)
this block of code specifies git parameters of the git project that is being checked out into the directory specified above.
This is so far what I understood, can someone please lt me know if my understanding is correct? And possibly add more details to it.
Also, I am confused with the current code syntax. Would it make any difference if I rewrite the top few lines as:
checkout scm dir("some_directory")(
changelog: false,
poll: false,
etc.
)
instead of using 'checkout' two times.

How to set up a pipeline for specific branch

I have a Java project in http://localhost:7990/scm/bout/boutique-a.git
I want to have 2 Jenkins pipeline jobs:
Job 1/ trigger on commit done on */develop
Job 2/ trigger on commit done on any */feature
p
each job will do a basic mvn install, mvn test, sonar ...
a simple script with
node {
checkout([$class: 'GitSCM',
branches: [[name: 'develop]],
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']]])
}
works if a commit is done in /develop or if I give explicitly the branch name like feature/test-a but how to configure a script for any feature/
It seems that what i'm asking is not possible using pipeline job.
I found a work arround for "feature/** ". I created a param BRANCH_NAME in the job, then the branch name is send by bitbucket when a push is made on "feature/** " through a basic POST request.
http://user:token#localhost:8081/jenkins/job/MY_JOB_NAME/buildWithParameters?token=U1C1yQo7x3&BRANCH_NAME=feature/branche-test

How to do a sparse checkout and update from SVN on Jenkins?

We have a large SVN repository and I would like to do a sparse checkout on Jenkins. I do understand the concept of sparse checkouts and having it locally I can get things to work as I would like to have it. Doing things on Jenkins however and running it repeatedly I can't get to work.
I have a repo structure as follows
trunk\file.txt
trunk\FolderA
trunk\FolderB
trunk\FolderC
trunk\FolderD
I would like to checkout and update
trunk\file.txt
trunk\FolderA
trunk\FolderB
but NOT
trunk\FolderC
trunk\FolderD
My pipeline code is as follows
checkout([$class: 'SubversionSCM',
additionalCredentials: [[credentialsId: strCredentialsId, realm: strRealm]],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '', filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [
[
remote: "${strRepoPath}/trunk",
local: "${softwarePath}",
depthOption: 'unknown',
credentialsId: strCredentialsId,
cancelProcessOnExternalsFail: true,
ignoreExternalsOption: false
],
[
remote: "${strRepoPath}/trunk/FolderA",
local: "${softwarePath}/FolderA",
depthOption: 'infinity',
credentialsId: strCredentialsId,
cancelProcessOnExternalsFail: true,
ignoreExternalsOption: false
],
[
remote: "${strRepoPath}/trunk/FolderB",
local: "${softwarePath}/FolderB",
depthOption: 'infinity',
credentialsId: strCredentialsId,
cancelProcessOnExternalsFail: true,
ignoreExternalsOption: false
]
],
quietOperation: false,
workspaceUpdater: [$class: 'UpdateWithCleanUpdater']])
Whenever I run this code the first time, everything looks as expected. However, when Jenkins runs it the following times, the UpdateWithCleanUpdater causes that FolderA and FolderB get first deleted and then checked out again. The result is still correct, however, it takes much longer that I would like it to take and longer than necessary.
I would like to keep the UpdateWithCleanUpdater because I want Jenkins to clean up files generated in the previous run.
Is there any solution to this using the Jenkins SVN plugin? How would I do this "manually", i.e. do a checkout the first time, clean up and only update the following times, and still do automatic Jenkins runs based on change detection on the SVN repo?
Thanks in advance!
Now that is a very narrow issue.
I'd say, either you replace the UpdateWithCleanUpdater with a manual script (by calling your local svn client; some examples here); or you could fork or contribute to the Jenkins SVN plugin.
On the trunk you have:
depthOption: 'unknown',
Shouldn't the value be 'empty'?
Yet, while what that might avoid the "FolderA and FolderB get first deleted and then checked out again", so I doubt that you would really get the "sparse checkout" in this way. It will be rather a "normal checkout into another SVN workspace".
As a workaround you can consider using svn:externals: Create a folder trunk/JenkinsWorkspace/trunk, and set svn:externals property with a content like:
^/trunk/FolderA FolderA
^/trunk/FolderB FolderB
^/trunk/file.txt file.txt
Then, in Jenkins you would just have a single checkout from ${strRepoPath}/JenkinsWorkspace.

Running Jenkins job after merge request in Gitlab

I'm trying to use a Gitlab web hook to trigger a job in Jenknis after pushing a commit/opening a merge commit using a pipeline script.
For some reason, Jenkins always checks out the master branch and builds it. How
can I specify which branch to build using the Groovy script?
I tried to use the environment variable from the Gitlab POST request, but it still always uses the master branch:
checkout changelog: false, poll: false, scm: [$class: 'GitSCM' , branches: [[name:'origin/${env.gitlabSourceBranch}']], browser: [$class 'GitLab', repoUrl: 'some-git-repo.com', version: 9.0], doGenerateSubmoduleConfiguration: false, extensions: [[$class: 'SubmoduleOption' disableSubmodules: false, parentCredentials: true, recursiveCredentials: true, recursiveSubmodules: true, reference: '', trackingSubmodules: false], [$class: 'PrebuildMerge', options: [fastForwardMode: 'FF', mergeRemote: '', mergeTarget: 'origin/${env.gitlabTargetBranch}']]], submodulecfg: [], userRemoteConfigs: [[credentialsId: '12345', url: 'git#some-git-repo.com:A/repo.git']]]
(I generated this command using the snippet generator)
You can use the "Generic Webhook trigger" plugin in Jenkins.
It allows you to get the branch name from the POST request sent by Gitlab, map it as a variable and use it inside your pipeline.
Configuration:
Inside the pipeline:
stage('Clone sources') {
steps {
git credentialsId: '...', poll: false, branch: ${branch}, url: '...'
}
}
Full example at: https://pillsfromtheweb.blogspot.com/2021/10/trigger-jenkins-on-merge-request-from.html

How to p4 unshelve from Jenkins 2 pipeline groovy script

I have a Jenkins2 pipeline (groovy) script, and I'd like to be able to (optionally) unshelve a shelf using the p4 SCM. I think I need something like this:
checkout(
[$class: 'PerforceScm',
credential: 'my-p4-credentials',
populate:
[$class: 'ForceCleanImpl',
have: false,
parallel: [enable: false,
minbytes: '1024',
minfiles: '1',
path: '/usr/local/bin/p4',
threads: '4'],
pin: p4shelf, // <--! this variable is the shelf CL
quiet: true],
workspace: [$class: 'TemplateWorkspaceImpl',
charset: 'auto',
format: 'jenkins-${NODE_NAME}-${JOB_NAME}',
pinHost: false,
templateName: p4branch]])
I dug through the p4-plugin github repo. Basically, this can't be done in one step. Instead, first, we must checkout from p4:
checkout([$class: 'PerforceScm', ...])
Then, we must do an unshelve operation:
p4unshelve resolve: '', shelf: shelf, credential: 'jnsmith-p4-credentials'
Works like gangbusters.

Resources