码迷,mamicode.com
首页 > 其他好文 > 详细

Jenkins pipeline shared library

时间:2018-02-10 18:18:23      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:ocs   likely   before   lex   rar   aries   access   through   扩展性   

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 the Jenkinsfile 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‘).

 

Jenkins pipeline shared library

标签:ocs   likely   before   lex   rar   aries   access   through   扩展性   

原文地址:https://www.cnblogs.com/lightsong/p/8439277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!