Yag*_*ias 37 linux bash shell groovy jenkins
在groovy脚本中(对于jenkins管道):如何运行bash命令而不是sh命令?
我尝试过以下方法:
在通话中拨打" #!/bin/bash" sh:
stage('Setting the variables values') {
steps {
sh '''
#!/bin/bash
echo "hello world"
'''
}
}
Run Code Online (Sandbox Code Playgroud)
用sh呼叫替换bash呼叫:
stage('Setting the variables values') {
steps {
bash '''
#!/bin/bash
echo "hello world"
'''
}
}
Run Code Online (Sandbox Code Playgroud)
附加信息:
我的命令比a更复杂echo hello world.
Jak*_*ake 39
您提供的Groovy脚本将第一行格式化为结果脚本中的空行.shebang告诉脚本使用/ bin/bash而不是/ bin/sh运行,需要位于文件的第一行,否则将被忽略.
所以相反,你应该像这样格式化你的Groovy:
stage('Setting the variables values') {
steps {
bash '''#!/bin/bash
echo "hello world"
'''
}
}
Run Code Online (Sandbox Code Playgroud)
它将使用/ bin/bash执行.
Jac*_*cob 10
根据这个文档,您应该能够这样做:
node {
sh "#!/bin/bash \n" +
"echo \"Hello from \$SHELL\""
}
Run Code Online (Sandbox Code Playgroud)
我确信上述答案完美无缺。但是,我很难将双引号添加为接近 100 的 bash 行。因此,以下方法对我有所帮助。(简而言之,shell 的每一行周围没有双引号)
另外,当我在步骤中使用“bash '''#!/bin/bash”时,出现以下错误 java.lang.NoSuchMethodError: No such DSL method '**bash**' found among steps
pipeline {
agent none
stages {
stage ('Hello') {
agent any
steps {
echo 'Hello, '
sh '''#!/bin/bash
echo "Hello from bash"
echo "Who I'm $SHELL"
'''
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面执行的结果是
对于多行 shell 脚本或多次运行的脚本,我会创建一个新的 bash 脚本文件(从 开始#!/bin/bash),然后简单地sh从 Jenkinsfile运行它:
sh 'chmod +x ./script.sh'
sh './script.sh'
Run Code Online (Sandbox Code Playgroud)
如果您想将 Jenkins 上所有项目的默认 shell 更改为bash,您可以通过 Web 门户在 Jenkins 配置中执行此操作:
管理 Jenkins > 配置系统(如果您愿意,只需转到 即可跳过此单击https://{YOUR_JENKINS_URL}/configure。)
在标记为“Shell 可执行文件”的字段中填写值/bin/bash,然后单击“保存”。