[英]Jenkins Pipeline Environment Variable in Shell script creates a new line
I am trying to access an env variable in Jenkins pipeline and want to use it in a Shell Script executing in the same pipeline but a differnt step, 我正在尝试在Jenkins管道中访问一个env变量,并想在同一管道中执行的Shell脚本中使用它,但是步骤不同,
pipeline {
agent any
tools {
maven 'M2'
}
environment {
stable_revision = sh(script: 'curl -H "Authorization: Basic $base64encoded" "https://xyz.sad" | jq -r "name"', returnStdout: true)
}
stages {
stage('Initial-Checks') {
steps {
echo "Stable Revision: ${env.stable_revision}" //displays the value
bat "sh && sh undeploy.sh"
}}
...
}}
This is a sample Shell script, it has many lines, but I have an issue in only accessing the above stable_revision
variable, 这是一个示例Shell脚本,有很多行,但是我只能访问上面的
stable_revision
变量,
#!/bin/bash
echo xyz = ${stable_revision} #### this gives the right value xyz = 22
echo xyz2 = ${stable_revision}/d ### here after the value the /d is written in new line
For example, let's say the stable_revision
value is 22
, then in the SH script echo
I am getting the value as, 例如,假设
stable_revision
值为22
,那么在SH脚本echo
我得到的值为:
xyz2 = 22
/d
I want the value to be xyz2 = 22/d
我希望值是
xyz2 = 22/d
You can use .trim()
to strip off a trailing newline. 您可以使用
.trim()
删除尾随的换行符。
environment {
stable_revision = sh(script: 'curl -H "Authorization: Basic $base64encoded" "https://xyz.sad" | jq -r "name"', returnStdout: true).trim()
}
returnStdout (optional) : If checked, standard output from the task is returned as the step value as a String, rather than being printed to the build log.
returnStdout(可选) :如果选中,则任务的标准输出将作为步骤值作为String返回,而不是打印到构建日志中。 (Standard error, if any, will still be printed to the log.) You will often want to call .trim() on the result to strip off a trailing newline.
(如果有标准错误,仍然会打印到日志中。)您通常会希望在结果上调用.trim()来删除尾随的换行符。
https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script
If you use bash
instead of sh
for your commands, you can benefit from Bash's built-in string transformations 如果您在命令中使用
bash
而不是sh
,则可以从Bash的内置字符串转换中受益
Here it trims all trailing characters from the [:space:]
class witch includes actual spaces and newlines. 在这里,它修剪了
[:space:]
类中所有结尾的字符,其中包括实际的空格和换行符。
echo "xyz2 = ${stable_revision%%[[:space:]]}/d"
If $stable_revision
is always an integer, you can force the shell to use it like an integer with: 如果
$stable_revision
始终是整数,则可以通过以下方式强制外壳将其像整数一样使用:
echo "xyz2 = $((stable_revision))/d"
If you are sure that $stable_revision
contains no space, you can force the shell to trim all spaces by using it like a table element: 如果您确定
$stable_revision
包含任何空格,则可以通过像表元素一样使用它来强制外壳修剪所有空格:
sr=($stable_revision); echo "xyz2 = ${sr[0]}/d"
You can also use the automatic trimming of a sub-shell returned value, that would trim any leading, trailing and duplicate spaces in-between: 您还可以使用自动修整子外壳程序返回值的方法,这将修整中间的任何前导,尾随和重复的空格:
echo "xyz2 = $(echo ${stable_revision})/d"`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.