SCM polling with includedRegions in Jenkins pipeline job - jenkins

I am using GitLab. Given multiple directories, e.g. folder1, folder2, folder3. I would like the job run only if there is any changes under folder1 only. Does anyone able to get includedRegions to work in Jenkins pipeline job.
checkout(
[
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PathRestriction', excludedRegions: '', includedRegions: 'folder1/.*']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'user', url: 'ssh://something/experiment.git']]
]
)

If you use a pipeline the checkout step performs after the build was launched.
The only way to manage this is to check the changeset with the groovy and skip the build if there were no changes at the included directory.

I managed to get it working. Since the checkout will be used across projects in our Jenkins, I wrote shared libraries to make it handy to use.
Example: strToSparseCheckout.groovy
#!/usr/bin/env groovy
import hudson.plugins.git.extensions.impl.SparseCheckoutPath
def call(paths) {
def list = paths.split('\n')
def sparsePaths = []
def isDefaultAdded = false
list.each {
def path = it - '/.*'
def sparsePath = new SparseCheckoutPath(path)
sparsePaths.push(sparsePath)
}
return sparsePaths
}
Example: sparseCheckout.groovy
#!/usr/bin/env groovy
def call(Map namedargs) {
checkout(
[
$class: 'GitSCM',
branches: [[name: "${namedargs.branch}"]],
extensions: [
[$class: 'LocalBranch', localBranch: '**'],
[$class: 'RelativeTargetDirectory', relativeTargetDir: "${namedargs.target_dir}"],
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: strToSparseCheckout("${namedargs.included_regions}")],
[$class: 'PathRestriction', excludedRegions: """${namedargs.excluded_regions}""", includedRegions: """${namedargs.included_regions}"""]
],
userRemoteConfigs: [[credentialsId: 'user', url: "${namedargs.url}"]]
]
)
}
In the pipeline, we can call the sparseCheckout shared library.
pipeline {
stage('Checkout') {
sparseCheckout(
[
url: 'ssh://path.to.git/something.git',
branch: 'refs/heads/master',
target_dir: 'something',
excluded_regions: "${params.excluded_regions}",
included_regions: "${params.included_regions}"
]
)
}
}

Related

Trigger a build in pipeline script at SCM Change of a special file

Project Folder is an external in a svn checkout. In this folder is a file, what should be used as trigger for a overnight build. Just on SVN change of this file a build should be triggerd. I used the option with includedRegions, but I reached no success. What could be false?
stage('Checkout AppBuilder Repository'){
steps{
checkout([$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: true,
includedRegions: '.*/Aenderungslisten.vi/.*',
locations: [[cancelProcessOnExternalsFail: false,
credentialsId: "${env.SVN_CRED_ID}",
depthOption: 'infinity',
ignoreExternalsOption: false, local: './AppBuilder',
remote: "${env.APP_BUILDER_URL}"]],
quietOperation: false,
workspaceUpdater: [$class: 'CheckoutUpdater']])
}
}

Is it possible to have a Jenkins parameter's name with spaces in it?

I'm trying to make my Jenkins UI more clean.
My Jenkins file calls a function which in turn runs the following:
properties ([
[$class: 'GitLabConnectionProperty', gitLabConnection: 'GitlabConnection'],
[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: '', name: 'activateInTest'],
[$class: 'ChoiceParameterDefinition', choices: 'false\ntrue\n', description: 'If running newBuild, skip unit tests', name: 'skipUnitTests']
]]
])
Currently, I can access these parameters like this:
if(activateInTest == 'true') {
//Do something
}
After going through other docs and examples. It looked as if I could also access parameters by doing something like params.activateInTest, which did not work. I also tried doing something like params["activateInTest"], but that didn't work either.
The reason I want to access it this way params["..."], is because I would like to have the name of my parameter be "Activate in Test" rather than "activateInTest".
In this example I see the person does use "BooleanParameterDefinition" with spaces in the name. But I can't seem to figure out how to use spaces in the name. Having spaces in the name is my only goal here.
yes, its possible, just use following notation:
${params['Name with space']}
tested on old Jenkins: 2.149
Indeed it is possible, user "string reference" to access it, i.e. params."Activate in Test"
For example:
properties([parameters([
string(name: 'Activate in Test', defaultValue: 'default value')
])])
echo params."Activate in Test"
In Java and Groovy space in a variable does not support! and it's not recommended but Jenkins supports it with 'String referencing'
But If you want to decorate the parameter Display Name it would be something like this
Jenkins Declarative Pipeline
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
echo "Biography: ${params.BIOGRAPHY}"
echo "Toggle: ${params.TOGGLE}"
echo "Choice: ${params.CHOICE}"
echo "Password: ${params.PASSWORD}"
}
}
}
}
Scripted Pipeline
node {
properties(
[
parameters(
[string(defaultValue: '/data', name: 'Directory', description: "Directort Path"),
string(defaultValue: 'Dev', name: 'DEPLOY_ENV', description: "Deploy Environment")
]
)
]
)
stage('debug') {
echo "${params}"
}
}

