在编写jenkins管道时,提交每个新更改以查看它是否有效似乎非常不方便。
是否有一种方法可以在本地执行这些而不提交代码?
在编写jenkins管道时,提交每个新更改以查看它是否有效似乎非常不方便。
是否有一种方法可以在本地执行这些而不提交代码?
当前回答
为了简单起见,您可以在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的工作。
其他回答
您不能在本地执行Pipeline脚本,因为它的全部目的是编写Jenkins脚本。(这就是为什么Jenkins文件最好保持简短,并限制在实际处理Jenkins特性的代码中;你实际的构建逻辑应该通过外部进程或构建工具来处理,你可以通过一行sh或bat步骤调用它们。)
如果你想测试对Jenkinsfile的修改,但不提交,可以使用1.14中添加的Replay功能。
JENKINS-33925跟踪自动测试框架的特性请求。
除了其他人已经提到的重放功能(关于它的有用性也一样!),我发现以下功能也很有用:
创建一个测试管道作业,您可以在其中键入管道代码或指向Jenkinsfile的repo/分支来快速测试一些东西。为了更准确的测试,使用Multibranch Pipeline,它指向你自己的fork,在那里你可以快速做出改变并提交,而不影响proch。 由于Jenkinsfile是Groovy代码,只需使用“Groovy Jenkinsfile”调用它来验证基本语法。
为了简单起见,您可以在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的工作。
据我所知,这个管道插件是新的Jenkinsfile机制的“引擎”,所以我非常肯定你可以用它来本地测试你的脚本。
我不确定是否有任何额外的步骤,当你复制到一个Jenkinsfile,但语法等应该完全相同。
编辑:在“引擎”上找到了参考,检查这个功能描述,最后一段,第一个条目。
将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}"
}
]
}