I have the following code to build as a job-dsl with "Active Choice Plugin":
freeStyleJob('job') {
description('description')
// Label which specifies on which nodes this job can be run.
label('master')
logRotator {
numToKeep(10)
}
//This Build is parametrized
parameters {
activeChoiceReactiveParam('branch') {
description('Select the branch you are going to use')
choiceType('SINGLE_SELECT')
script('["integration", "master"]')
fallbackScript('"Error. No branch to select."')
filterable(true)
}
}
}
I when executing it I get the following error:
How can this error be solved?
The syntax is actually a bit different:
job('example-1') {
parameters {
activeChoiceReactiveParam('CHOICE-1') {
description('Allows user choose from multiple choices')
filterable()
choiceType('SINGLE_SELECT')
groovyScript {
script('["choice1", "choice2"]')
fallbackScript('"fallback choice"')
}
referencedParameter('BOOLEAN-PARAM-1')
referencedParameter('BOOLEAN-PARAM-2')
}
}
}
Use the API viewer to lookup the syntax:
https://jenkinsci.github.io/job-dsl-plugin/#path/job-parameters-activeChoiceReactiveParam
Related
I'm writing pipelinejob which looks like that:
pipelineJob('TESTING) {
description('TEST')
definition {
cpsScm {
scm {
git {
branch("\$deploy_branch")
remote {
credentials("CREDENTIALS")
url('REPO')
}
browser {
bitbucketWeb {
repoUrl("REPO")
}
}
extensions {
wipeWorkspace()
}
}
}
scriptPath("Jenkinsfile.deploy_seeds.build")
}
}
parameters {
gitParam('deploy_branch') {
type('BRANCH')
defaultValue('origin/master')
}
}
}
so inside this pipelinejob I call script from repo which looks like that:
pipeline {
agent any
stages {
stage('Test'){
steps {
echo('Hello World!')
dsl {
external('deploy_SEEDS.groovy')
additionalClasspath('src')
}
echo("End dsl block")
}
}
}
}
Unfortunately, after running this job I got:
java.lang.NoSuchMethodError: No such DSL method 'dsl' found among steps [ansiColor, ansiblePlaybook etc.... I have installed job-dsl plugin. Could somebody tell me what I'm doing wrong?
I have a Jenkins seed DSL job:
job("cronjob/${ACTION}_${ENVRIONMENT}_environment_CRONJOB") {
scm {
git('https://git_user#github.com/abc-Data/devops.git','*/develop')
}
triggers {
scm("${SCHEDULE}")
}
steps {
ansiblePlaybook("ansible/scripts/ansible-${ACTION}.yml") {
inventoryPath("/etc/ansible/hosts")
credentialsId("usercred")
extraVars {
extraVar('environment_name',"${ENVRIONMENT}",false)
}
}
}
}
ACTION, ENVIRONMENT and SCHDULE are parameters.
ACTION can have values of create or remove, and I have two ansible playbooks ansible-create.yml and ansible-remove.yml.
When I run the sseed job, I got the following error:
ERROR: (unknown source) No signature of method: javaposse.jobdsl.dsl.helpers.step.StepContext.ansiblePlaybook() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl, script$_run_closure1$_closure4$_closure5) values: [ansible/scripts/ansible-create.yml, ...]
Finished: FAILURE
The ansiblePlaybook("ansible/scripts/ansible-${ACTION}.yml") does not work with the variable ACTION.
If I hard code the the script name in ansiblePlaybook, the seed job will create a new job.
The other two variables work fine in the DSL script.
What did I miss here?
Thanks!
As #daggett suggested above, I changed the code as below:
job("cronjob/${ACTION}_${ENVRIONMENT}_environment_CRONJOB") {
scm {
git('https://git_user#github.com/abc-Data/devops.git','*/develop')
}
triggers {
scm("${SCHEDULE}")
}
steps {
ansiblePlaybook("ansible/scripts/ansible-${ACTION}.yml" as String) {
inventoryPath("/etc/ansible/hosts")
credentialsId("usercred")
extraVars {
extraVar('environment_name',"${ENVRIONMENT}",false)
}
}
}
}
It works as expect and create a new job with the use entered variable values. Thanks to you again #daggett!
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")
}
}
}
I have a groovy script to setup a scheduled job in Jenkins.
I want to execute some shell scripts on failed build.
If I had the scripts manually after the job creation after the job is updated by groovy script, they run.
But the groovy script does not add it:
job('TestingAnalysis') {
triggers {
cron('H 8 28 * *')
}
steps {
shell('some jiberish to create error')
}
publishers {
postBuildScripts {
steps {
shell('echo "fff"')
shell('echo "FFDFDF"')
}
onlyIfBuildSucceeds(false)
onlyIfBuildFails(true)
}
retryBuild {
rerunIfUnstable()
retryLimit(3)
fixedDelay(600)
}
}
}
Every thing works fine except:
postBuildScripts {
steps {
shell('echo "fff"')
shell('echo "FFDFDF"')
}
onlyIfBuildSucceeds(false)
onlyIfBuildFails(true)
}
This is my result:
I tried postBuildSteps and also got error.
I tried also with error:
postBuildScripts {
steps {
sh' echo "ggg" '
}
onlyIfBuildSucceeds(false)
onlyIfBuildFails(true)
}
Take a look at JENKINS-66189 seems like there is an issue with version 3.0 of the PostBuildScript in which the old syntax (that you are using) is no longer supported. In order to use the new version it in a Job Dsl script you will need to use Dynamic DSL syntax.
Use the following link in your own Jenkins instance to see the correct usage:
YOUR_JENKINS_URL/plugin/job-dsl/api-viewer/index.html#path/freeStyleJob-publishers-postBuildScript.
it will help you build the correct command. In your case it will be:
job('TestingAnalysis') {
triggers {
cron('H 8 28 * *')
}
steps {
shell('some jiberish to create error')
}
publishers {
postBuildScript {
buildSteps {
postBuildStep {
stopOnFailure(false) // Mandatory setting
results(['FAILURE']) // Replaces onlyIfBuildFails(true)
buildSteps {
shell {
command('echo "fff"')
}
shell {
command('echo "FFDFDF"')
}
}
}
}
markBuildUnstable(false) // Mandatory setting
}
}
}
Notice that instead of using functions like onlyIfBuildSucceeds and onlyIfBuildFails you now just pass a list of relevant build results to the results function. (SUCCESS,UNSTABLE,FAILURE,NOT_BUILT,ABORTED)
I using Jenkins CI with Job DSL and Multijob plugins. I'm attempting to define a parametrized Multijob containing conditional step using DSL, but I fail to figure out the correct syntax. My code:
multiJob("MyJob")
{
parameters {
stringParam("PLATFORM", "Win32")
stringParam("CONFIGURATION", "Release")
}
steps
{
phase("Build") {
job("BuildJob") { sameNode() }
}
conditionalSteps {
condition {
and { stringsMatch("${PLATFORM}", "Win32", false) } { stringsMatch("${CONFIGURATION}", "Release", false) }
}
runner('Fail')
steps {
phase("Prepare installer") {
job("PrepareInstallerJob") { sameNode() }
}
}
}
}
}
Running this I get the following error:
Processing DSL script My.groovy
ERROR: (My.groovy, line 117) No such property: PLATFORM for class: javaposse.jobdsl.dsl.helpers.step.RunConditionContext
Finished: FAILURE
When line 117 is the line containing the "and" condition.
What would be the correct syntax? Why does not it resolve the PLATFORM parameter?
Groovy interpolates double quoted strings, see String interpolation. You need to use single quotes to avoid the interpolation, e.g. '${PLATFORM}'.