在Jenkins声明式管道中使用构建基块 [英] Using Building Blocks in Jenkins Declarative Pipeline

查看:102
本文介绍了在Jenkins声明式管道中使用构建基块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从使用Jenkins声明式管道开始.当我支持一些类似的项目时,我正在考虑将类似的管道步骤(甚至阶段)放入可重用的构建块中.这些区块应维护在中央位置,然后由各个管道包含(例如:DRY).

I'm just starting with using Jenkins declarative pipelines. As I'm supporting a few similar projects I was thinking of putting similar pipeline steps (or even stages) into reusable building blocks. These blocks should be maintained at a central spot and then included by individual pipelines (speak: DRY).

我将共享库视为脚本管道的一个选项,但是我不确定它是否也适用于声明性管道.

I saw shared libraries as an option for scripted pipelines but I'm not sure if it works for declarative pipelines, too.

您知道在Jenkins声明性管道中使用诸如构建基块之类的方法吗?

Do you know a way to use something like building blocks in Jenkins declarative pipelines?

要阐明的示例:

如果您有用于Maven项目的标准管道(例如Spring Boot),则看起来像这样:

If you got a standard pipeline for Maven projects (e. g. Spring Boot), it looks somewhat like:

pipeline {
    agent {
        dockerfile true
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -U -DskipTests clean package'
            }
        }
        stage('Test') {
            parallel {
                stage('Unit Tests') {
                    steps {
                        sh 'mvn test'
                    }
                }
                stage('Integration Tests') {
                    steps {
                        sh 'mvn integration-test'
                    }
                }
            }
        }
        stage('Deploy') {
            steps {
                sh 'mvn deploy'
            }
        }
    }
}

但是,如果可以轻松处理以下用例,那么最好将其复制到所有项目中.

But instead of copying this to all projects, it would be great if following use cases could be easy handled.

对于无需自定义的项目,最好将其用于示例,例如:

For a project without need to customize it would be good to use it for examle like:

defaultMavenpipeline{}

其中defaultMavenpipeline将被上述管道替换(我认为使用共享库是可能的).

where defaultMavenpipeline will be replaced by above pipeline (I think this is possible with shared libraries).

对于仅需要自定义某些阶段的项目,这样的事情可能发生吗?

For a project with need to customize only some stages would something like this be possible?

pipeline {
    defaultDockerAgent{}
    stages {
        stage('Build') {
            steps {
                sh 'mvn -U -DskipTests clean package'
                // ... customize some stuff ...
            }
        }
        defaultTestStage{}
        stage('Deploy') {
            steps {
                // ... customize some stuff ...
                sh 'mvn deploy'
            }
        }
    }
}

很抱歉,很长的帖子,非常感谢!

Sorry for long post and thank you very much in advance!

推荐答案

您可以在Shared-Library中非常使用声明性管道.

You can very much use declarative pipeline with Shared-Library.

请按照以下步骤操作:

1)创建一个共享库

  • 创建存储库共享库
  • 在上述存储库中创建一个名为vars的目录.在vars目录中,创建具有以下内容的文件sayHello.groovy:
  • Create a repository shared-lib
  • Create a directory named vars in above repository. Inside vars directory, create a file sayHello.groovy with the following content:
// vars/sayHello.groovy
def call(String name = 'human') {
    // Any valid steps can be called from this code.
    // Can be used in both scripted and declarative pipeline
    echo "Hello, ${name}."
}

2)配置Jenkins以在任何管道作业中访问共享库

2) Configure Jenkins for accessing Shared Library in any pipeline job

  • 转到管理Jenkins»配置系统»全局管道库"部分
  • 根据需要命名库(在我的情况下为my-shared-library,如下所示)
  • 添加包含您的Groovy代码的分支名称.就我而言,它位于默认分支中,即master
  • 除非您知道自己在做什么,否则无需选中/取消选中复选框
  • Go to Manage Jenkins » Configure System » Global Pipeline Libraries section
  • Name the library whatever you want (in my case, my-shared-library as shown below)
  • Add the branch name that contains your Groovy code. In my case, it was in the default branch i.e., master
  • No need to check/uncheck the check-boxes unless you know what you're doing

3)访问您项目中的共享库

3) Access shared library in your project

  • 在Jenkinsfile或Pipeline script部分中,添加以下代码:
  • In Jenkinsfile or Pipeline script section, add the following code:
@Library('my-shared-lib')_

pipeline {
    agent any

    stages {
        stage('Info') {
            steps {
                echo 'Publishing some details here'
            }
        }

        stage('Using shared-library') {
            steps {
                sayHello 'Alex'
            }
        }
    }
}

就是这样.你完成了! :)

That's it. You're done! :)

注意:对于上述Jenkinsfile共享库中使用的下划线(_),来自

Note: For the the underscore (_) used in shared-library above in Jenkinsfile, from official link, for Shared Libraries which only define Global Variables (vars/), or a Jenkinsfile which only needs a Global Variable, the annotation pattern @Library('my-shared-library') _ may be useful for keeping code concise. In essence, instead of annotating an unnecessary import statement, the symbol _ is annotated.

输出:

这篇关于在Jenkins声明式管道中使用构建基块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