简体   繁体   English

在詹金斯管道上运行 bash 命令

[英]Run bash command on jenkins pipeline

Inside a groovy script (for a jenkins pipeline): How can I run a bash command instead of a sh command?在 groovy 脚本中(对于 jenkins 管道):如何运行bash命令而不是sh命令?

I have tried the following:我尝试了以下方法:

Call " #!/bin/bash " inside the sh call:sh调用中调用“ #!/bin/bash ”:

stage('Setting the variables values') {
    steps {
         sh '''
            #!/bin/bash
            echo "hello world"
         '''
    }
}

Replace the sh call with a bash call:bash调用替换sh调用:

stage('Setting the variables values') {
    steps {
         bash '''
            #!/bin/bash
            echo "hello world"
         '''
    }
}

Additional Info:附加信息:

My command is more complex than a echo hello world .我的命令比echo hello world更复杂。

The Groovy script you provided is formatting the first line as a blank line in the resultant script.您提供的 Groovy 脚本将生成脚本中的第一行格式化为空行。 The shebang, telling the script to run with /bin/bash instead of /bin/sh, needs to be on the first line of the file or it will be ignored.告诉脚本使用 /bin/bash 而不是 /bin/sh 运行的 shebang 需要位于文件的第一行,否则将被忽略。

So instead, you should format your Groovy like this:因此,您应该像这样格式化 Groovy:

stage('Setting the variables values') {
    steps {
         sh '''#!/bin/bash
                 echo "hello world" 
         '''
    }
}

And it will execute with /bin/bash.它将使用 /bin/bash 执行。

According to this document , you should be able to do it like so:根据此文档,您应该可以这样做:

node {
    sh "#!/bin/bash \n" + 
       "echo \"Hello from \$SHELL\""
}

For multi-line shell scripts or those run multiple times, I would create a new bash script file (starting from #!/bin/bash ), and simply run it with sh from Jenkinsfile:对于多行 shell 脚本或多次运行的脚本,我将创建一个新的 bash 脚本文件(从#!/bin/bash开始),然后使用 Jenkinsfile 中的sh简单地运行它:

sh 'chmod +x ./script.sh'
sh './script.sh'

I'm sure that the above answers work perfectly.我确信上述答案完美无缺。 However, I had the difficulty of adding the double quotes as my bash lines where closer to 100. So, the following way helped me.但是,我很难将双引号添加为接近 100 的 bash 行。因此,以下方式对我有所帮助。 (In a nutshell, no double quotes around each line of the shell) (简而言之,shell 的每一行都没有双引号)

Also, when I had "bash '''#!/bin/bash" within steps, I got the following error java.lang.NoSuchMethodError: No such DSL method '**bash**' found among steps此外,当我在步骤中有“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"
                '''
            }
        }
    }
}

The result of the above execution is上述执行的结果是

在此处输入图像描述

If you want to change your default shell to bash for all projects on Jenkins, you can do so in the Jenkins config through the web portal:如果您想将 Jenkins 上所有项目的默认 shell 更改为bash ,您可以通过 Web 门户在 Jenkins 配置中执行此操作:

Manage Jenkins > Configure System (Skip this clicking if you want by just going to https://{YOUR_JENKINS_URL}/configure .)管理 Jenkins > 配置系统(如果需要,只需转到https://{YOUR_JENKINS_URL}/configure即可跳过此单击。)

Fill in the field marked 'Shell executable' with the value /bin/bash and click 'Save'.使用值/bin/bash填写标记为“Shell executable”的字段,然后单击“Save”。

In my case, I had to execute a Shell script in bash via jenkinsfile .就我而言,我必须通过jenkinsfile在 bash 中执行一个 Shell 脚本。 Here's what worked for me :这对我有用:

sh 'core_devops/automation/scripts/ecs_initialize.sh'

And the first line in the script had脚本的第一行有

#!/bin/bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM