当前位置:网站首页>35-Jenkins-Shared library application
35-Jenkins-Shared library application
2022-07-31 08:29:00 【Love learning de test Xiaobai】
前言
- 本篇来学习Jenkins中使用共享库
Reason for using shared library
- As the pipeline is adopted by more and more projects in the organization,Common patterns are likely to emerge. Sharing pipelines across multiple projects helps reduce redundancy and preserve code
- 共享库目录结构
(root)
+- src # Groovy source files
| +- org
| +- foo
| +- Bar.groovy # for org.foo.Bar class
+- vars
| +- foo.groovy # for global 'foo' variable
| +- foo.txt # help for 'foo' variable
+- resources # resource files (external libraries only)
| +- org
| +- foo
| +- bar.json # static helper data for org.foo.Bar
- srcDirectories are similar to standardsJava源目录结构.执行流水线时,This directory will be added to the classpath.
- vars目录托管脚本文件,These script files are in“管道”is exposed as a variable.
- resources目录允许1ibraryResourceUse a step from an external library to load the associated nonGroovy文件.
定义共享库
- 目录: src/org/devops/tools.groovy
pacakge org.devops // 不可省略
//打印信息
def PrintMsg(msg){
println(msg)
}
//Print information with color 需安装ansiColor插件
def Post(value,color){
colors = ['red' : "\033[40;31m >>>>>>>>>>>${value}<<<<<<<<<<< \033[0m",
'blue' : "\033[47;34m ${value} \033[0m",
'green' : "\033[40;32m >>>>>>>>>>>${value}<<<<<<<<<<< \033[0m" ]
ansiColor('xterm') {
println(colors[color])
}
}
- 目录:src/org/devops/GlobalVars.groovy
package org.devops //This specifies the directory location where the file is located
class GlobalVars {
//The name should be the same as the file name
static String name = "大海"
static int age = 28
static String city = "北京"
}
- 目录:vars/sayHello.groovy
def call(String name) {
echo "Hello, ${name}!"
}
- 目录:vars/log.groovy
def info(message) {
echo "INFO: ${message}"
}
def warning(message) {
echo "WARNING: ${message}"
}

使用共享库
1. 配置共享库
- Manage Jenkins --> Configure System -->-Global Pipeline Libraries

- Name :共享库的唯一标识,在Jenkinsfile中会使用到..
- Default version :默认版本.可以是分支名、tag标签等.
- Load implicitly:隐式加载.如果勾选此项,将自动加载全局共享库,在Jenkinsfile中不需要显式引用,就可以直接使用.
- Allow default version to be overridden :如果勾选此项,则表示允许“Default version”被Jenk-insfile中的配置覆盖.
- [email protected] changes in job recent changes:如果勾选此项,那么共享库的最后变更信息会跟项目的变更信息一起被打印在构建日志中.
- Retrieval method:获取共享库代码的方法.我们选择LegacySCM”选项,进而选择使用GitRepository configuration shared library
2. Pipeline中使用
@Library('[email protected]') _ // _ 不可省略
import org.devops.GlobalVars
def mytools = new org.devops.tools()
pipeline {
agent any
stages {
stage("hello"){
steps{
script{
mytools.Post("This is ShareLib",'green')
println "${GlobalVars.name}"
println "${GlobalVars.age}"
println "${GlobalVars.city}"
}
}
}
stage("world"){
steps{
script{
mytools.PrintMsg("This is ShareLib")
sayHello('大海')
log.info 'Starting'
log.warning 'Nothing to do!'
}
}
}
}
}
- @Library(‘[email protected]’)_
- <version>可以是:
- 分支,如@Library ( ‘[email protected]’).
- tag标签,如@Library ( ‘[email protected]’ )
- git commit id,如@Library ( ‘[email protected]’)
- 因为Jenkins支持同时添加多个共享库,所以@Library注解还允许我们同时引入多个共享库,如:@Library ( [‘global-shared-library’ , '[email protected]]).
边栏推荐
- linux redis6.2.6配置文件
- 【小程序项目开发-- 京东商城】uni-app之商品列表页面 (上)
- SQL语句知识大全
- Super detailed mysql database installation guide
- Unreal基础概念
- MySQL detailed explanation
- 蚂蚁核心科技产品亮相数字中国建设峰会 持续助力企业数字化转型
- Client navicat installation tutorial
- Fund investment advisory business
- Regarding "computing power", this article is worth reading
猜你喜欢

最大似然估计和最小二乘法 含代码

MySql database optimization query tool

SSM框架简单介绍

【云原生】微服务之Feign的介绍与使用

MySQL 5.7 安装教程(全步骤、保姆级教程)

使用PageHelper实现分页查询(详细)

《opencv学习笔记》-- 仿射变换

中软国际携手深开鸿发布(1+1) x N 战略,以数字化、智慧化改变人类生产和生活方式

sqlmap使用教程大全命令大全(图文)

MySQL installation to the last step in the write the configuration file failed?And after the installation steps
随机推荐
力扣 593. 有效的正方形
MySQL 5.7详细下载安装配置教程
Reimbursement Process | By Tianfang
基于golang的swagger超贴心、超详细使用指南【有很多坑】
MySQL 5.7升级到8.0详细过程
XSS详解
波士顿房价数据集 Boston house prices dataset
《c语言》青蛙跳台阶递归问题
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
如何升级nodejs版本
重装系统后,hosts文件配置后不生效
uniapp 高度不自适应
MySQL detailed explanation
ZCMU--1862: zbj的狼人杀
Failure scenarios of @Transactional annotations
开源|商品识别推荐系统
循环结构--for循环
UML图及在drawio中的绘制
【C#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)
MySql database optimization query tool