目录
6、流水线中使用凭证 -- 安装Credential Binding 插件
7、控制脚本安全性--Script Security插件提供
1、通知
1.1 电子邮件
A、jenkins全局配置,某些选项要点高级(在Configure System)
B、流水线中发送电子邮件
脚本式流水线
node ('worker_node1') {
try {
...
currentBuild.result = 'SUCCESS'
}
catch (err) {
currentBuild.result = 'FAILURE'
}
finally {
mail to: 'bcl@nclasters.org',
subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
body: "${env.BUILD_URL} has result ${currentBuild.result}"
}
}
声明式流水线
pipeline {
agent any
stages {
...
}
post {
always {
mail to: 'bcl@nclasters.org',
subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
body: "${env.BUILD_URL} has result ${currentBuild.result}"
}
}
}
C、扩张电子邮件通知---越来越多的团队使用及时消息服务进行通知Slack 和HipChat。
1.2 协作服务
需要安装Slack 和 HipChat Notification 插件,然后配置信息。需要对应账号。
与钉钉集成
Jenkins 集成 DingTalk 插件实现项目构建状态钉钉消息推送
钉钉机器人插件(在jenkins中使用方法,声明式流水线使用方法都有)
2、报告
发布HTML报告(安装HTML Publisher plugin),下面是脚本式实例
finally {
unstash 'api-reports'
publishHTML (target: [
allowMissing: false, // 报告确实使构建失败
alwaysLinkToLastBuild: false, // true,构建失败会链接到上一次成功的构建报告
keepAll: true, // true 会将所有成功构建的报告归档
reportDir: 'api/build/reports/test', // 相对于工作空间的路径
reportFiles: 'index.html', // HTML文件的名称,多个逗号分隔
reportName: "API Unit Testing Results" // 链接的报告名称
])
unstash 'util-reports' // 释放内容
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'util/build/reports/test',
reportFiles: 'index.html',
reportName: "Util Unit Testing Results"
])
}
3、安全加固jenkins
3.1 启用安全性
A、禁用记住我
B、安全域
C、授权 Authorization
legacy mode (传统模式):任何具有管理员角色的人都具有完全的控制权,其他人只能读。
3.2 其他全局安全性配置
Markup Formatter:标记格式化程序,允许用户在各种文本域中放置自由形式的文本,例如任务描述、构建描述等。
插件管理:Authorize Project插件为每一个项目添加了一个新的授权页。可以控制那些用户可以运行这个任务
4、jenkins中的凭证
凭证一般是指对寿县资源的访问权限的一个或者多个值。
6、流水线中使用凭证 -- 安装Credential Binding 插件
使用用户名和密码
withCredentials([usernameColonPassword(credentialsId: 'xx', variable: 'user')]) {
// some block
}
使用SSH密钥
withCredentials([sshUserPrivateKey(credentialsId: '2c514b23-6377-4e7a-ba65-90b83e5eedc1', keyFileVariable: 'test', passphraseVariable: '', usernameVariable: '')]) {
// some block
}
下面是一个在流水线脚本中使用的实例:
node {
def sshRepodef = 'git@diyvb:repos/shared-libraries.git'
stage('Get Source') {
git url: sshRepodef
}
stage('Update Source') {
sh "git config user.name diyuser"
sh "git config user.email diyuser@localhost"
sshagent(['03463f1a-bddd-48cc-b1e0-575bfa9b721f']) {
sh "git tag -a ${env.BUILDTAG} -m 'Demonstrate push of tags'"
sh "git push ${sshRepodef} --tags"
}
}
}
令牌凭证:
node {
withCredentials([string(credentialsId: '<token>', variable: 'TOKEN')])
{
sh '''
set +x
curl -H "Token: $TOKEN" https://some.api/
'''
}
}
set +x 防止脚本执行的时候打印凭证;
7、控制脚本安全性--Script Security插件提供
8、Groovy沙箱
jenkins2还支持Groovy Sanbox 复选框来实现的。在流水线项目中勾选即可。在正在运行的脚本批准页面由管理来批准是否运行。