0

So I have a jenkins pipeline which connects to the server and I would like it to define some env variables that are referencing some env variables from jenkins.

To connect to the server I am using sshCommand plugin:

def remote = [name: 'something', host : '${HOST}', password: '${PASSWORD}'] pipeline { agent any stages { stage('env var'){ steps { sshCommand remote: remote, command: 'export ENV1=${env.ENV1}' } } } } 

However it outputs constantly the following error:

groovy.lang.MissingPropertyException: No such property: remote for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) 

Any ideas on where is this error coming from?

1
  • 1
    Just define the remote variable without the def keyword. See the answer to the following question for further details. Commented Nov 14, 2021 at 20:03

1 Answer 1

1

There is the scope issue of def variables, as pointed out by commenter Noam Helmer. See the answer to this question for further details.

Another issue is that before the pipeline starts, environment variables are not yet made available by Jenkins.

AFAIK, the earliest possible location to refer to environment variables is from within the environment block of the pipeline, but that only allows to define string values, not complex objects like your remote variable.

A possible workaround is to return the object from a function or a closure. In this case the environment variables will only be resolved at the time when the function or closure is called, not at the time when the function or closure is defined. In the following I've opted for a closure {} as it allows for a cleaner syntax.

// Define a closure {} that returns an object []. remote = {[name: 'something', host: HOST, password: PASSWORD]} pipeline { agent any stages { stage('env var'){ steps { // Call the closure to resolve the env vars. sshCommand remote: remote(), command: 'export ENV1=$ENV1' } } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.