반응형
선언적 Jenkins 파이프 라인의 단계간에 변수를 어떻게 전달합니까?
선언적 파이프 라인의 단계간에 변수를 어떻게 전달합니까?
스크립팅 된 파이프 라인에서 절차는 임시 파일에 쓴 다음 파일을 변수로 읽는 것입니다.
선언적 파이프 라인에서 어떻게해야합니까?
예를 들어 셸 작업에 의해 생성 된 변수를 기반으로 다른 작업의 빌드를 트리거하고 싶습니다.
stage("stage 1") {
steps {
sh "do_something > var.txt"
// I want to get var.txt into VAR
}
}
stage("stage 2") {
steps {
build job: "job2", parameters[string(name: "var", value: "${VAR})]
}
}
파일을 사용하려면 (스크립트가 필요한 값을 생성하기 때문에) readFile
아래와 같이 사용할 수 있습니다. 그렇지 않은 경우 아래 sh
표시된 script
옵션과 함께 사용 하십시오 .
// Define a groovy global variable, myVar.
// A local, def myVar = 'initial_value', didn't work for me.
// Your mileage may vary.
// Defining the variable here maybe adds a bit of clarity,
// showing that it is intended to be used across multiple stages.
myVar = 'initial_value'
pipeline {
agent { label 'docker' }
stages {
stage('one') {
steps {
echo "${myVar}" // prints 'initial_value'
sh 'echo hotness > myfile.txt'
script {
// OPTION 1: set variable by reading from file.
// FYI, trim removes leading and trailing whitespace from the string
myVar = readFile('myfile.txt').trim()
// OPTION 2: set variable by grabbing output from script
myVar = sh(script: 'echo hotness', returnStdout: true).trim()
}
echo "${myVar}" // prints 'hotness'
}
}
stage('two') {
steps {
echo "${myVar}" // prints 'hotness'
}
}
// this stage is skipped due to the when expression, so nothing is printed
stage('three') {
when {
expression { myVar != 'hotness' }
}
steps {
echo "three: ${myVar}"
}
}
}
}
간단히:
pipeline {
parameters {
string(name: 'custom_var', defaultValue: '')
}
stage("make param global") {
steps {
tmp_param = sh (script: 'most amazing shell command', returnStdout: true).trim()
env.custom_var = tmp_param
}
}
stage("test if param was saved") {
steps {
echo "${env.custom_var}"
}
}
}
반응형
'UFO ET IT' 카테고리의 다른 글
Java에서 일반 클래스에 대한 일반 생성자를 만드는 방법은 무엇입니까? (0) | 2020.12.01 |
---|---|
RuntimeWarning으로 numpy 나누기 : double_scalars에서 잘못된 값이 발견되었습니다. (0) | 2020.12.01 |
불변 문자열 대 std :: string (0) | 2020.12.01 |
명령에서 결과 데이터가 필요할 때 명령 쿼리 분리 (CQS)를 어떻게 적용합니까? (0) | 2020.12.01 |
"해제 된 스크립트에서 코드를 실행할 수 없습니다"오류의 원인 (0) | 2020.12.01 |