前言

每个项目都需要构建镜像, 但是假如说每个镜像配合一个Jenkins任务,那未免有点不太合适.

所以重新写了一个接入指南.

项目容器构建接入指南

要求

  1. Dockerfile 必须在git仓库的根目录
  2. 必须添加名称为BuildEnv.groovy的配置文件
  3. 需要将xxx构建用户加入项目,并赋予可读权限

公司正在使用的仓库,以及对应的credentialsId

  1. docker-registry.xxxx.com: 71983ff4-4a95-4b27-a7a8-f09f62dd7577
  2. docker-registry.aaaa.com: 71983ff4-4a95-4b27-a7a8-f09f62dd7577

Webhook地址

地址: xxx.aaa.com

可接受事件类型:

  • Tag push events

接入流程

  1. 在此页面获取自己上传仓库的 credentialsId
  2. 赋予xxxgit用户可读权限
  3. 创建配置文件, 修改响应的配置
  4. 在gitlab中配置,webhook地址选择对应可接入类型.

BuildEnv.groovy 配置文件格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 在Jenkins 注册的仓库凭证ID
env.credentialsId="71983ff4-4a95-4b27-a7a8-f09f62dd7577"

// 镜像地址
env.registry="docker-registry.xxxx.com/xxxx/"

// 容器名称
env.imageName="xxxx"


// 构建结束后通知人员, 已;分割
env.notifyUsers="xxxx@xxxx.com;xxxx@qq.com"


// 最终容器地址为: env.registry + env.imageName + : + tag
// 比如说: env.registry="docker-registry.xxxx.com/xxxx/" env.imageName="xxxx" tag="v0.0.1" 最终地址为: "docker-registry.xxxx.com/xxxx/xxxx:v0.0.1"

// 这里一般不改
env.httpRegistry='https://' + env.registry

实现方式

我们可以把Jenkins任务当做函数, 把执行当做函数调用, 构建的步骤是一样的,那我们只需要进行传参不就可以了吗?

使用 Generic Webhook Trigger 插件捕获参数

我们在Post content parameters 下来捕获 git 发送过来的值. 可以翻往期的博客,有介绍.

使用 JSONPath 来获取

  • Variable: full_tag
  • Expression: $.ref

  • Variable: user_name
  • Expression: $.user_name

  • Variable: repository_name
  • Expression: $.repository.name

  • Variable: git_http_url
  • Expression: $.repository.git_http_url

  • Variable: tag
  • Expression: $.ref
  • Value filter: refs/tags/

  • Variable: repository_git_http_url
  • Expression: $.repository.git_http_url
  • Value filter: \.git

  • Variable: checkout_sha
  • Expression: $.checkout_sha

一共需要这么多参数, 如果需要其他参数, 直接添加就行.

其中: Token 我们需要自己定义.

Pipeline script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pipeline {
agent any

stages {
stage('Before Build') {
steps {
echo "TAG: ${tag} user_name: ${user_name} repository_name: ${repository_name} git_http_url: ${git_http_url}"
}
}
stage('Checkout Code') {
steps {
checkout scm: [$class: 'GitSCM',
userRemoteConfigs: [[credentialsId: '1', url: '${git_http_url}']],
branches: [[name: '${full_tag}']]]
}
}
stage('Build Container') {
steps {
script {
load "./BuildEnv.groovy"
if (!(env.credentialsId && env.registry && env.imageName)) {
error "You need to set env by BuildEnv.groovy."
}
docker.withRegistry(env.httpRegistry, env.credentialsId) {
def image = docker.build("${env.registry}${env.imageName}:${tag}")
image.push()
sh label: '', script: "docker rmi ${image.id}"
}
}
}
}
}

post {
always {
echo 'finished'
deleteDir() /* clean up our workspace */
}

success {
emailext body: '<style class="fox_global_style">div.fox_html_content { line-height: 1.5; }ol, ul { margin-top: 0px; margin-bottom: 0px; list-style-position: inside; }div.fox_html_content { font-size: 14px; font-family: \'Microsoft YaHei UI\'; color: rgb(0, 0, 0); line-height: 1.5; }</style><div><div style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: normal;"><br></div><div style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: normal;"><br></div><ul><li><span style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: 1.5; font-size: 10.5pt; background-color: transparent;">仓库:</span><font face="Microsoft YaHei UI, Tahoma">${repository_name}</font></li><li style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: normal;"><span style="font-size: 10.5pt; line-height: 1.5; background-color: transparent;">构建状态:成功</span></li><li><span style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: 1.5; font-size: 10.5pt; background-color: transparent;">代码版本:</span><font face="Microsoft YaHei UI, Tahoma">${tag}</font></li><li><font face="Microsoft YaHei UI, Tahoma"><span style="line-height: normal;">提交用户:</span></font><font face="monospace, monospace"><span style="line-height: normal;">${user_name}</span></font></li><li><font face="Microsoft YaHei UI, Tahoma"><span style="line-height: normal;">仓库地址: <a href="${repository_git_http_url}/commit/${checkout_sha}">${repository_git_http_url}</a></span></font></li></ul></div>', subject: '【构建通知】仓库:${repository_name} TAG: ${tag}', to: "${env.notifyUsers}"
}

failure {
emailext attachLog: true, body: '<style class="fox_global_style">div.fox_html_content { line-height: 1.5; }ol, ul { margin-top: 0px; margin-bottom: 0px; list-style-position: inside; }div.fox_html_content { font-size: 14px; font-family: \'Microsoft YaHei UI\'; color: rgb(0, 0, 0); line-height: 1.5; }</style><div><div style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: normal;"><br></div><div style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: normal;"><br></div><ul><li><span style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: 1.5; font-size: 10.5pt; background-color: transparent;">仓库:</span><font face="Microsoft YaHei UI, Tahoma">${repository_name}</font></li><li style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: normal;"><span style="font-size: 10.5pt; line-height: 1.5; background-color: transparent;">构建状态:失败</span></li><li><span style="font-family: \'Microsoft YaHei UI\', Tahoma; line-height: 1.5; font-size: 10.5pt; background-color: transparent;">代码版本:</span><font face="Microsoft YaHei UI, Tahoma">${tag}</font></li><li><font face="Microsoft YaHei UI, Tahoma"><span style="line-height: normal;">提交用户:</span></font><font face="monospace, monospace"><span style="line-height: normal;">${user_name}</span></font></li></ul></div>', subject: '【构建通知】仓库:${repository_name} TAG: ${tag}', to: "${env.notifyUsers}"
}
}
}