momo's Blog.

Jenkins一些插件使用心得以及一些坑

字数统计: 979阅读时长: 5 min
2020/03/16 Share

1、前言

记录使用插件的心得,以及踩过的坑。

2、使用方法

2.1 Jenkins获取git分支以及tag

Git Parameter 插件地址
使用 Git Parameter

使用此插件

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
Pipeline: Branch type - Basic usage (Declarative Pipeline)
// Using git without checkout
pipeline {
agent any
parameters {
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH'
}
stages {
stage('Example') {
steps {
git branch: "${params.BRANCH}", url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
}
}
}
}
Pipeline: Branch type - Basic usage (Scripted Pipeline)
properties([
parameters([
gitParameter(branch: '',
branchFilter: 'origin/(.*)',
defaultValue: 'master',
description: '',
name: 'BRANCH',
quickFilterEnabled: false,
selectedValue: 'NONE',
sortMode: 'NONE',
tagFilter: '*',
type: 'PT_BRANCH')
])
])
node {
git branch: "${params.BRANCH}", url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
}

2.2 使用Jenkins发送邮件

Jenkins默认是支持发送邮件,不过发送邮件的内容有局限性。经过测试,原版邮件不支持已变量的形式发送,因此使用邮件插件 Email Extension

使用此插件需要在Jenkins系统配置中进行邮件服务器的配置,配置完成后,使用方式如下:

1
2
3
4
5
6
emailext (
subject: "【xxx】 FAILED:: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
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: 10.5pt; font-family: 'Microsoft YaHei UI'; color: rgb(0, 0, 0); line-height: 1.5; }div.fox_html_content { font-size: 10.5pt; font-family: 'Microsoft YaHei UI'; color: rgb(0, 0, 0); line-height: 1.5; }</style><div><span style="font-family: 微软雅黑; font-size: 15px; line-height: 1.5; background-color: rgb(253, 253, 253);">FAILED</span><font face="微软雅黑" style="font-size: 15px;"><span style="font-variant-ligatures: normal; orphans: 2; white-space: pre; widows: 2; background-color: rgb(253, 253, 253);">: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'</span></font></div><div style="orphans: 2; widows: 2;"><font face="微软雅黑"><span style="font-size: 15px; line-height: 22.5px; white-space: pre; background-color: rgb(253, 253, 253);">本邮件为系统自动发送,请勿回复</span></font></div><div style="orphans: 2; widows: 2;"><ul><li><font face="微软雅黑" style="font-size: 10.5pt; line-height: 1.5; background-color: transparent;"><span style="font-size: 15px; line-height: 22.5px; white-space: pre; background-color: rgb(253, 253, 253);">状态</span></font><span style="font-size: 10.5pt; line-height: 1.5; background-color: transparent;">状态:${env.JOB_NAME} 任务构建失败请联系运维同学查看</span></li><li>启动用户: &nbsp;${env.USER_T}</li><li><span style="font-size: 10.5pt; line-height: 1.5; background-color: transparent;">URL:&nbsp;</span><span style="font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; font-variant-ligatures: normal; white-space: pre; background-color: rgb(253, 253, 253);">${env.BUILD_URL}</span></li><li><span style="font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; font-variant-ligatures: normal; white-space: pre; background-color: rgb(253, 253, 253);"><span andale="" mono',="" 'ubuntu="" monospace;="" font-size:="" 14px;="" color:="" rgb(0,="" 0,="" 0);="" background-color:="" rgb(253,="" 253,="" 253);="" font-weight:="" normal;="" font-style:="" normal;text-decoration:="" none;'="">NUMBER: ${env.BUILD_NUMBER}</span></span></li></ul></div>""",
to: 'guozhipeng',
from: "sa@mu77.com"
)

邮件内容支持以html的方式发送,加上一些Jenkins自带的变量,可以很容易组合出内容丰富的邮件。

2.3 在邮件中获取job启动用户

Jenkins默认是无法获取到是谁启动了任务的,因此需要使用一些插件
user build vars

Property Default
BUILD_USER Full name (first name + last name)
BUILD_USER_FIRST_NAME First name
BUILD_USER_LAST_NAME Last name
BUILD_USER_ID Jenkins user ID
BUILD_USER_EMAIL Email address

使用方法:

可在流水线 script 字段下定义全局变量,随后在post 模块下使用

1
2
3
4
wrap([$class: 'BuildUser']){
env.USER_T = "$BUILD_USER"
env.USER_EMAIL_T = "$BUILD_USER_EMAIL"
}

2.4 安装了Blue Ocean 以后,想让用户点击链接直接进入到Blue界面,而不是经典界面?

需要使用Display URL API 插件

通过调用此插件内置的变量,可以将通知连接替换为Blue页面

内置的变量有

  • RUN_DISPLAY_URL – links to the run result
  • RUN_CHANGES_DISPLAY_URL – links to the changes page for a run
  • JOB_DISPLAY_URL – links to the jobs homepage

2.5 Jenkins 权限控制

Role-based Authorization Strategy 插件

CATALOG
  1. 1. 1、前言
  2. 2. 2、使用方法
    1. 2.1. 2.1 Jenkins获取git分支以及tag
    2. 2.2. 2.2 使用Jenkins发送邮件
    3. 2.3. 2.3 在邮件中获取job启动用户
    4. 2.4. 2.4 安装了Blue Ocean 以后,想让用户点击链接直接进入到Blue界面,而不是经典界面?
    5. 2.5. 2.5 Jenkins 权限控制