如何在 Jenkins 管道中加载 bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47455978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:38:09  来源:igfitidea点击:

How to load bash script in Jenkins pipeline?

bashjenkinsjenkins-pipeline

提问by DenCowboy

We have some complex bash script which is now in our managed files section in Jenkins. We try to migrate the job to a pipeline but we don't know enough to translate the bash script to groovy so we want to keep this in bash. We have a jenkins-shared-library in Git in which we store our pipeline templates. Inside the job we add the right environment variables.

我们有一些复杂的 bash 脚本,现在位于 Jenkins 的托管文件部分。我们尝试将作业迁移到管道,但我们不知道将 bash 脚本转换为 groovy,因此我们希望将其保留在 bash 中。我们在 Git 中有一个 jenkins-shared-library,我们在其中存储我们的管道模板。在作业中,我们添加了正确的环境变量。

We want to keep our bash script in git instead of in the managed files. What is the right way to load this script in the pipeline and execute it? We tried some stuff with libraryResource, but we didn't manage to make it work. Where do we have to put the test.shscript in git and how can we call it? (or is it completely wrong to run a shell script here)

我们希望将 bash 脚本保存在 git 中而不是托管文件中。在管道中加载此脚本并执行它的正确方法是什么?我们尝试了一些东西libraryResource,但我们没有设法让它工作。我们必须将test.sh脚本放在 git 中的什么位置以及如何调用它?(或者在这里运行shell脚本是完全错误的)

def call(body) {
    // evaluate the body block, and collect configuration into the object
    def pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {
        agent any

        options {
            buildDiscarder(logRotator(numToKeepStr: '3'))
        }

        stages {

            stage ('ExecuteTestScript') {
                steps {
                    def script = libraryResource 'loadtestscript?'

                    script {
                        sh './test.sh'
                    }
                }
            }

        }

        post {
            always {
                cleanWs()
            }

        }

    }
}

回答by Barizon

In my company we also have complex bash scripts in our CI and libraryResourcewas a better solution. Following your script, you can perform some changes in order to use a bashscript stored into libraryResource:

在我的公司,我们的 CI 中也有复杂的 bash 脚本,这libraryResource是一个更好的解决方案。按照您的脚本,您可以执行一些更改以使用bash存储到的脚本libraryResource

stages {
    stage ('ExecuteTestScript') {
        steps {
            // Load script from library with package path
            def script_bash = libraryResource 'com/example/loadtestscript'

            // create a file with script_bash content
            writeFile file: './test.sh', text: script_bash

            // Run it!
            sh 'bash ./test.sh'
        }
    }
}

回答by Ena

I want to elaborate on @Barizon answer, that pointed me in the right direction.

我想详细说明@Barizon 的答案,这为我指明了正确的方向。

My need was to execute the script on a remote service with ssh.

我的需要是使用 ssh 在远程服务上执行脚本。

I created a groovy script inside the /varfolder of the shared library project, let's call it my_script.groovy.

我在/var共享库项目的文件夹中创建了一个 groovy 脚本,我们称之为my_script.groovy.

Inside the script i defined the funciton:

在脚本中,我定义了函数:

def my_function(String serverIp, String scriptArgument) {
    def script_content = libraryResource 'my_scripts/test.sh'
    // create a file with script_bash content
    writeFile file: './test.sh', text: script_content
    echo "Execute remote script test.sh..."
    def sshCommand = "ssh username@${serverIp} \'bash -xs\' < ./test.sh ${scriptArgument}"
    echo "Ssh command is: ${sshCommand}"
    sh(sshCommand)
}

From the pipeline I can invoke it like this:

从管道中,我可以像这样调用它:

@Library('MySharedLibrary')_
pipeline {
  agent any
  stages {
    stage('MyStage') {
        steps {
            script {
                my_script.my_function("192.168.1.1", "scriptArgumentValue")
            }
        }
    }
  }
}