Using activeChoiceReactiveParam in pipeline - jenkins

How to use activeChoiceReactiveParam in Jenkins pipeline?
this is the relevant part of my jenkins file:
parameters{
activeChoiceParam('foo') {
description('zzzz')
filterable()
choiceType('SINGLE_SELECT')
groovyScript {
script('xx')
fallbackScript('xx')
}
}
}
Getting this error when running the build:
WorkflowScript: 10: Build parameters definitions cannot have blocks # line 10, column 6.
activeChoiceParam('foo') {
^

I think that as this post says, pipeline doesn't support activeChoiceParams.
That post is from 2 years ago but seems to be valid still; the Active Choices plugin repo links a Jenkins issue which blocks pipeline from working well with the plugin, and their pipeline example is similar to what they described in the post I linked.

Related

what does No such DSL method 'readCSV' means in a jenkins pipeline?

I need to read a simple csv file and going through the documentation I found this readCSV method which comes with Jenkins, I have set a sample file named test.csv in the workspace folder and use this simple test pipeline:
'''
pipeline {
agent any
stages {
stage('read csv') {
steps {
script{
def records = readCSV file: 'test.csv'
println records
}
}
}
}
}
'''
But I keep getting the No such DSL method 'readCSV' error and I am not sure what it means, I have read here in SO that usually means you lack a plugin but this does not seem to be the case
Apparently the pipeline-utility-steps plugin was not installed and I thought it was installed by default, in case anyone else faces the same issue.

Jenkins "unable to resolve class Declarative" error when building Python script

I began learning to use to Jenkins and wanted to make it run a Python script of mine automatically. I followed their tutorial and created a new Project called Pipeline Test.
I've also added the GitHub repo of a Python script I wanted to test (https://github.com/mateasmario/spam-bot).
As you can see, I've created a Jenkinsfile in that repo. Because my script is called spam-bot.py, I want my Jenkinsfile to run that script every time I click "Build now" inside Jenkins. This is the content of my Jenkinsfile:
Jenkinsfile (Declarative Pipeline)
pipeline {
agent { docker { image 'python:3.10.1-alpine' } }
stages {
stage('build') {
steps {
sh 'python spam-bot.py'
}
}
}
}
The problem is, whenever I click "Build now", my build fails and the console outputs the following error:
Started by user Mario Mateas
Obtained Jenkinsfile from git https://github.com/mateasmario/spam-bot.git
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 1: unable to resolve class Declarative
# line 1, column 26.
Jenkinsfile (Declarative Pipeline)
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:334)
at hudson.model.ResourceController.execute(ResourceController.java:99)
at hudson.model.Executor.run(Executor.java:432)
Finished: FAILURE
I looked up for this error on the internet, but didn't find any useful information, and that's why I decided to ask here.
I also don't have any Docker container configured. Do I need to configure one? I took a look over Jenkins' documentation for Docker, but didn't see any useful information for adding a Python image (as the one mentioned at the beginning of the Jenkinsfile) to the container.
Your Jenkinsfile contains invalid syntax on the first line, which is why the error is being thrown. Assuming you intended that first line to be a comment, you can modify the pipeline code to be:
// Jenkinsfile (Declarative Pipeline)
pipeline {
...
}
and your pipeline code will have valid syntax.

Looking for a jenkins pipeline build job syntax that really works

I'm trying to trigger a jenkins pipeline job from inside another jenkins pipeline job, with parameters.
I'm relatively newbie in java/groovy so I search the web for functional samples but all that I found are unusable for syntax or scripting reasons.
Some of my tests below:
How to trigger another Jenkins pipeline that needs a $BRANCH variable?
node() {
build job: 'INVENTORIES', propagate: true, wait: true
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'build' found among steps [ansiblePlaybook
Jenkins pipeline for building other jobs
node() {
stage('Desc1') {
steps {
dir('/var/lib/jenkins/workspace') {
build job: 'INVENTORIES', propagate: true, wait: true
}
}
}
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'steps' found among steps [ansiblePlaybook,
node() {
stages {
stage ("build") { //an arbitrary stage name
steps {
build 'INVENTORIES' //this is where we specify which job to invoke.
}
}
}
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'stages' found among steps [ansiblePlaybook,
I've tried plenty of samples (script block, step block, stage block...) but it never works, always throwing java exception like:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.CoreStep.delegate expects interface jenkins.tasks.SimpleBuildStep but received class ...
Before I jump from a bridge, could anyone here helps me?
Thanks in advance, I know swimming but it's a little cold
[SOLVED]
It were missing a Pipeline Plugin but the error messages wasn't clear enought and log content too poor to guess.
Thanks to #zett42 to have pointed me on the good search way.
Have a nice day.

Why is Jenkins caching my old broken Jenkinsfile?

I have pushed several updates, which actually shows in the changes in jenkins, but the error still shows the same error message when the first Jenkinsfile caused the error.
example:
Started by an SCM change
Obtained Jenkinsfile from git git#bitbucket[myserver]/jenkins_docker.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 2: Expected an agent # line 2, column 3.
agent {
^
WorkflowScript: 2: No agent type specified. Must be one of [any, docker, dockerfile, label, none] # line 2, column 3.
agent {
^
2 errors
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
My jenkinsfile is now updated to look like this:
pipeline {
agent {
label: 'nodejs10'
}
stages {
stage('Test') {
steps {
echo 'Testing...'
}
}
}
}
ok it's not caching. I realised my mistakes.
I had the nodejs plugin installed but I hadn't configured node10 in the global tool configurations for nodejs plugin:
https://wiki.jenkins.io/display/JENKINS/NodeJS+Plugin
Also I was calling it wrong. I have now changed agent to 'any', and followed the instructions on this artcile as below:
https://medium.com/#gustavo.guss/jenkins-starting-with-pipeline-doing-a-node-js-test-72c6057b67d4
pipeline {
agent any
tools {nodejs "nodejs10"}
// stuff here...
}
this works. Though I now have a different issue with npm not installing, but that is now in a separate quesiton.

Job DSL script fails on generating job with allure plugin for reports

I have some groovy script for generating jenkins jobs, which worked great.
But after reinstallation of jenkins this script throws exception.
problem part of script is:
publishers {
allure(['path-to/reports'])
}
And error is:
Processing DSL script seed.groovy
ERROR: (JobBuilder.groovy, line 55) No signature of method:
javaposse.jobdsl.dsl.helpers.publisher.PublisherContext.allure() is
applicable for argument types: (java.util.ArrayList) values:
[[PyUIAutotest/reports]]
Possible solutions: mailer(java.lang.String),
use([Ljava.lang.Object;), asType(java.lang.Class)
Finished: FAILURE
It fails on wrong signature of method, but it is correct, and i don't know what is wrong.
example of script available on wiki dock. and javadoc for allure() method
Jenkins ver. 2.46.2; Job DSL ver. 1.63, Allure-Jenkins-Plugin ver. 2.15
weird thing in that, i have another server with same plugin versions, and it works fine
Instead of
publishers {
allure(['path-to/reports'])
}
use this syntax (which you can also generate using Jenkins pipeline-syntax generator):
allure([includeProperties: false, jdk: '', properties: [], reportBuildPolicy: 'ALWAYS', results: [[path: 'path-to/reports']]])
You might find my answer here useful: https://stackoverflow.com/a/71082002/1169433
In short, you may need to add the allure-jenkins-plugin in your build dependencies.

Resources