這個問題在這里已經有了答案: 將 sh 輸出分配給宣告性管道上的變數 1 個回答 5 天前關閉。
我有以下詹金斯檔案:
pipeline {
agent any
environment {
VERSION=""
}
stages {
stage ("release"){
steps {
script {
${VERSION}=sh(returnStdout: true, script:$("./bashscript.sh).trim())
}
}
}
}
我想要做的很簡單,我想呼叫 bash 檔案,該檔案將值回傳給我的 Jenkinsfile 中的環境變數。
我有兩個問題: 1. 如何從 bash 檔案回傳值?2.如何將其插入到Jenkinsfile中的環境變數中?
那是我的 .sh 檔案:
# !/bin/bash
if [ some condition... ]
then
some commands....
return "value"
else
some commands....
return "other value"
fi
找了半天也沒找到,這有可能嗎?
注意:我看到了很多 groovy 的解決方案,但我需要它在管道中......
uj5u.com熱心網友回復:
- 要從 shell 為 jenkins 設定變數示例,請使用returnStdout: true。在您的腳本中回顯您需要的內容
myVal = sh(script: ' ./myFunc.ksh ', returnStdout: true)
- 從 bash 執行函式,因為它們假設回傳一個變數。要使功能可見,您需要源檔案(匯入)示例如下
# for point 2
#/bin/bash
# content of your file ./myfileFunc.ksh
function myFunc {
typeset l_in_param1="$1"
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)
- 腳本將資料回傳到標準輸出和錯誤輸出,因此您可以稍后通過使用 , returnStdout: true 從那里讀取將其存盤在變數中
myVal = sh(腳本:'./myFunc.ksh',returnStdout:true)
# content of your file ./myFunc.ksh
typeset l_in_param1="$1"
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 將為空,因為我們將錯誤移至錯誤輸出
代碼示例的執行
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318834.html
