Is it possible to propagate variable from a Jenkins pipeline job script to loaded jenkinsfile script?









up vote
0
down vote

favorite












I would like to know if it's possible to pass a variable define in a jenkins pipeline job to a loaded script like.
The loaded script



Pipeline Job



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Define specific variable
String SERVER=props."SRV_$srv"
String CONF=env.DEPLOY_HOME + "/$srv"

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"



parallel servers






server-configuration file



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$SERVER 'sh echo "$CONF"'"




Thanks










share|improve this question

















  • 1




    I got another hint on your script: since the loaded scripts have their own node block the parallel call should not be done inside a node block as this will only block a node for no reason. If the runtime is a few secs only this won’t hurt. However if there’s a shortage in nodes you may block one node until another one will be free... Just saying:)
    – Joerg S
    Nov 11 at 7:16










  • Hum... Thanks. If I understand what you said, it's better if I do that ? def server .... node .... parralel server ? (parralel call is inside the node ?) Or I suppress the node block in the loaded script ?
    – Sylvain Goubaud
    Nov 12 at 18:55











  • This will depend on your need. If you'll be able to reuse that node I'd rather prefer it that way as - for large Jenkins instances - opening a new node block may take up to some minutes.
    – Joerg S
    Nov 13 at 17:08










  • Thanks for your answer. I made some tries. So, for my needs, It's better is I delete the node block in server-configuration file. Thanks
    – Sylvain Goubaud
    Nov 14 at 20:24














up vote
0
down vote

favorite












I would like to know if it's possible to pass a variable define in a jenkins pipeline job to a loaded script like.
The loaded script



Pipeline Job



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Define specific variable
String SERVER=props."SRV_$srv"
String CONF=env.DEPLOY_HOME + "/$srv"

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"



parallel servers






server-configuration file



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$SERVER 'sh echo "$CONF"'"




Thanks










share|improve this question

















  • 1




    I got another hint on your script: since the loaded scripts have their own node block the parallel call should not be done inside a node block as this will only block a node for no reason. If the runtime is a few secs only this won’t hurt. However if there’s a shortage in nodes you may block one node until another one will be free... Just saying:)
    – Joerg S
    Nov 11 at 7:16










  • Hum... Thanks. If I understand what you said, it's better if I do that ? def server .... node .... parralel server ? (parralel call is inside the node ?) Or I suppress the node block in the loaded script ?
    – Sylvain Goubaud
    Nov 12 at 18:55











  • This will depend on your need. If you'll be able to reuse that node I'd rather prefer it that way as - for large Jenkins instances - opening a new node block may take up to some minutes.
    – Joerg S
    Nov 13 at 17:08










  • Thanks for your answer. I made some tries. So, for my needs, It's better is I delete the node block in server-configuration file. Thanks
    – Sylvain Goubaud
    Nov 14 at 20:24












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I would like to know if it's possible to pass a variable define in a jenkins pipeline job to a loaded script like.
The loaded script



Pipeline Job



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Define specific variable
String SERVER=props."SRV_$srv"
String CONF=env.DEPLOY_HOME + "/$srv"

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"



parallel servers






server-configuration file



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$SERVER 'sh echo "$CONF"'"




Thanks










share|improve this question













I would like to know if it's possible to pass a variable define in a jenkins pipeline job to a loaded script like.
The loaded script



Pipeline Job



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Define specific variable
String SERVER=props."SRV_$srv"
String CONF=env.DEPLOY_HOME + "/$srv"

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"



parallel servers






server-configuration file



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$SERVER 'sh echo "$CONF"'"




Thanks







jenkins groovy jenkins-pipeline






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 16:44









Sylvain Goubaud

485




485







  • 1




    I got another hint on your script: since the loaded scripts have their own node block the parallel call should not be done inside a node block as this will only block a node for no reason. If the runtime is a few secs only this won’t hurt. However if there’s a shortage in nodes you may block one node until another one will be free... Just saying:)
    – Joerg S
    Nov 11 at 7:16










  • Hum... Thanks. If I understand what you said, it's better if I do that ? def server .... node .... parralel server ? (parralel call is inside the node ?) Or I suppress the node block in the loaded script ?
    – Sylvain Goubaud
    Nov 12 at 18:55











  • This will depend on your need. If you'll be able to reuse that node I'd rather prefer it that way as - for large Jenkins instances - opening a new node block may take up to some minutes.
    – Joerg S
    Nov 13 at 17:08










  • Thanks for your answer. I made some tries. So, for my needs, It's better is I delete the node block in server-configuration file. Thanks
    – Sylvain Goubaud
    Nov 14 at 20:24












  • 1




    I got another hint on your script: since the loaded scripts have their own node block the parallel call should not be done inside a node block as this will only block a node for no reason. If the runtime is a few secs only this won’t hurt. However if there’s a shortage in nodes you may block one node until another one will be free... Just saying:)
    – Joerg S
    Nov 11 at 7:16










  • Hum... Thanks. If I understand what you said, it's better if I do that ? def server .... node .... parralel server ? (parralel call is inside the node ?) Or I suppress the node block in the loaded script ?
    – Sylvain Goubaud
    Nov 12 at 18:55











  • This will depend on your need. If you'll be able to reuse that node I'd rather prefer it that way as - for large Jenkins instances - opening a new node block may take up to some minutes.
    – Joerg S
    Nov 13 at 17:08










  • Thanks for your answer. I made some tries. So, for my needs, It's better is I delete the node block in server-configuration file. Thanks
    – Sylvain Goubaud
    Nov 14 at 20:24







