Error: ‘Jenkins: Cannot define variable in pipeline stage’

0 votes

I’m using the Jenkins pipeline feature to run a declarative pipeline in which I’m trying to define a variable:

pipeline {
   agent none
   stages {
       stage("first") {
           def foo = "foo" 
           sh "echo ${foo}"
       }
   }
}

I get the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: 
           def foo = "foo"
           ^
Aug 17, 2018 in Jenkins by Tyrion anex
• 8,280 points
512 views

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Try this, you have to define an environment variable in order to do this.

pipeline {
   environment {
     FOO = "foo"
   }
   agent none
   stages {
       stage("first") {
           sh "echo ${FOO}"
      }
   }
}
answered Aug 17, 2018 by Sophie may
• 9,050 points
0 votes

You can create variables before the pipeline block starts.

For Example,

def foo = "foo" 
pipeline {
   agent none
   stages {
       stage("first") {
           sh "echo ${foo}"
       }
   }
}

answered Jan 27 by anonymous
Hey, I have a small doubt. If variables are defined outside the pipeline are they like global variables?
0 votes
answered Mar 13 by Charles Wu
Yeah, I found this thread but I had a doubt but I didn't have enough reputations to comment there.
Tell me about it !!

Never been able to comment on stackoverflow... :(

Related Questions In Jenkins

0 votes
2 answers
+2 votes
3 answers

Permission denied error while running a simple job in Jenkins

Hey @Hannah, try changing the permissions using ...READ MORE

answered Oct 25, 2018 in Jenkins by krishti
2,943 views
0 votes
1 answer

Error: ‘Jenkins pipeline: No such DSL method'

This is a simple mistake, just remove ...READ MORE

answered Aug 17, 2018 in Jenkins by Sophie may
• 9,050 points
1,683 views
0 votes
2 answers

"No such file pom.xml" error in Jenkins

Can you please che ck the location ...READ MORE

answered May 15 in Jenkins by Samir
90 views
+13 votes
2 answers
0 votes
1 answer
0 votes
1 answer

How to trigger a Jenkins pipeline A in another Jenkins pipeline B?

You can run the following code, its ...READ MORE

answered Jul 5, 2018 in Jenkins by Sophie may
• 9,050 points
788 views
0 votes
5 answers

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.