我的问题是:如何从连接到远程计算机的SSH session 中导出变量,以在具有本地主机的 shell 上使用它,而所有这些都在jenkins上?
我的SSH脚本如下:

cd /home/$USER/link
docker-compose -f link.yml up -d #this is a script, in my case I use a docker command, the file exists and is copied in another post
echo the application is available at:
CID=$(docker ps -qf "name=webapp1" |tail -1)
app_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CID) #to get the ip address of the last container
echo $app_ip:8080

我想在 shell 上使用$app_ip,在主机终端上使用AKA,但我不知道如何进行操作,我的尝试是使用ssh -t root@192.168.197.141 env app_ip='$(echo $app_ip)' bash -i,但显然它需要身份验证,我认为在jenkins上是不可能的,您能指出吗?我朝着正确的方向?谢谢

最佳答案

如果您有一个名为 script.sh 的脚本,则可以使用$ 1,$ 2等或$ @获取输入参数,以获取所有参数。

echo "Hi, the first argument is $1"
echo "Hi, all the arguments are: $@"

您可以将参数传递给它,只需将它们附加在同一行即可:
bash script.sh "master" "production"

结果将是:
Hi, the first argument is master
Hi, all the arguments are: master production

使用SSH远程执行

如果您的 script.sh 位于远程计算机 108.107.106.105 / devops文件夹中,用户名为 my_user ,则可以执行该命令并通过以下行发送参数:
ssh -i /tmp/myKey my_user@108.107.106.105 /devops/script.sh "master" "production"

结果将是相同的。

关于shell - 如何从远程计算机上的jenkins中的SSH将变量导出到本地计算机上的shell?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55223692/

10-14 10:12