How can I use foreach with conditionalSteps in Jenkins Job DSL - jenkins

I'm trying to use the conditionalSteps add in with Jenkins Job DSL to conditionally trigger a build step. I want this step to trigger if any file in a given set exists. I am able to get this work by explcitly calling out multiple fileExists and an or. However I would like to dynamically create this using a foreach.
Here's what I have been playing with on http://job-dsl.herokuapp.com/
def files = ["file1", "file2", "file3"]
job('SomeJob') {
steps {
conditionalSteps {
condition {
/* This works fine:
or {
fileExists("file1.jenkinsTrigger", BaseDir.WORKSPACE)
}{
fileExists("file2.jenkinsTrigger", BaseDir.WORKSPACE)
}{
fileExists("file3.jenkinsTrigger", BaseDir.WORKSPACE)
}
*/
//But I want to create the Or clause from the array above
or {
files.each {
fileExists("${it}.jenkinsTrigger", BaseDir.WORKSPACE)
}
}
}
runner('Unstable')
steps {
gradle 'test'
}
}
}
}
The above gets
javaposse.jobdsl.dsl.DslScriptException: (script, line 17) No condition specified
and I have tried all manner of combinations to get this work without avail... any tips would be much appreciated

The or DSL method expects an array of closures. So you need to convert the collection of file names to an array of closure.
Example:
def files = ["file1", "file2", "file3"]
job('example') {
steps {
conditionalSteps {
condition {
or(
(Closure[]) files.collect { fileName ->
return {
fileExists("${fileName}.jenkinsTrigger", BaseDir.WORKSPACE)
}
}
)
}
runner('Unstable')
steps {
gradle 'test'
}
}
}
}

Related

Declarative dynamic parallel stages

I figure I’m doing something unorthodox here, but I’d like to stick to declarative for convenience while dynamically generating parallel steps.
I found a way to do something like that, but mixing both paradigms, which doesn’t seem to work well with the BlueOcean UI (multiple stages inside each parallel branch do not show up properly).
The closest I got was with something like this:
def accounts() {
return ["dynamic", "list"]
}
def parallelJobs() {
jobs = []
for (account in accounts()) {
jobs[] = stage(account) {
steps {
echo "Step for $account"
}
}
}
return jobs
}
# this is inside a shared library, called by my Jenkinsfile, like what is described
# under "Defining Declarative Pipelines in Shared Libraries" in
# https://www.jenkins.io/blog/2017/09/25/declarative-1/
def call() {
pipeline {
stages {
stage('Build all variations') {
parallel parallelJobs()
}
}
}
}
The problem is Jenkins errors like this:
Expected a block for parallel # line X, column Y.
parallel parallelJobs()
^
So, I was wondering if there is a way I could transform that list of stages, returned by parallelJobs(), into the block expected by Jenkins...
Yes, you can. You need to return a map of stages. Following is a working pipeline example.
pipeline {
agent any
stages {
stage('Parallel') {
steps {
script {
parallel parallelJobs()
}
}
}
}
}
def accounts() {
return ["dynamic", "list"]
}
def parallelJobs() {
jobs = [:]
for (account in accounts()) {
jobs[account] = { stage(account) {
echo "Step for $account"
}
}
}
return jobs
}

How to send a referencedParameter to a readFileFromWorkspace through activeChoiceReactiveParam