Parameterized Jenkins Pipeline Properties: Can't use parameters on properties section

I am trying to set properties by jobs actual name.
My pipeline header looks as follows:
def jobs_actual_name = [
choice(name: 'var', choices: ['choice1', 'choice2', 'choice3']),
]
properties([
parameters(jobs_actual_name)
])
The upper pipeline works fine but I want to use a parameter instead of directly giving the name.
Like:
def jobs_actual_name = [
choice(name: 'var', choices: ['choice1', 'choice2', 'choice3']),
]
def another_jobs_actual_name = [
choice(name: 'var', choices: ['choice1', 'choice2', 'choice3']),
]
properties([
parameters("${env.JOB_BASE_NAME}")
])
How would I do this? Thanks in advance.
You can have some logic execute before setting the properties:
def actualParameters = (env.JOB_BASE_NAME == 'jobs_actual_name' ? jobs_actual_name : another_jobs_actual_name)
properties([
parameters(actualParameters)
])

How to stop Jenkins polling SVN source control?

I have a Jenkins server building our project and there's a data section that gets checked out from source control (SVN). I changed it to try to stop it being included in source control polling (poll: false) however I can see changes to the repository are still triggering builds.
Have I got the syntax correct here? I added "poll: false" but it's being ignored.
stage('Data Checkout')
{
dir('E:\\workspace\\data')
{
checkout([
poll: false,
scm: [$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [[credentialsId: 'xxx',
depthOption: 'infinity',
ignoreExternalsOption: true,
local: 'local',
remote: "svn://xxx/data"]],
workspaceUpdater: [$class: 'UpdateUpdater']]])
}
}

DynamicReferenceParameter defined in Pipeline

I use the DynamicReferenceParameter for some advanced parameter inputs in Jenkins.
Here is a tiny example, which I entered using the UI:
This works fine, my checkboxes and input fields do show up!
But when I try to define my parameters in my Jenkinsfile, I get an error. My Jenkinsfile looks like this:
properties([parameters([
[
$class: 'DynamicReferenceParameter',
name: 'SFM',
script: [
$class: 'GroovyScript',
fallbackScript: '',
script: """
def services = ['service1',
'service2',
'service3']
def html =
'''
<!DOCTYPE html>
<html>
<body>
<table id="serviceTable">
'''
for (service in services){
html += "<tr>"
html += "<td><input type=\"checkbox\" id=\"checkbox_$service\">$service</td>"
html += "<td><div id=\"version_$service\" >version: <input type=\"text\"></div></td>"
html += "</tr>"
}
html += '''
</table>
</body>
</html>
'''
return html
"""
]
]
])])
You see, I just copy&pasted the script from the UI input and surrounded it with """.
This results in error no. 1:
Groovy.lang.MissingPropertyException: No such property: service for class: WorkflowScript
So there is an error parsing my variables.
So I just tried to delete all variables and set static values (replace $service with service1). This results in error no. 2:
java.lang.ClassCastException: org.biouno.unochoice.model.GroovyScript.script expects class org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript but received class java.lang.String
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:416)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:340)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:281)
Caused: java.lang.IllegalArgumentException: Could not instantiate {fallbackScript=, script=[...]
What do I do wrong?
script and fallbackScript are not really String's. They are using the Security Script Plugin classes (it's been like that for a while already).
Here's some code snippet that may help.
properties([parameters([
[
$class: 'DynamicReferenceParameter',
name: 'TEST',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [], sandbox: true, script: ''
],
script: [
classpath: [], sandbox: true, script:
"""
def html =
'''
<!DOCTYPE html>
<html>
<body>
<table id="serviceTable">
<tr>
<td><input type="checkbox" id="checkbox">service1</td>
<td><div id="version" >version: <input type="text"></div></td>
</tr>
</table>
</body>
</html>
'''
return html
"""
]
]
]
])])
ps: take it with a grain of salt, as I normally use only FreeStyle jobs. I think some people tried using the plug-in with pipelines, but as far as I know, it's still a work-in-progress (see https://issues.jenkins-ci.org/browse/JENKINS-39742 for example)
Hope that helps,
Bruno
properties([parameters([
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
omitValueField: true,
description: 'Editable field when PARENT_PARAM is Others',
name: 'ACTIVE_PARAM',
randomName: 'choice-parameter-5631314456178624',
referencedParameters: 'PARENT_PARAM',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: "return['undefined']"
],
script: [
classpath: [],
sandbox: true,
script:
"""
inputBox="<input class='setting-input' name='value' type='text' value=''>"
"""
]
]
]
])])

Resources