Jenkinsfile
https://jenkins.io/doc/book/pipeline/jenkinsfile/
Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines "as code" via the Pipeline DSL.
例子&教程:
https://github.com/ciandcd
http://www.cnblogs.com/itech/p/5875428.html
以脚本形式定义 持续集成步骤,与界面配置相比, 其支持多业务融合, 具有更加的灵活性、扩展性,便于定位集成过程中发现的问题。
但是脚本方式融合了若干业务后, 就徽导致脚本代码繁杂, 不易于维护。 且不能达到业务配置和脚本公共逻辑分离的目的。
为解决这个问题, 存在两种解决方案, 下面分别介绍。
load 加载方式
https://stackoverflow.com/questions/37800195/how-do-you-load-a-groovy-file-and-execute-it
Example.Groovy
def exampleMethod() {
println("exampleMethod")
}
def otherExampleMethod() {
println("otherExampleMethod")
}
return this
JenkinsFile
node {
// Git checkout before load source the file
checkout scm
// To know files are checked out or not
sh ‘‘‘
ls -lhrt
‘‘‘
def rootDir = pwd()
println("Current Directory: " + rootDir)
// point to exact source file
def example = load "${rootDir}/Example.Groovy"
example.exampleMethod()
example.otherExampleMethod()
}
shared library方式
https://jenkins.io/doc/book/pipeline/shared-libraries/
最为推荐。
As Pipeline is adopted for more and more projects in an organization, common patterns are likely to emerge. Oftentimes it is useful to share parts of Pipelines between various projects to reduce redundancies and keep code "DRY"
https://stackoverflow.com/questions/38695237/create-resusable-jenkins-pipeline-script?noredirect=1
The Shared Libraries (docs) allows you to make your code accessible to all your pipeline scripts. You don‘t have to build a plugin for that and you don‘t have to restart Jenkins.
E.g. this is my library and this a Jenkinsfile that calls this common function.
The global library can be configured through the following means:
- an
@Library(‘github.com/...‘)
annotation in theJenkinsfile
pointing to the URL of the shared library repo.- configured on the folder level of Jenkins jobs.
- configured in Jenkins configuration as global library, with the advantage that the code is trusted, i.e., not subject to script security.
A mix of the first and last method would be a not explicitly loaded shared library that is then requested only using its name in the
Jenkinsfile
:@Library(‘mysharedlib‘)
.
原文地址:https://www.cnblogs.com/lightsong/p/8439277.html