I'm trying to send a referencedParameter ('product') to a Groovy script (services.groovy) that is triggered by the command readFileFromWorkspace that is wrapped with an activeChoiceReactiveParam.
Expected result: Get a dropdown list with the files content.
Actual result: The job fails when processing the DSL script
ERROR: (services.groovy, line 5) No such property: product for class: dsl.jobs.argocd.services
I tried to define the products referenced parameter as an environment variable (And updated in the services.groovy script), it didn't work.
I tried to re-create the services.groovy file in the directory /tmp/ but I had issues finding the files.
products.groovy:
package dsl.jobs.argocd
return ['a','b','c']
services.groovy:
package dsl.jobs.argocd
return_value = []
if (product.equals("a")){
return_value = ['e']
}
if (product.equals("b")){
return_value = ['f']
}
if (product.equals("c")){
return_value = ['g']
}
return return_value;
Pipeline:
pipelineJob("test") {
description("test")
keepDependencies(false)
parameters {
activeChoiceParam('product') {
description('What product would you like to update?')
filterable()
choiceType('SINGLE_SELECT')
groovyScript {
script(readFileFromWorkspace('dsl/jobs/argocd/products.groovy'))
fallbackScript('return ["ERROR"]')
}
}
activeChoiceReactiveParam('service') {
description('Which services would you like to update?')
filterable()
choiceType('CHECKBOX')
groovyScript {
script(readFileFromWorkspace('dsl/jobs/argocd/services.groovy'))
fallbackScript('return ["ERROR"]')
}
referencedParameter("product")
}
}
}
Am I approaching this wrong? Is there a different way to use the same parameter in a few Groovy files?
Well, seems the code above is perfect, the only problem was the location of the services.groovy script.
I took the file out of the DSL directory (Since I don't want it to be parsed as a DSL file), referenced it to the correct location, and it works perfect.
Updated pipeline:
pipelineJob("test") {
description("test")
keepDependencies(false)
parameters {
activeChoiceParam('product') {
description('What product would you like to update?')
filterable()
choiceType('SINGLE_SELECT')
groovyScript {
script(readFileFromWorkspace('dsl/jobs/argocd/products.groovy'))
fallbackScript('return ["ERROR"]')
}
}
activeChoiceReactiveParam('service') {
description('Which services would you like to update?')
filterable()
choiceType('CHECKBOX')
groovyScript {
script(readFileFromWorkspace('services.groovy'))
fallbackScript('return ["ERROR"]')
}
referencedParameter("product")
}
}
}

Groovy modify map in list

I am trying to modify each map in a list in my build. First stage validates the config and I want to generate the image name for each dockerfile and attach it to the map. This value is used in a later stage. Issue is the value .image_name is null still in later stages.
call(build = [:]) {
def docker_files = build.docker_files
stage("Validate") {
steps {
script {
docker_files.each {
// do validation stuff
it.image_name = build_util.get_image_name(it)
}
}
}
}
stage("Build") {
steps {
script {
docker_files.each {
println "${it.image_name}" // would print 'null'
build_util.build(it)
}
}
}
}
}
The App Jenkinsfile looks like so
App([
docker_files: [
[file: "Dockerfile", name: "blah"],
[file: "nginx/Dockerfile", name: "nginx"]
]
])
Edit: I have since attempted the following as well to no avail
docker_files.eachWithIndex { it, idx ->
it.image_name = build_util.get_image_name(it)
docker_files[idx] = it
}
I'm assuming this has something to do with scoping however I have modified other values that were defined immediately inside call. Those modifications carried to later stages so I'm not sure why I am seeing this issue here.

Pass variable to JobDsl seed job (Jenkins) in scriptText?

I am working on a project and i have to configure a jenkins using JCasC (config as code plugin).
I have to create a job BUT i can't pass variables in the script.
My code:
freeStyleJob("SEED") {
parameters {
stringParam("MY_PARAMETER", "defaultValue", "A parameter")
}
steps {
jobDsl {
scriptText('''
job("seedJOB") {
displayName('${MY_PARAMETER}') // don't work
description("${MY_PARAMETER}") // don't work
//description("$MY_PARAMETER") // don't work
//description('$MY_PARAMETER') // don't work
// i tried to use triple full quotes instead of triple single quote but it's not working...
... here the job...
'''.stripIndent())
}
}
EDIT: BEST SOLUTION HERE:
i'm writing groovy code in """ quotes so if I want to evaluate variable : I don't have to put ${} just write your variable name:
With the solution:
freeStyleJob("SEED") {
parameters {
stringParam("MY_PARAMETER", "defaultValue", "A parameter")
}
steps {
jobDsl {
scriptText('''
job("seedJOB") {
displayName('MY_PARAMETER) // solution
... here the job...
'''.stripIndent())
}
}
easy!
May you could write it to a file ? You'll get something like that in your step:
steps {
shell('echo $DISPLAY_NAME > display_name.txt')
jobDsl {
scriptText('''
job("seedjob") {
String jobname = readFileFromWorkspace('display_name.txt').trim()
displayName(jobname)
}
'''.stripIndent())
}
}
You could also use a .properties file to do it more properly.

How to return a value from Jenkins function to the build stage?

I want to return the value from groovy function back to my jenkins build stage so that the value can be used as a condition in other stages. I am not able to figure out how to implement this. I have tried something like below but that didn't work.
I have Jenkinsfile something like this:
pipeline
{
agent any
stages
{
stage('Sum')
{
steps
{
output=sum()
echo output
}
}
stage('Check')
{
when
{
expression
{
output==5
}
}
steps
{
echo output
}
}
}
}
def sum()
{
def a=2
def b=3
def c=a+b
return c
}
The above approach doesn't work. Can someone provide correct implementation.
You are missing a script-step. It is necessary if you want to execute plain groovy in your Jenkinsfile. Furthermore output has to be set as global variable if you want to access it later.
def output // set as global variable
pipeline{
...
stage('Sum')
{
steps
{
script
{
output = sum()
echo "The sum is ${output}"
}
}
}
...

Resources