1.获取用户名字
需要两个插件:
build-user-vars-plugin 下载地址
Groovy Postbuild
官方语法:
http://jenkinsServer/jenkins/pipeline-syntax/globals#en
https://plugins.jenkins.io/build-user-vars-plugin/
pipeline中的全局变量,默认不支持获取当前构建任务的构建人,要想获取构建人的信息
echo "${currentBuild.displayName},${currentBuild.result}"
#Pipeline Examples
node {
wrap([$class: 'BuildUser']) {
def user = env.BUILD_USER_ID
}
}
wrap([$class: 'BuildUser']) {
sh 'echo "${BUILD_USER}"'
sh "echo ${JOB_NAME} ${BUILD_NUMBER} ${GIT_BRANCH} ${BUILD_USER}"
}
#Pipeline Examples
node {
wrap([$class: 'BuildUser']) {
def user = env.BUILD_USER_ID
}
}
wrap([$class: 'BuildUser']) {
sh 'echo "${BUILD_USER}"'
sh "echo ${JOB_NAME} ${BUILD_NUMBER} ${GIT_BRANCH} ${BUILD_USER}"
}
2.设置构建名称
实现方式:
wrap([$class: 'BuildUser']) {
// some block
def user = env.BUILD_USER
//manager.addShortText("启动人:${manager.envVars['BUILD_USER']}")
manager.addShortText("启动人:${user}")
//buildName "#${BUILD_NUMBER}-^${BRANCH}^-${BUILD_USER}"
buildName "#${BUILD_NUMBER}-^${update}"
//输出最新一次提交信息
HTTPD_LOCATION= sh( returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')
//修改Description"
buildDescription "${HTTPD_LOCATION}"
// buildDescription '<span style="padding-left: 160px;color: #0587d4;"> demo:${buildVersion} ${environment} ${branch} </span>'
}
wrap([$class: 'BuildUser']) {
// some block
def user = env.BUILD_USER
//manager.addShortText("启动人:${manager.envVars['BUILD_USER']}")
manager.addShortText("启动人:${user}")
//buildName "#${BUILD_NUMBER}-^${BRANCH}^-${BUILD_USER}"
buildName "#${BUILD_NUMBER}-^${update}"
//输出最新一次提交信息
HTTPD_LOCATION= sh( returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')
//修改Description"
buildDescription "${HTTPD_LOCATION}"
// buildDescription '<span style="padding-left: 160px;color: #0587d4;"> demo:${buildVersion} ${environment} ${branch} </span>'
}
pipeline {
agent any
environment {
HOST_TEST = '172.16.195.190'
HOST_ONLINE = 'jenkins@39.101.219.110'
SOURCE_DIR = "$WORKSPACE/src/EduPayCenter/PayCenterApi/bin/Release/net5.0/linux-x64/publish/*"
TARGET_DIR = '/data/ufun/pay/'
BUILD_DIR = "$WORKSPACE/src/EduPayCenter/PayCenterApi"
SERVICE_DIT = "/data/ufun/start_scripts/pay.sh"
LOG_DIR = "/data/ufun/start_scripts/logs/pay.log"
}
options{
timestamps()
}
parameters {
choice(
description: '你需要选择哪个环境进行部署 ?',
name: 'env',
choices: ['测试环境']
)
string(name: 'update', defaultValue: '', description: '本次更新内容?')
}
stages{
stage('检查服务') {
steps {
//
script {
wrap([$class: 'BuildUser']) {
// some block
def user = env.BUILD_USER
//manager.addShortText("启动人:${manager.envVars['BUILD_USER']}")
manager.addShortText("启动人:${user}")
//buildName "#${BUILD_NUMBER}-^${BRANCH}^-${BUILD_USER}"
buildName "#${BUILD_NUMBER}-^${update}"
//输出最新一次提交信息
//HTTPD_LOCATION= sh( returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')
//修改Description"
//buildDescription "${HTTPD_LOCATION}"
}
//def healthUrl = null
//if ('production' == "${profile}") {
healthUrl = "ssh ${HOST_TEST} 'ps -ef|grep PayCenterApi|grep -v grep'"
//}
echo "睡眠两分钟,待应用完全准备好"
Thread.sleep((long) 1000 * 60 * 1)//睡眠1分钟
String shellStr = sh(script: "${healthUrl}", returnStdout: true)
echo "应用健康检查结果:${shellStr}"
if (shellStr.indexOf("/data/ufun/pay/PayCenterApi") > 0) {
echo "应用健康运行"
} else {
Thread.sleep((long) 1000 * 60 * 1)//睡眠0.5分钟
shellStr = sh(script: "${healthUrl}", returnStdout: true)
if (shellStr.indexOf("/data/ufun/pay/PayCenterApi") > 0) {
echo "应用健康运行"
} else {
throw new RuntimeException("应用不稳定,请检查服务是否正常")
}
}
}
//
}
}
}
}
pipeline {
agent any
environment {
HOST_TEST = '172.16.195.190'
HOST_ONLINE = 'jenkins@39.101.219.110'
SOURCE_DIR = "$WORKSPACE/src/EduPayCenter/PayCenterApi/bin/Release/net5.0/linux-x64/publish/*"
TARGET_DIR = '/data/ufun/pay/'
BUILD_DIR = "$WORKSPACE/src/EduPayCenter/PayCenterApi"
SERVICE_DIT = "/data/ufun/start_scripts/pay.sh"
LOG_DIR = "/data/ufun/start_scripts/logs/pay.log"
}
options{
timestamps()
}
parameters {
choice(
description: '你需要选择哪个环境进行部署 ?',
name: 'env',
choices: ['测试环境']
)
string(name: 'update', defaultValue: '', description: '本次更新内容?')
}
stages{
stage('检查服务') {
steps {
//
script {
wrap([$class: 'BuildUser']) {
// some block
def user = env.BUILD_USER
//manager.addShortText("启动人:${manager.envVars['BUILD_USER']}")
manager.addShortText("启动人:${user}")
//buildName "#${BUILD_NUMBER}-^${BRANCH}^-${BUILD_USER}"
buildName "#${BUILD_NUMBER}-^${update}"
//输出最新一次提交信息
//HTTPD_LOCATION= sh( returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')
//修改Description"
//buildDescription "${HTTPD_LOCATION}"
}
//def healthUrl = null
//if ('production' == "${profile}") {
healthUrl = "ssh ${HOST_TEST} 'ps -ef|grep PayCenterApi|grep -v grep'"
//}
echo "睡眠两分钟,待应用完全准备好"
Thread.sleep((long) 1000 * 60 * 1)//睡眠1分钟
String shellStr = sh(script: "${healthUrl}", returnStdout: true)
echo "应用健康检查结果:${shellStr}"
if (shellStr.indexOf("/data/ufun/pay/PayCenterApi") > 0) {
echo "应用健康运行"
} else {
Thread.sleep((long) 1000 * 60 * 1)//睡眠0.5分钟
shellStr = sh(script: "${healthUrl}", returnStdout: true)
if (shellStr.indexOf("/data/ufun/pay/PayCenterApi") > 0) {
echo "应用健康运行"
} else {
throw new RuntimeException("应用不稳定,请检查服务是否正常")
}
}
}
//
}
}
}
}
2.构建历史中显示更多信息(方式一)
安装插件 groovy-postbuild
构建后操作步骤 – > Groovy Postbuild --> 填写如下内容 --> 应用 && 保存
manager.addShortText("部署分支:${manager.envVars['GIT_BRANCH']}")
//common上面参数化 构建的传参取值
manager.addShortText(manager.build.buildVariables.get("common"))
//获取代码提交人的信息并显示在历史记录上
String command='cd /root/.jenkins/workspace/OperateFe-Test && git show -s --pretty=%an'
def commit= [ 'sh', '-c', command ].execute().text
manager.addShortText("代码提交人:${commit}")
2.构建历史中显示更多信息(方式一)
安装插件 groovy-postbuild
构建后操作步骤 – > Groovy Postbuild --> 填写如下内容 --> 应用 && 保存
manager.addShortText("部署分支:${manager.envVars['GIT_BRANCH']}")
//common上面参数化 构建的传参取值
manager.addShortText(manager.build.buildVariables.get("common"))
//获取代码提交人的信息并显示在历史记录上
String command='cd /root/.jenkins/workspace/OperateFe-Test && git show -s --pretty=%an'
def commit= [ 'sh', '-c', command ].execute().text
manager.addShortText("代码提交人:${commit}")