[英]How to load a Groovy script from a git repo in a Jenkinsfile that is in a different git repo
So I have a groovy script called "deployer.groovy" that is in a git repository called "jenkins-pipeline-library".所以我有一个名为“deployer.groovy”的 groovy 脚本,它位于名为“jenkins-pipeline-library”的 git 存储库中。 ( https://github.com/xyzDev/jenkins-pipeline-library ) there is nothing else in this repository just this groovy file in the main branch.
( https://github.com/xyzDev/jenkins-pipeline-library )这个存储库中没有其他东西,只有主分支中的这个 groovy 文件。
Also, I have a Jenkinsfile that is in a different git repository.另外,我有一个 Jenkinsfile,它位于不同的 git 存储库中。 I cannot put both of these file in a same Git repository.
我不能将这两个文件放在同一个 Git 存储库中。
(because im not allowed to, the idea is to be able to run this deployer.groovy by using Jenkinsfile so that people dont see the content of the deployer.groovy but be able to use it) (因为我不允许这样做,所以我们的想法是能够通过使用 Jenkinsfile 运行这个 deployer.groovy,这样人们就看不到 deployer.groovy 的内容但能够使用它)
I am trying to load this deployer.groovy in my Jenkinsfile and then run it.我正在尝试在我的 Jenkinsfile 中加载这个 deployer.groovy 然后运行它。
Is there any way to do this?有没有办法做到这一点? Please any suggestions would be highly appreciated.
请任何建议将不胜感激。
There are several ways to achive this.有几种方法可以实现这一点。
Your jenkins-pipeline-library
could be git-submodule in other repositories.您的
jenkins-pipeline-library
可能是其他存储库中的 git-submodule 。
git submodule add -b master https://github.com/xyzDev/jenkins-pipeline-library jenkins-pipeline-library
On your Jenkins-Server under Manage Jenkins -> Configure System -> Global Pipeline Libraries
you can add your repostitory.在您的 Jenkins-Server 上的
Manage Jenkins -> Configure System -> Global Pipeline Libraries
下,您可以添加您的存储库。
After that in any jenkinsfile you can use it like this之后,在任何 jenkinsfile 中,您都可以像这样使用它
import JenkinsPipelineLibrary // Name depends on the actual name of the file
This is an example from a pipeline script I made:这是我制作的管道脚本的一个示例:
import utils.build.PipelineUtil
PIPELINE_UTIL = new PipelineUtil()
properties(
[
[
$class: 'BuildDiscarderProperty',
strategy: [$class: 'LogRotator', numToKeepStr: '25']
],
PIPELINE_UTIL.getReleaseTrigger('com.logicals.cloud.server')
]
)
Note: The PipelineUtil
is located in the repository under utils/build/PipelineUtil.groovy.注意:
PipelineUtil
位于 utils/build/PipelineUtil.groovy 下的存储库中。
Official documentation官方文档
Extending with Shared Libraries is the documentation that I would recommend for you to understand and achieve what you need. Extending with Shared Libraries是我建议您理解和实现所需的文档。
Explanation解释
Jenkins configuration: Go to jenkins-url/configure
-> Global Pipeline Libraries
in this section you can setup the library: using libraries , retrieval method Jenkins 配置: Go 到
jenkins-url/configure
-> Global Pipeline Libraries
在本节中您可以设置库: 使用库, 检索方法
Library repository: Shared library repository should have the .groovy
files in specific folder structure, for your use case you need this:库存储库:共享库存储库应具有特定文件夹结构中的
.groovy
文件,对于您的用例,您需要:
(root)
+- vars
| +- foo.groovy # for global 'foo' variable
| +- MyDeclarativePipeline.groovy # for global 'MyDeclarativePipeline' variable
vars/foo.groovy
#!/usr/bin/env groovy
def test() {
// define logic here
}
def deployInternal() {
// define logic here
}
vars/MyDeclarativePipeline.groovy
#!/usr/bin/env groovy
def call() {
/* insert your pipeline here */
pipeline {
agent any
stages {
stage('Test') {
steps {
// input the logic here or
foo.test()
}
}
stage('Deploy Internal') {
steps {
// input the logic here or
foo.deployInternal()
}
}
}
}
}
Jenkinsfile:詹金斯文件:
@Library('my-shared-library') _
MyDeclarativePipeline()
Note: instead of MyDeclarativePipeline()
you can insert the pipeline {...}
block which was defined in MyDeclarativePipeline.groovy
注意:代替
MyDeclarativePipeline()
您可以插入在MyDeclarativePipeline.groovy
中定义的pipeline {...}
块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.