Question regarding using groovy code in Jenkins, specifically in Jenkinsfile and groovy plugins
Questions tagged [jenkins-groovy]
2809 questions
94
votes
4 answers
How to create methods in Jenkins Declarative pipeline?
In Jenkins scripted pipeline we are able to create methods and can call them.
Is it possible also in the Jenkins declarative pipeline? And how?

vinesh vini
- 1,041
- 1
- 7
- 4
51
votes
2 answers
Strange variable scoping behavior in Jenkinsfile
When I run the below Jenkins pipeline script:
def some_var = "some value"
def pr() {
def another_var = "another " + some_var
echo "${another_var}"
}
pipeline {
agent any
stages {
stage ("Run") {
steps {
…

haridsv
- 9,065
- 4
- 62
- 65
41
votes
5 answers
How to exit from the Jenkins pipeline if a stage sets build fail/unstable status?
I have a declarative Jenkins pipeline with stage1, stage2, stage3 and so on. I want to stop stage2 from running if stage1 sets the build unstable/fail.
I know I can stop the steps in stage1 from running using return when the build is not success but…

user6348718
- 1,355
- 5
- 21
- 28
38
votes
1 answer
Create a file with some content using Groovy in Jenkins pipeline
I am trying to create a file called groovy1.txt with the content "Working with files the Groovy way is easy."
Note: I don't want to use the shell to create this file, instead I want to use Groovy to achieve this.
I have following script in my…

smc
- 2,175
- 3
- 17
- 30
26
votes
11 answers
How to get a list of all Jenkins nodes assigned with label including master node?
I'm creating a Jenkins pipeline job and I need to run a job on all nodes labelled with a certain label.
Therefore I'm trying to get a list of node names assigned with a certain label. (With a node I can get the labels with getAssignedLabels())
The…

Patrick B.
- 11,773
- 8
- 58
- 101
22
votes
4 answers
No such property: api for class: groovy.lang.Binding error
Im trying to build a pipeline on Jenkins that runs a command on node and informs me of the following error:
groovy.lang.MissingPropertyException: No such property: api for class: groovy.lang.Binding
at…

Mario Ramos García
- 755
- 3
- 8
- 20
19
votes
2 answers
Difference between execute groovy script and the execute system groovy script in jenkins?
Can anyone explain the different between the execute groovy script and the execute system groovy script in jenkins? And how to call the script to slave using execute system groovy script.

user2400564
- 4,619
- 7
- 24
- 27
16
votes
3 answers
Jenkins shared library with intellij
I started implement Jenkins shared libraries and trying to write my jenkinsfile with intellij as my ide.
How do I get functions from the shared lib repository to the other repository which holds the jenkins file inside it.
Just to clarify things are…

Shahar Hamuzim Rajuan
- 5,610
- 9
- 53
- 91
15
votes
3 answers
Jenkins: What is a good way to store a variable between two job runs?
I have a time-triggered job which needs to retrieve certain values stored in a previous run of this job.
Is there a way to store values between job runs in the Jenkins environment?
E.g., I can write something like next in a shell script…

Sergey Grinev
- 34,078
- 10
- 128
- 141
15
votes
3 answers
Jenkins Declarative Pipeline - how to get path to Jenkinsfile in use?
We have a series of Jenkinsfile scripts which are identical, except each configures an environment item to point to the directory in the SCM checkout that holds the Jenkinsfile. That is used to reference a file in the same directory. The SCM repo…

Kevin Buchs
- 2,520
- 4
- 36
- 55
14
votes
1 answer
Sequential stages within parallel pipeline in Jenkins
I have a dynamic scripted pipeline in Jenkins that has many parallel stages, but within each stage, there are multiple serial steps. I have wasted several days trying to make it work: no matter what I try, all serial substages are lumped into one…

Oleksiy
- 6,337
- 5
- 41
- 58
14
votes
6 answers
How do you handle global variables in a declarative pipeline?
Previously asked a question about how to overwrite variables defined in an environment directive and it seems that's not possible.
I want to set a variable in one stage and have it accessible to other stages.
In a declarative pipeline it seems the…

red888
- 27,709
- 55
- 204
- 392
12
votes
3 answers
Jenkins: set a parameter defaultValue dynamically
I'm trying to set up a multibranch pipeline configuration where the "Deploy" boolean checkbox is defaulted to true on non-production branches, and false on the production build.
pipeline {
parameters{
booleanParam(defaultValue: true,…

Cal
- 411
- 1
- 3
- 11
12
votes
1 answer
How to use groovy to populate an env variable in a declarative jenkins pipeline
I am struggling to populate an environment variable in a Jenkinsfile using groovy
The code below fails:
pipeline {
environment {
PACKAGE_NAME = JOB_NAME.tokenize('/')[1]
}
{
with the following error:
Environment variable values can only be…

user3124206
- 375
- 1
- 7
- 16
11
votes
1 answer
Aggregate multiple upstream SCM changes in downstream job
On my current project, I have several dozen "sub-projects", each with trunk and branches (legacy setup, can't change it)
There are multiple commits on a branch, let's say 5, and then a branch is reintegrated to trunk. When a branch is reintegrated…

Slav
- 27,057
- 11
- 80
- 104