Ssh

在 Jenkins-job 中的遠端 SSH 命令中傳遞變數

  • March 21, 2020

我有一個小測試工作:

touch 22.txt
echo "wwe" > 22.txt
test=$(echo ${BUILD_NUMBER})
echo $test
ssh jenkins@srv1 "mkdir D:\myfolder\$test"
ssh jenkins@srv1 "dir D:\myfolder\"

BUILD_NUMBER表示詹金斯的工作數量

我在輸出中的內容:

Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building remotely on MAC-slave in workspace /Users/Admin/workspace/testmacosx
[testmacosx] $ /bin/sh -xe /var/folders/rq/4q3q1m2959q6zpgxghhrvdnw0000gn/T/jenkins1551827609931995498.sh
+ touch 22.txt
+ echo wwe
++ echo 5
+ test=5
+ echo 5
5
+ ssh jenkins@srv1 'mkdir D:\myfolder$test'
/var/folders/rq/4q3q1m2959q6zpgxghhrvdnw0000gn/T/jenkins1551827609931995498.sh: line 7: unexpected EOF while looking for matching `"'
Build step 'Execute shell' marked build as failure
Finished: FAILURE

你已經看到了,它$test在遠端 ssh 命令中看不到這個變數。我怎樣才能傳遞這個變數?你能幫我嗎 ?

解決。通過這個: ssh jenkins@srv1 "mkdir D:\myfolder\\$test" 所以,我們必須\在變數之前添加

引用自:https://serverfault.com/questions/968732