1




1




I got another hint on your script: since the loaded scripts have their own node block the parallel call should not be done inside a node block as this will only block a node for no reason. If the runtime is a few secs only this won’t hurt. However if there’s a shortage in nodes you may block one node until another one will be free... Just saying:)
– Joerg S
Nov 11 at 7:16




I got another hint on your script: since the loaded scripts have their own node block the parallel call should not be done inside a node block as this will only block a node for no reason. If the runtime is a few secs only this won’t hurt. However if there’s a shortage in nodes you may block one node until another one will be free... Just saying:)
– Joerg S
Nov 11 at 7:16












Hum... Thanks. If I understand what you said, it's better if I do that ? def server .... node .... parralel server ? (parralel call is inside the node ?) Or I suppress the node block in the loaded script ?
– Sylvain Goubaud
Nov 12 at 18:55





Hum... Thanks. If I understand what you said, it's better if I do that ? def server .... node .... parralel server ? (parralel call is inside the node ?) Or I suppress the node block in the loaded script ?
– Sylvain Goubaud
Nov 12 at 18:55













This will depend on your need. If you'll be able to reuse that node I'd rather prefer it that way as - for large Jenkins instances - opening a new node block may take up to some minutes.
– Joerg S
Nov 13 at 17:08




This will depend on your need. If you'll be able to reuse that node I'd rather prefer it that way as - for large Jenkins instances - opening a new node block may take up to some minutes.
– Joerg S
Nov 13 at 17:08












Thanks for your answer. I made some tries. So, for my needs, It's better is I delete the node block in server-configuration file. Thanks
– Sylvain Goubaud
Nov 14 at 20:24




Thanks for your answer. I made some tries. So, for my needs, It's better is I delete the node block in server-configuration file. Thanks
– Sylvain Goubaud
Nov 14 at 20:24












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










withEnv is what you are looking for



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
withEnv(["SERVER=props.SRV_$srv","CONF=env.DEPLOY_HOME + $srv"])
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"




parallel servers




server-config



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$env.SERVER 'sh echo "$env.CONF"'"







share|improve this answer




















  • Thank you very much. I tried withEnv and it worked. I was about to write a comment when I saw your answer
    – Sylvain Goubaud
    Nov 10 at 20:10










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241133%2fis-it-possible-to-propagate-variable-from-a-jenkins-pipeline-job-script-to-loade%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










withEnv is what you are looking for



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
withEnv(["SERVER=props.SRV_$srv","CONF=env.DEPLOY_HOME + $srv"])
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"




parallel servers




server-config



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$env.SERVER 'sh echo "$env.CONF"'"







share|improve this answer




















  • Thank you very much. I tried withEnv and it worked. I was about to write a comment when I saw your answer
    – Sylvain Goubaud
    Nov 10 at 20:10














up vote
1
down vote



accepted










withEnv is what you are looking for



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
withEnv(["SERVER=props.SRV_$srv","CONF=env.DEPLOY_HOME + $srv"])
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"




parallel servers




server-config



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$env.SERVER 'sh echo "$env.CONF"'"







share|improve this answer




















  • Thank you very much. I tried withEnv and it worked. I was about to write a comment when I saw your answer
    – Sylvain Goubaud
    Nov 10 at 20:10












up vote
1
down vote



accepted







up vote
1
down vote



accepted






withEnv is what you are looking for



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
withEnv(["SERVER=props.SRV_$srv","CONF=env.DEPLOY_HOME + $srv"])
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"




parallel servers




server-config



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$env.SERVER 'sh echo "$env.CONF"'"







share|improve this answer












withEnv is what you are looking for



node 

// Some declaration and code

stage('Prepare deploy')
// Create a talbe for the servers lists
def servers = [:]

// For each server know, if deployment is enabled
// deploy resources
params.each srv, value ->
if ("$value" == "true")
// add the current current to the enabled servers
servers["Server $srv"] =

// Install server - the SERVER and CONF variable need to be propagate to the others scripts
withEnv(["SERVER=props.SRV_$srv","CONF=env.DEPLOY_HOME + $srv"])
load env.JENKINSFILES_DIRECTORY + "/server-configuration"
load env.JENKINSFILES_DIRECTORY + "/server-deploiement"
load env.JENKINSFILES_DIRECTORY + "/server-postconfig"
load env.JENKINSFILES_DIRECTORY + "/server-start"




parallel servers




server-config



#!/usr/bin/env groovy​
node
// Some declaration
stage('configure serveur')
// The variable SERVER and CONF must be visible here
// is it possible ?
sh "ssh -X " + env.USER +"@$env.SERVER 'sh echo "$env.CONF"'"








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 18:08









Ram Kamath

519314




519314











  • Thank you very much. I tried withEnv and it worked. I was about to write a comment when I saw your answer
    – Sylvain Goubaud
    Nov 10 at 20:10
















  • Thank you very much. I tried withEnv and it worked. I was about to write a comment when I saw your answer
    – Sylvain Goubaud
    Nov 10 at 20:10















Thank you very much. I tried withEnv and it worked. I was about to write a comment when I saw your answer
– Sylvain Goubaud
Nov 10 at 20:10




Thank you very much. I tried withEnv and it worked. I was about to write a comment when I saw your answer
– Sylvain Goubaud
Nov 10 at 20:10

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241133%2fis-it-possible-to-propagate-variable-from-a-jenkins-pipeline-job-script-to-loade%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo