ׁׁׁׁ如何从 bash 文件到 jenkinsfile 的 return 值?
ׁׁׁׁHow to return value from bash file to jenkinsfile?
我有以下 jenkins 文件:
pipeline {
agent any
environment {
VERSION=""
}
stages {
stage ("release"){
steps {
script {
${VERSION}=sh(returnStdout: true, script:$("./bashscript.sh).trim())
}
}
}
}
我想做的很简单,我想调用 bash 文件,该文件 return 是我 Jenkinsfile 中环境变量的值。
我有 2 个问题:1. 如何从 bash 文件中获取 return 值? 2.如何插入到Jenkinsfile的环境变量中?
那是我的 .sh 文件:
# !/bin/bash
if [ some condition... ]
then
some commands....
return "value"
else
some commands....
return "other value"
fi
找了半天也没找到,请问有可能吗?
注意:我看到很多 groovy 的解决方案,但我需要它在管道中...
- 要为 jenkins 使用 shell 设置变量示例,请使用 returnStdout: true 。
在你的脚本中回显你需要的东西
myVal = sh(script: ' ./myFunc.ksh ', returnStdout: true)
- 从 bash 执行函数,因为它们假设 return 一个变量。
要使函数可见,您需要获取它的源文件(导入)示例如下
# for point 2
#/bin/bash
# content of your file ./myfileFunc.ksh
function myFunc {
typeset l_in_param1=""
if [[ $l_in_param1 == "A" ]]; then
echo "VAL_A"
elif [[ $l_in_param1 == "B" ]]; then
echo "VAL_B"
else
echo "ERROR VAL" >&2
fi
}
# source it
source ./myfileFunc.ksh
#use :
val1=$(myFunc A)
val2=$(myFunc C)
- 脚本 returns 数据到标准和错误输出,这样你可以稍后通过使用 returnStdout: true
从那里读取来将它存储在变量中
myVal = sh(脚本: './myFunc.ksh ', returnStdout: true)
# content of your file ./myFunc.ksh
typeset l_in_param1=""
if [[ $l_in_param1 == "A" ]]; then
echo "VAL_A"
elif [[ $l_in_param1 == "B" ]]; then
echo "VAL_B"
else
echo "ERROR VAL" >&2
fi
l_result1=$( ./myFunc.ksh A )
l_result2=$( ./myFunc.ksh C )
echo $l_result1;
echo $l_result2;
此处 l_result2 将为空,因为我们将错误移至错误输出
Execution of code example
我有以下 jenkins 文件:
pipeline {
agent any
environment {
VERSION=""
}
stages {
stage ("release"){
steps {
script {
${VERSION}=sh(returnStdout: true, script:$("./bashscript.sh).trim())
}
}
}
}
我想做的很简单,我想调用 bash 文件,该文件 return 是我 Jenkinsfile 中环境变量的值。
我有 2 个问题:1. 如何从 bash 文件中获取 return 值? 2.如何插入到Jenkinsfile的环境变量中?
那是我的 .sh 文件:
# !/bin/bash
if [ some condition... ]
then
some commands....
return "value"
else
some commands....
return "other value"
fi
找了半天也没找到,请问有可能吗?
注意:我看到很多 groovy 的解决方案,但我需要它在管道中...
- 要为 jenkins 使用 shell 设置变量示例,请使用 returnStdout: true 。 在你的脚本中回显你需要的东西
myVal = sh(script: ' ./myFunc.ksh ', returnStdout: true)
- 从 bash 执行函数,因为它们假设 return 一个变量。 要使函数可见,您需要获取它的源文件(导入)示例如下
# for point 2
#/bin/bash
# content of your file ./myfileFunc.ksh
function myFunc {
typeset l_in_param1=""
if [[ $l_in_param1 == "A" ]]; then
echo "VAL_A"
elif [[ $l_in_param1 == "B" ]]; then
echo "VAL_B"
else
echo "ERROR VAL" >&2
fi
}
# source it
source ./myfileFunc.ksh
#use :
val1=$(myFunc A)
val2=$(myFunc C)
- 脚本 returns 数据到标准和错误输出,这样你可以稍后通过使用 returnStdout: true 从那里读取来将它存储在变量中
myVal = sh(脚本: './myFunc.ksh ', returnStdout: true)
# content of your file ./myFunc.ksh
typeset l_in_param1=""
if [[ $l_in_param1 == "A" ]]; then
echo "VAL_A"
elif [[ $l_in_param1 == "B" ]]; then
echo "VAL_B"
else
echo "ERROR VAL" >&2
fi
l_result1=$( ./myFunc.ksh A )
l_result2=$( ./myFunc.ksh C )
echo $l_result1;
echo $l_result2;
此处 l_result2 将为空,因为我们将错误移至错误输出
Execution of code example