/ / pipeline jenkins: lo script shell non può ottenere la variabile d'ambiente aggiornata - shell, jenkins, jenkins-pipeline

pipeline jenkins: lo script di shell non può ottenere la variabile di ambiente aggiornata - shell, jenkins, jenkins-pipeline

In Jenkins, voglio ottenere l'input dell'utente e passare a uno script di shell per un ulteriore utilizzo.
Ho provato a impostare come variabile d'ambiente, ma lo script della shell non è riuscito a ottenere l'ultimo valore e il vecchio valore è echo.

pipeline {
agent none
environment{
myVar="something_default"
}

stages {
stage("First stage") {
agent none
steps{
echo "update myVar by user input"
script {
test = input message: "Hello",
ok: "Proceed?",
parameters: [
string(name: "input", defaultValue: "update it!", description: "")
]
myVar = "${test["input"]}"

}
echo "${myVar}"  // value is updated
}
}
stage("stage 2") {
agent any
steps{
echo "${myVar}" // try to see can myVar pass between stage and it output expected value
sh("./someShell.sh") // the script just contain a echo e.g. echo "myVar is ${myVar}"
// it echo the old value. i.e.something_default

}
}
}
}

risposte:

1 per risposta № 1

È necessario passare le variabili tra le fasi come variabili di ambiente, ad es. come questo:

stage("As user for input") {
steps {
env.DO_SOMETING = input (...)
env.MY_VAR = ...
}
}
stage("Do something") {
when { environment name: "DO_SOMETING", value: "yes" }
steps {
echo "DO_SOMETING has the value ${env.DO_SOMETHING}"
echo "MY_VAR has the value ${env.MY_VAR}"
}
}

1 per risposta № 2

Le variabili d'ambiente che abbiamo impostato nella pipeline Script saranno accessibili solo all'interno dello script. Quindi, anche se dichiari la tua variabile come globale, non funzionerà all'interno di uno script di shell.

L'unica opzione a cui riesco a pensare è passare come argomento allo script della shell

 sh("./someShell.sh ${myVar}")

MODIFICARE: Risposta aggiornata basata sulla query dell'OP sullo script Shell per l'analisi dell'input

LINE="[fristPara:100, secondPaa:abc]"
LINE=$(echo $LINE | sed "s/[//g")
LINE=$(echo $LINE | sed "s/]//g")
while read -d, -r pair; do
IFS=":" read -r key val <<<"$pair"
echo "$key = $val"
done <<<"$LINE,

"


0 per risposta № 3

Devi dichiarare la variabile su un ambito globale in modo che entrambi i posti facciano riferimento alla stessa istanza.

def myVal

pipeline { ... }