使用来自 Git 的代码运行 Jenkins 管道
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46277936/
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
Run Jenkins Pipeline with Code from Git
提问by J.Doe
I want to use following Pipeline script from git in jenkins
我想在 jenkins 中使用来自 git 的以下管道脚本
#!groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
I set the Repository URL right, under "Additional Behaviors" i added "Check out to a sub-directory" and wrote my sub directory there.
我将 Repository URL 设置为正确,在“Additional Behaviors”下我添加了“Check out to a sub-directory”并在那里写下我的子目录。
At "Script-Path" I wrote: mysubdirectory/Jenkinsfile
在“脚本路径”我写道:mysubdirectory/Jenkinsfile
When I try to run it I get following ERROR:
当我尝试运行它时,出现以下错误:
java.io.FileNotFoundException
at jenkins.plugins.git.GitSCMFile.invoke(GitSCMFile.java:167)
at jenkins.plugins.git.GitSCMFile.invoke(GitSCMFile.java:159)
at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:162)
at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:71)
at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:158)
at jenkins.plugins.git.GitSCMFile.content(GitSCMFile.java:159)
at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:101)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:59)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:262)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:415)
Finished: FAILURE
What am I doing wrong?
我究竟做错了什么?
How can I run a Jenkins Script from git correctly?
如何从 git 正确运行 Jenkins 脚本?
回答by Chandra Sekhar Y
To run the Jenkinsfile Successfully from Git repo Jenkinsfile should be available in main directory path,but not in the sub directory. For Example:
从 Git 存储库成功运行 Jenkinsfile Jenkinsfile 应该在主目录路径中可用,但不在子目录中。例如:
.
├── .setting
├── project
└── Jenkinsfile
Jenkinsfile should not be in the Sub Directory.
Jenkinsfile 不应在子目录中。
回答by Rob Hales
Jenkins does 2 checkouts when it looks for a pipeline script. With git, the first one is often a lightweight checkout that only gets the Jenkinsfile, rather than the whole repo, but it is 2 separate checkouts. The second checkout is the real checkout to run the Jenkinsfile.
Jenkins 在查找管道脚本时会进行 2 次检出。使用 git,第一个通常是轻量级检出,只获取 Jenkinsfile,而不是整个 repo,但它是 2 个单独的检出。第二次结帐是运行 Jenkinsfile 的真正结帐。
The reason it does 2 checkouts is because it has to first look at the Jenkinsfile to see what it is you want to do and validate syntax, etc. If you are skipping an SCM checkout in your script so you can do it later or differently, then it needs to know not to do the "real" checkout. For that matter, you could theoretically pull your Jenkinsfile from one repo, skip the SCM checkout, and pull a completely different repo (or branch, or tag) and build against that, but using the Jenkinsfile from the first checkout.
它执行 2 次签出的原因是因为它必须首先查看 Jenkinsfile 以了解您想要做什么并验证语法等。如果您在脚本中跳过 SCM 签出,以便您可以稍后或以不同方式执行,那么它需要知道不要进行“真正的”结帐。就此而言,理论上您可以从一个存储库中提取 Jenkinsfile,跳过 SCM 检出,然后拉出一个完全不同的存储库(或分支或标签)并针对它进行构建,但使用第一次检出中的 Jenkinsfile。
So by telling Jenkins to look in the subdirectory for the Jenkinsfile, you are telling it to look in someplace in the original checkout that actually doesn't exist, because your Jenkinsfile is really in the root of your git repo.
因此,通过告诉 Jenkins 在 Jenkinsfile 的子目录中查找,您是在告诉它在原始结帐中实际上不存在的某个位置查找,因为您的 Jenkinsfile 确实位于您的 git 存储库的根目录中。
When the second checkout is done into a subdirectory, you will need to take this into account in your Jenkinsfile, because the Jenkinsfile runs from the root of the workspace. You would need to set into the directory i.e. dir("mysubdirectory") {}, to find build files, etc.
当第二次签出到子目录中时,您需要在 Jenkinsfile 中考虑到这一点,因为 Jenkinsfile 从工作区的根目录运行。您需要设置目录,即 dir("mysubdirectory") {},以查找构建文件等。
回答by Carlos Lucas
Try to omit your subdirectory from the Sript-path.
尝试从 Sript-path 中省略您的子目录。
When you specify a subdirectory to clone your project, Jenkins searches the pipeline file in that directory. In your case, Jenkins is looking for the Jenkinsfile in "mysubdirectory/mysubdirectory/Jenkinsfile"
当您指定一个子目录来克隆您的项目时,Jenkins 会在该目录中搜索管道文件。在您的情况下,Jenkins 正在“mysubdirectory/mysubdirectory/Jenkinsfile”中寻找 Jenkinsfile
回答by Coco
I got the same error. By disabling Lightweight checkout
in the job configuration the error was solved!
我得到了同样的错误。通过Lightweight checkout
在作业配置中禁用,错误解决了!
回答by Mike P
I had a similar problem to this, but in my case the naming mismatch was the branch in Git. In the pipeline settings, I was specifying the branch name in uppercase when in the repo, the branch name was lowercase. (Obviously not the solution to the original problem, but may prove useful to some other poor soul!)
我遇到了类似的问题,但在我的情况下,命名不匹配是 Git 中的分支。在管道设置中,我在 repo 中指定了大写的分支名称,分支名称是小写的。(显然不是原始问题的解决方案,但可能对其他一些可怜的灵魂有用!)
回答by Kimi
I had a similar problem when I copied pipeline job.
我在复制管道作业时遇到了类似的问题。
solved: remove pipeline scm part from copied pipeline job. save changes. build one empty round. then create pipeline scm block again.
已解决:从复制的管道作业中删除管道 scm 部分。保存更改。建立一个空回合。然后再次创建管道 scm 块。