Jenkinsfile 共享库
官方文档: https://www.jenkins.io/zh/doc/book/pipeline/shared-libraries/
共享库主要用来抽离公共函数
共享库解决的问题
jenkinsfile 通常是一些脚本,没有共享库,重复脚本会非常多
比如,重复的获取配置代码,到后期维护不易
共享库制作
- 新建一个 git 库
- Jenkins 中, Manage Jenkins » Configure System » Global Trusted Pipeline Libraries 里填写注册该 git 库
- jenkinsfile 中声明库,然后就可以调用库中的函数
共享 git 库文件目录规范
(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
- resources 目录下放配置、资源
- vars 目录下,定义静态函数
- src 目录下,定义动态 class 类(未实验成功)
- vars 下定义静态函数,基本已经够用了
共享库中配置的例子
比如, resources 目录下有配置 dev_game_list.txt
:
内网服,dev,192.168.100.44,/data/Game,9000,true
QA 服,dev,192.168.100.47,/data/Game,8999,true
数值服,dev,192.168.100.50,/data/Game,8998,true
审核服,release,60.x.x.x,/data/Game/game1,9000,true
外网测试服,release,60.x.x.x,/data/Game/gameTest,8999,true
可以在 vars 下编写 groovy 文件,比如 conf.groovy
:
void loadEnv_Game() {
jobName = env.JOB_NAME
prefix = jobName.split('_')[0]
loadEnv_GameByPrefix(params.server, prefix, '')
}
void loadEnv_GameByPrefix(String server, String prefix, String varPrefix) {
env."${varPrefix}prefix" = prefix
env."${varPrefix}server" = server
gameList = libraryResource("${prefix}_game_list.txt")
lines = gameList.split('\n')
result = lines.find { String line -> line.contains(env."${varPrefix}server") }
env."${varPrefix}publish_mode" = result.split(',')[1]
env."${varPrefix}publish_addr" = result.split(',')[2]
env."${varPrefix}publish_dir" = result.split(',')[3]
env."${varPrefix}publish_serverid" = result.split(',')[4]
env."${varPrefix}is_docker" = result.split(',')[5]
if (prefix != 'release') {
env."${varPrefix}ssh_credential" = 'dev'
} else {
env."${varPrefix}ssh_credential" = 'release'
}
if ("${env."${varPrefix}is_docker"}" == 'true') {
env."${varPrefix}publish_dir" = "${env."${varPrefix}publish_dir"}Docker"
}
echo "${varPrefix}is_docker: ${env."${varPrefix}is_docker"}"
echo "${varPrefix}prefix: ${prefix}"
echo "${varPrefix}server: ${env."${varPrefix}server"}"
echo "${varPrefix}publish_mode: ${env."${varPrefix}publish_mode"}"
echo "${varPrefix}publish_addr: ${env."${varPrefix}publish_addr"}"
echo "${varPrefix}publish_dir: ${env."${varPrefix}publish_dir"}"
echo "${varPrefix}publish_serverid: ${env."${varPrefix}publish_serverid"}"
}
每个 jenkinsfile 中,只要调用 conf.loadEnv_Game()
,自动加载对应服的配置
Jenkinsfile 中调用说明
大概用例如下:
library 'my-shared-library'
publish_game_list = conf.getPublishGameList()
version = conf.getVersionList('host40').call()
pipeline {
agent {
node {
label 'host40'
}
}
parameters {
choice(name: 'server', choices: publish_game_list, description: '选服')
choice(name: 'version', choices: version, description: '选版本')
}
stages {
stage('test_init') {
steps {
script {
conf.loadEnv_Game()
}
}
}
}
}
- 加载共享库,比如开头填写 :
library 'my-shared-library'
- 可以预先生成些数据,比如:
- conf.getPublishGameList() , 服列表
- conf.getVersionList(‘host40’).call() , 版本列表
- 调用 conf.loadEnv_Game() ,根据服名称,加载对应服的配置信息,并设置到环境变量里
类似的,比如获取某个服的 DB 配置等等,也可以在共享库内实现一次, jenkinsfile 中直接从环境变量中获取
以上