在编写jenkins管道时,提交每个新更改以查看它是否有效似乎非常不方便。

是否有一种方法可以在本地执行这些而不提交代码?


当前回答

我使用重放未来,做一些更新和运行迅速。

其他回答

这是一个简短的解决方案,可以让我非常快速地测试管道代码:

pipeline {
    agent any
    options {
        skipDefaultCheckout true
        timestamps()
    }
    parameters {
        text(name: 'SCRIPT', defaultValue: params.SCRIPT,
            description: 'Groovy script')
    }
    stages {
        stage("main") {
            steps {
                script {
                    writeFile file: 'script.groovy', 
                        text: params.SCRIPT
                    def groovyScript = load 'script.groovy'
                    echo "Return value: " + groovyScript
                }
            } // steps
        } // stage
    } // stages
} // pipeline

skipDefaultCheckout为true,因为我们不需要这个工具git存储库中的文件。 defaultValue:参数。SCRIPT将默认值设置为最近一次执行。如果只有一个用户使用,它允许非常快速的短测试周期。 给定的脚本被写入一个文件,并加载和加载。

有了这个设置,我可以在其他jenkins文件中测试我能做的所有事情,包括使用共享库。

Jenkins有一个“重放”功能,它可以让你快速重放作业而不更新源:

有一些限制,对于脚本管道,我使用这个解决方案:

使用内联groovy脚本的管道作业:


node('master') {
    stage('Run!') {
                def script = load('...you job file...')
    }
}

用于测试的Jenkinsfile具有与lesfuret相同的结构:


def execute() {
 ... main job code here ...
}
execute()

博士TL;

Jenkins Pipeline单元测试框架 Jenkinsfile跑步

长版本 管道测试越来越让人头疼。不同于经典的声明式作业配置方法(其中用户仅限于UI所公开的内容),新的Jenkins Pipeline是一种用于构建过程的成熟编程语言,您可以将声明式部分与自己的代码混合在一起。作为优秀的开发人员,我们也希望对这类代码进行单元测试。

在开发Jenkins pipeline时,您应该遵循三个步骤。第一步。应该覆盖80%的用例。

Do as much as possible in build scripts (eg. Maven, Gradle, Gulp etc.). Then in your pipeline scripts just calls the build tasks in the right order. The build pipeline just orchestrates and executes the build tasks but does not have any major logic that needs a special testing. If the previous rule can't be fully applied then move over to Pipeline Shared libraries where you can develop and test custom logic on its own and integrate them into the pipeline. If all of the above fails you, you can try one of those libraries that came up recently (March-2017). Jenkins Pipeline Unit testing framework or pipelineUnit (examples). Since 2018 there is also Jenkinsfile Runner, a package to execution Jenkins pipelines from a command line tool.

例子

pipelineUnit GitHub回购包含了一些关于如何使用Jenkins Pipeline Unit测试框架的Spock示例

为了简单起见,您可以在git存储库的根目录下创建一个Jenkinsfile,类似于下面基于声明性管道的groovy语法的示例'Jenkinsfile'。

pipeline {

    agent any

    stages {
        stage('Build the Project') {
            steps {
                git 'https://github.com/jaikrgupta/CarthageAPI-1.0.git'
                echo pwd()
                sh 'ls -alrt'
                sh 'pip install -r requirements.txt'
                sh 'python app.py &'
                echo "Build stage gets finished here"
            }
        }
        stage('Test') {
            steps {
                sh 'chmod 777 ./scripts/test-script.sh'
                sh './scripts/test-script.sh'
                sh 'cat ./test-reports/test_script.log'
                echo "Test stage gets finished here"
            }
        }
}

https://github.com/jaikrgupta/CarthageAPI-1.0.git

现在可以在Jenkins中将一个新项目设置为Pipeline作业。 从SCM和Git中选择SCM选项的Definition as Pipeline脚本。 将项目的git repo链接粘贴到Repository URL中,并将Jenkinsfile粘贴到脚本名称框中。 然后单击轻量级签出选项并保存项目。 因此,无论何时将commit推到git repo,您都可以在Jenkins中运行Build Now测试更改。

请按照以下视觉效果中的说明轻松设置Jenkins Pipeline的工作。