Jenkins pipeline shared library
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')
.
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2016-02-10 Salted hash password