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.
Related
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.
In continuation to jenkins-pipeline-syntax-for-p4sync - I am not able to get the "Poll SCM" option work for my pipeline job.
Here is my configuration:
"Poll SCM" is checked and set to poll every 10 minutes
Pipeline script contains the following:
node ('some-node') // not actual value
{
stage ('checkout')
{
checkout([
$class: 'PerforceScm',
credential: '11111111-1111-1111-1111-11111111111', // not actual value
populate: [
$class: 'AutoCleanImpl',
delete: true,
modtime: false,
parallel: [
enable: false,
minbytes: '1024',
minfiles: '1',
path: '/usr/local/bin/p4',
threads: '4'
],
pin: '',
quiet: true,
replace: true
],
workspace: [
$class: 'ManualWorkspaceImpl',
charset: 'none',
name: 'jenkins-${NODE_NAME}-${JOB_NAME}',
pinHost: false,
spec: [
allwrite: false,
clobber: false,
compress: false,
line: 'LOCAL',
locked: false,
modtime: false,
rmdir: false,
streamName: '',
view: '//Depot/subfolder... //jenkins-${NODE_NAME}-${JOB_NAME}/...' // not actual value
]
]
]
)
}
stage ('now do something')
{
sh 'ls -la'
}
}
Ran the job manually once
Still, polling does not work and job does not have a "Perforce Software Polling Log" link like a non-pipelined job has when configuring the perforce source and Poll SCM in the GUI.
It's like the PerforceSCM is missing a poll: true setting - or i'm doing something wrong.
Currently I have a workaround in which I poll perforce in a non-pipelined job which triggers a pipelined job, but then I have to pass the changelists manually and I would rather the pipeline job to do everything.
edit: versions
jenkins - 2.7.4
P4 plugin - 1.4.8
Pipeline plugin - 2.4
Pipeline SCM Step plugin - 2.2
If you go to the Groovy snippet generator and check the "include in polling" checkbox, you'll see that the generated code includes a line item for it:
checkout([
poll: true,
As an aside, you may run into problems at the moment using ${NODE_NAME} in your workspace name. The polling runs on the master, so it might not properly find the change number of your previous build. If that's the case, I know a fix for it should be coming shortly.
After updating all the plugins to latest (as of this post date) and restarting the jenkins server - the polling appears to be working with the exact same configuration (job now has the poll log link).
I'm not sure what exactly resolved the issue - but I consider it resolved.
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
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
I have a pipeline job that uses the p4jenkins plugin to poll a perforce stream for changes. The polling itself does seem to work, but when polling, it keeps finding the same changelists as new over and over again.
The polling interval is set as:
H/2 * * * * # short interval for testing purposes
My Jenkins pipeline checkout step (using a shared global library):
def call(body) {
def config=[:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
//checkout from p4 based on provided stream
checkout([
changelog: true,
poll: true,
scm: [
$class: 'PerforceScm',
credential: 'my_p4_service_creds',
populate : [
$class: 'AutoCleanImpl',
delete: false,
modtime: false,
parallel: [
enable: false,
minbytes: '1024',
minfiles: '1',
path: '/my/path/to/local/p4/bin/p4',
threads: '4'],
pin '',
quiet: false,
replace: true
],
workspace: [
$class: 'StreamWorkspaceImpl',
charset: 'none',
format: 'jenkins-${NODE_NAME}-${JOB_NAME}',
pinHost: false,
streamName:
"//${config.streamDepot}/${config.stream}"
]
]
])
}
At first, I thought it maybe was picking up the same changes over and over because they were pending changelists, but I've also tried making sure the stream only contains changelists that can be synced directly to a workspace.
Any ideas?