Man*_*ngh 18 groovy jenkins jenkins-pipeline
我正在尝试创建一个Jenkins管道,我需要执行多个shell命令并在下一个命令中使用一个命令的结果.我发现将命令包装在一对三个单引号中'''
可以完成相同的操作.但是,在使用管道将一个命令的输出提供给另一个命令时,我遇到了问题.例如
stage('Test') {
sh '''
echo "Executing Tests"
URL=`curl -s "http://localhost:4040/api/tunnels/command_line" | jq -r '.public_url'`
echo $URL
RESULT=`curl -sPOST "https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=$URL" | jq -r '.code'`
echo $RESULT
'''
}
Run Code Online (Sandbox Code Playgroud)
带管道的命令无法正常工作.这是jenkins控制台输出:
+ echo Executing Tests
Executing Tests
+ curl -s http://localhost:4040/api/tunnels/command_line
+ jq -r .public_url
+ URL=null
+ echo null
null
+ curl -sPOST https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=null
Run Code Online (Sandbox Code Playgroud)
Man*_*ngh 12
我尝试在jenkins片段生成器中为管道输入所有这些命令,它给出了以下输出:
sh ''' echo "Executing Tests"
URL=`curl -s "http://localhost:4040/api/tunnels/command_line" | jq -r \'.public_url\'`
echo $URL
RESULT=`curl -sPOST "https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=$URL" | jq -r \'.code\'`
echo $RESULT
'''
Run Code Online (Sandbox Code Playgroud)
注意命令jq -r \'.public_url\'
和中 的转义单引号jq -r \'.code\'
.以这种方式使用代码解决了这个问题
更新::经过一段时间甚至开始出现问题.在这些命令之前执行某些命令.其中一个是grunt serve
,而另一个是./ngrok http 9000
.我在每个命令之后添加了一些延迟,它现在解决了问题.
以下场景显示了一个可能需要使用多行 shell 命令的真实示例。也就是说,假设您正在使用类似的插件,Publish Over SSH
并且您需要在单个 SSH 会话中在目标主机中执行一组命令:
stage ('Prepare destination host') {
sh '''
ssh -t -t user@host 'bash -s << 'ENDSSH'
if [[ -d "/path/to/some/directory/" ]];
then
rm -f /path/to/some/directory/*.jar
else
sudo mkdir -p /path/to/some/directory/
sudo chmod -R 755 /path/to/some/directory/
sudo chown -R user:user /path/to/some/directory/
fi
ENDSSH'
'''
}
Run Code Online (Sandbox Code Playgroud)
特别说明:
ENDSSH'
前面不应有任何字符。所以它应该在新行的起始位置。ssh -t -t
,如果你有sudo
远程shell命令中 归档时间: |
|
查看次数: |
45944 次 |
最近记录: |