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

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


当前回答

在我的开发设置中(缺少合适的Groovy编辑器),大量的Jenkinsfile问题源于简单的语法错误。要解决这个问题,你可以根据Jenkins实例验证Jenkinsfile(运行在$JENKINS_HTTP_URL):

curl -X POST -H $(curl '$JENKINS_HTTP_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') -F "jenkinsfile=< jenkinsfile " $JENKINS_HTTP_URL/管道模型转换器/验证

上面的命令是对 -a-Declarative-Jenkinsfile-from-the-command-line https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Validating- (or-linting)

其他回答

在我的开发设置中(缺少合适的Groovy编辑器),大量的Jenkinsfile问题源于简单的语法错误。要解决这个问题,你可以根据Jenkins实例验证Jenkinsfile(运行在$JENKINS_HTTP_URL):

curl -X POST -H $(curl '$JENKINS_HTTP_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') -F "jenkinsfile=< jenkinsfile " $JENKINS_HTTP_URL/管道模型转换器/验证

上面的命令是对 -a-Declarative-Jenkinsfile-from-the-command-line https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Validating- (or-linting)

将SSH密钥放入Jenkins配置文件,然后使用声明式linter,如下所示:

ssh jenkins.hostname.here declarative-linter < Jenkinsfile

这将对你的詹金斯档案进行静态分析。在您选择的编辑器中,定义一个自动运行该命令的键盘快捷方式。在Visual Studio Code中,这是我使用的,转到Tasks > Configure Tasks,然后使用以下JSON创建一个Validate Jenkinsfile命令:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Validate Jenkinsfile",
      "type": "shell",
      "command": "ssh jenkins.hostname declarative-linter < ${file}"
    }
  ]
}

据我所知,这个管道插件是新的Jenkinsfile机制的“引擎”,所以我非常肯定你可以用它来本地测试你的脚本。

我不确定是否有任何额外的步骤,当你复制到一个Jenkinsfile,但语法等应该完全相同。

编辑:在“引擎”上找到了参考,检查这个功能描述,最后一段,第一个条目。

有点晚了,但这就是为什么我写了jenny,一个小的重新实现一些核心的Jenkinsfile步骤。(https://github.com/bmustiata/jenny)

为了简单起见,您可以在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的工作。