这里记录一些当前使用的pipeline模板和邮件模板
Java项目模板
// java项目
// 需要解析http返回结果时使用
import groovy.json.JsonSlurperClassic
import groovy.json.JsonOutput
// 按需求更改变量的值
node {
// 参数设置
def repoUrl = 'git@*****.git'
def repoBranch = 'dev'
def gitDir = ''
def workspace = ''
// docker镜像信息
def imageName = "*****/demo"
def imageTag = "dev"
// rancher1.X部署所需变量
def rancher1Url = 'https:///v2-beta' // rancher1.X地址
def rancher1Env = '' // rancher1.X需部署服务所在环境的ID
def rancher1Service = '/' // rancher1.X需部署服务的 栈/服务名
// rancher2.X部署所需变量
def rancher2Url = "https:///v3/project/:" // rancher2.X地址+project
def rancher2Namespace = "" // rancher2.X需部署服务的命名空间
def rancher2Service = "bookkeeper" // rancher2.X需部署服务的服务名
def recipients = '' // 收件人
def jenkinsHost = '' // jenkins服务器地址
// 认证ID
def gitCredentialsId = 'aa651463-c335-4488-8ff0-b82b87e11c59'
def settingsConfigId = '3ae4512e-8380-4044-9039-2b60631210fe'
def rancherAPIKey = 'd41150be-4032-4a53-be12-3024c6eb4204'
def soundsId = '69c344f1-8b11-47a1-a3b6-dfa423b94d78'
// 工具配置
def mvnHome = 'maven3.5.2'
env.SONAR_HOME = "${tool 'sonarscanner'}"
env.PATH="${env.SONAR_HOME}/bin:${env.PATH}"
try {
// 正常构建流程
stage("Preparation"){
// 拉取需要构建的代码
git_maps = checkout scm: [$class: 'GitSCM', branches: [[name: "*/${repoBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${gitDir}"]], userRemoteConfigs: [[credentialsId: "${gitCredentialsId}", url: "${repoUrl}"]]]
env.GIT_REVISION = git_maps.GIT_COMMIT[0..8]
}
try {
stage('BackEndBuild') {
// maven构建生成二进制包
dir("${workspace}"){
withMaven(
maven: "${mvnHome}",
mavenSettingsConfig: "${settingsConfigId}",
options: [artifactsPublisher(disabled: true)]) {
sh "mvn -U clean package -Dmaven.test.skip=true"
}
}
}
}finally {
stage('ArchiveJar') {
// 获取二进制包产物
archiveArtifacts allowEmptyArchive: true, artifacts: "${workspace}target/surefire-reports/TEST-*.xml"
archiveArtifacts allowEmptyArchive: true, artifacts: "${workspace}target/*.jar"
}
}
stage('CodeCheck'){
// sonar_scanner进行代码静态检查
withSonarQubeEnv('sonar') {
sh """
sonar-scanner -X -Dsonar.language=java \
-Dsonar.projectKey=$JOB_NAME \
-Dsonar.projectName=$JOB_NAME \
-Dsonar.projectVersion=$GIT_REVISION \
-Dsonar.sources=src/ \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.binaries=target/ \
-Dsonar.exclusions=src/test/**
"""
}
}
stage("QualityGate") {
// 获取sonar检查结果
timeout(time: 1, unit: "HOURS") {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage('DockerBuild'){
// docker生成镜像并push到远程仓库中
sh """
rm -f ${workspace}src/docker/*.jar
cp ${workspace}target/*.jar ${workspace}src/docker/
"""
dir("${workspace}src/docker/"){
def image = docker.build("${imageName}:${imageTag}")
image.push()
sh "docker rmi ${imageName}:${imageTag}"
}
}
stage('Rancher1Deploy'){
// Rancher1.X上进行服务的更新部署
rancher confirm: false, credentialId: "${rancherAPIKey}", endpoint: "${rancher1Url}", environmentId: "${rancher1Env}", environments: '', image: "${imageName}:${imageTag}", ports: '', service: "${rancher1Service}"
}
stage('Rancher2Deploy') {
// Rancher2.X上更新容器
// 获取服务信息
def response = httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'GET', responseHandle: 'LEAVE_OPEN', timeout: 10, url: "${rancher2Url}/workloads/deployment:${rancher2Namespace}:${rancher2Service}"
def serviceInfo = new JsonSlurperClassic().parseText(response.content)
response.close()
def dockerImage = imageName+":"+imageTag
if (dockerImage.equals(serviceInfo.containers[0].image)) {
// 如果镜像名未改变,直接删除原容器
// 查询容器名称
response = httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'GET', responseHandle: 'LEAVE_OPEN', timeout: 10, url: "${rancher2Url}/pods/?workloadId=deployment:${rancher2Namespace}:${rancher2Service}"
def podsInfo = new JsonSlurperClassic().parseText(response.content)
def containerName = podsInfo.data[0].name
response.close()
// 删除容器
httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'DELETE', responseHandle: 'NONE', timeout: 10, url: "${rancher2Url}/pods/${rancher2Namespace}:${containerName}"
} else {
// 如果镜像名改变,使用新镜像名更新服务
serviceInfo.containers[0].image = dockerImage
def updateJson = new JsonOutput().toJson(serviceInfo)
httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'PUT', requestBody: "${updateJson}", responseHandle: 'NONE', timeout: 10, url: "${rancher2Url}/workloads/deployment:${rancher2Namespace}:${rancher2Service}"
}
}
// 构建成功:声音提示,邮件发送
httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/4579.wav'
emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
} catch(err) {
// 构建失败:声音提示,邮件发送
currentBuild.result = 'FAILURE'
httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/8923.wav'
emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
}
}
php+js的项目模板
与java不同的是,我们php+js的服务使用了3个容器,php+nginx+code的方式进行部署,每次只需要更新服务中的code容器即可;另外,为了防止php和js的构建污染jenkins服务,我们利用了官方的node镜像和一个自定义已安装好php构建环境的composer镜像,并使用docker.image('').inside{}代码块将构建过程放置到了docker容器当中。
// php+js项目
import groovy.json.JsonSlurperClassic
import groovy.json.JsonOutput
node {
def repoUrl = 'git@******.git'
def repoBranch = 'develop'
def imageName = '***/code'
def imageTag = 'dev'
def buildEnv = 'develop'
// rancher2.X部署所需变量
def rancherUrl = 'https:///v3/project/:'
def rancherNamespace = ''
def rancherService = ''
def recipients = ''
def jenkinsHost = '' // jenkins服务器地址
// 认证ID
def gitCredentialsId = 'aa651463-c335-4488-8ff0-b82b87e11c59'
def settingsConfigId = '3ae4512e-8380-4044-9039-2b60631210fe'
def rancherAPIKey = 'd41150be-4032-4a53-be12-3024c6eb4204'
def soundsId = '69c344f1-8b11-47a1-a3b6-dfa423b94d78'
env.SONAR_HOME = "${tool 'sonarscanner'}"
env.PATH="${env.SONAR_HOME}/bin:${env.PATH}"
try {
stage("prepare") {
// 拉取需要构建的代码
git_maps = checkout scm: [$class: 'GitSCM', branches: [[name: "*/${repoBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], userRemoteConfigs: [[credentialsId: "${gitCredentialsId}", url: "${repoUrl}"]]]
env.GIT_REVISION = git_maps.GIT_COMMIT[0..8]
}
stage('CodeCheck'){
// sonar_scanner进行代码静态检查
withSonarQubeEnv('sonar') {
sh """
sonar-scanner -X \
-Dsonar.projectKey=$JOB_NAME \
-Dsonar.projectName=$JOB_NAME \
-Dsonar.projectVersion=$GIT_REVISION \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.modules=php-module,javascript-module \
-Dphp-module.sonar.projectName=PHP-Module \
-Dphp-module.sonar.language=php \
-Dphp-module.sonar.sources=. \
-Dphp-module.sonar.projectBaseDir=backend/app \
-Djavascript-module.sonar.projectName=JavaScript-Module \
-Djavascript-module.sonar.language=js \
-Djavascript-module.sonar.sources=. \
-Djavascript-module.sonar.projectBaseDir=front/src
"""
}
}
stage("QualityGate") {
// 获取sonar检查结果
timeout(time: 1, unit: "HOURS") {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage("frontBuild") {
// 前端构建
docker.image("node").inside() {
sh """
cd front
npm install
npm run build ${buildEnv}
"""
}
}
stage("back-build") {
// 后端构建
docker.image("/composer:v0").inside() {
sh """
cd backend
composer install
"""
}
}
stage("docker-code-build") {
// 代码镜像构建
sh "cp -r front/build docker/code/build"
sh "cp -r backend docker/code/api"
dir("docker/code") {
docker.build("${imageName}:${imageTag}").push()
sh "docker rmi ${imageName}:${imageTag}"
}
}
stage('rancherDeploy') {
// Rancher上更新容器
// 查询容器名称
def response = httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'GET', responseHandle: 'LEAVE_OPEN', timeout: 10, url: "${rancherUrl}/pods/?workloadId=deployment:${rancherNamespace}:${rancherService}"
def podsInfo = new JsonSlurperClassic().parseText(response.content)
def containerName = podsInfo.data[0].name
print(containerName)
response.close()
// 删除容器
httpRequest acceptType: 'APPLICATION_JSON', authentication: "${rancherAPIKey}", contentType: 'APPLICATION_JSON', httpMode: 'DELETE', responseHandle: 'NONE', timeout: 10, url: "${rancherUrl}/pods/${rancherNamespace}:${containerName}"
}
stage("tear-down") {
sh "rm -rf docker"
}
httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/4579.wav'
emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
} catch(err) {
currentBuild.result = 'FAILURE'
httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/8923.wav'
emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
}
}
Android项目模板
这里android项目只是进行了单元测试和构建过程,保证本次构建能够成功进行
// android项目
// 按需求更改变量的值
node {
// 参数设置
def repoUrl = 'git@*****.git'
def repoBranch = 'develop'
def gitDir = ''
def workspace = 'Demo/'
def version = ''
def recipients = ''
def jenkinsHost = '' // jenkins服务器地址
// 认证ID
def gitCredentialsId = 'aa651463-c335-4488-8ff0-b82b87e11c59'
def soundsId = '69c344f1-8b11-47a1-a3b6-dfa423b94d78'
// 工具配置
def gradleTool = './gradlew'
// env.GRADLE_HOME = "${tool 'Gradle3.3'}"
// env.PATH="${env.GRADLE_HOME}/bin:${env.PATH}"
try {
// 正常构建流程
stage("Preparation"){
// 拉取需要构建的代码
checkout scm: [$class: 'GitSCM', branches: [[name: "*/${repoBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${gitDir}"]], userRemoteConfigs: [[credentialsId: "${gitCredentialsId}", url: "${repoUrl}"]]]
}
try {
stage('UnitTest') {
// 在清空前一次构建的结果后进行单元测试,并获取测试报告
dir("${workspace}"){
sh "${gradleTool} clean"
sh "${gradleTool} testReleaseUnitTest --stacktrace"
}
}
stage('Build') {
// 使用Gradlew进行sdk的构建
dir("${workspace}"){
sh "${gradleTool} makeJar"
}
}
}
finally {
stage('Results'){
// 获取单元测试报告
sh """
cd ${workspace}librasdk/build/reports/tests/
tar -zcvf test_reports.tar.gz ./test*/
"""
archiveArtifacts allowEmptyArchive: true, artifacts: "${workspace}librasdk/build/reports/tests/test_reports.tar.gz"
// 获取sdk构建生成的jar包
archive "${workspace}librasdk/build/libs/*/*.jar"
}
}
httpRequest authentication:"${soundsId}", url:'http://10.38.162.13:8081/sounds/playSound?src=file:///var/jenkins_home/sounds/4579.wav'
emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
} catch(err) {
// 构建失败
currentBuild.result = 'FAILURE'
httpRequest authentication:"${soundsId}", url:'http://${jenkinsHost}/sounds/playSound?src=file:///var/jenkins_home/sounds/8923.wav'
emailext attachlog:true, body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: "${recipients}"
}
}
邮件模板
标题
构建通知:${BUILD_STATUS} - ${PROJECT_NAME} - # ${BUILD_NUMBER}!
内容
${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志
(本邮件是程序自动下发的,请勿回复!)
构建结果 - ${BUILD_STATUS}
构建信息
- 项目名称 : ${PROJECT_NAME}
- 构建编号 : 第${BUILD_NUMBER}次构建
- 触发原因: ${CAUSE}
- 构建日志: ${BUILD_URL}console
- 构建 Url : ${BUILD_URL}
- 项目 Url : ${PROJECT_URL}
Changes Since Last
Successful Build:
- 历史变更记录 : ${PROJECT_URL}changes
${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:
%c
",showPaths=true,changesFormat="[%a]
%m
",pathFormat=" %p"}
Failed Test Results
$FAILED_TESTS
构建日志 (最后 100行):