Jenkinsfile
3.85 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!groovy
pipeline {
agent { label 'master' }
parameters {
string(name:'callBack', defaultValue: 'auto', description: '回调服务地址')
choice(name:'clusterId',choices:'aws\nyoho-web-gray\nyoho-ops\nyoho-web\nyoho-media\n', description: '集群的信息')
string(name:'projectName', defaultValue: 'yoho-api-doc', description: 'GITLAB应用名称')
string(name:'projectGitGroup', defaultValue: 'fe', description: 'GITLAB所在的组')
string(name:'deployName', defaultValue: 'yoho-api-doc', description: '应用名称')
string(name:'userMail', defaultValue: 'jimi.ji@yoho.cn', description: '触发的人员邮箱')
string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
string(name:'branch', defaultValue: 'master', description: 'git分支名称')
}
//常量参数,初始确定后一般不需更改
environment{
QA_EMAIL='jimi.ji@yoho.cn'
NODE_ENV='production'
}
options {
//保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '50'))
}
stages {
stage('Declarative: code to images push') {
steps {
script {
echo "PRINT THE PARA ticketID ${params.ticketID}......"
echo "PRINT THE PARA projectName ${params.projectName}......"
echo "PRINT THE PARA projectGitGroup ${params.projectGitGroup}......"
echo "PRINT THE PARA deployName ${params.deployName}......"
echo "PRINT THE PARA userMail ${params.userMail}......"
echo "PRINT THE PARA imageSource ${params.imageSource}......"
if(params.projectName==''){
error "no project ${params.projectName}"
}
def workdir="${env.WORKSPACE}/${params.projectName}"
sh "git clone -b ${params.branch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${workdir}"
if(!fileExists("${workdir}/docker.sh")){
echo "you need docker.sh file in project ${params.projectName}"
error "you need docker.sh file in project ${params.projectName}"
}
if(!fileExists("${workdir}/Dockerfile")){
echo "you need Dockerfile file in project ${params.projectName}"
error "you need Dockerfile file in project ${params.projectName}"
}
sh "sh ${workdir}/docker.sh ${workdir}"
def date = new Date()
def formattedDate = date.format("yyyMMddHHMMSS")
def version="${formattedDate}-${BUILD_NUMBER}"
sh "docker build -t ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${version} ${workdir}"
sh "docker push ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${version}"
sh "docker tag ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${version} ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:latest"
sh "docker push ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:latest"
echo "ending build push ${params.deployName}......"
if(params.callBack!='auto'){
build job: 'Docker-Image-Deplopy', parameters: [string(name: 'callBack', value: "${params.callBack}"), string(name: 'clusterId', value: "${params.clusterId}"), string(name: 'serviceName', value: "${params.deployName}"), string(name: 'containerName', value: "${params.deployName}"), string(name: 'imageInfo', value: "${params.imageSource}/${params.deployName}:${version}") , string(name: 'deployEnv', value: 'qcloud')]
}
}
}
}
}
post {
always {
echo "Pipeline is done,begin send mail"
deleteDir() /* clean up our workspace */
println "###############################"
sh "curl '${callBack}&status=${currentBuild.currentResult}&jobId=${BUILD_NUMBER}'"
mail to: "${env.QA_EMAIL},${params.userMail}",
subject: "PineLine '${JOB_NAME}' (${BUILD_NUMBER}),构建结果: ${currentBuild.currentResult}",
body: "构建的内容为:${params.projectName},提交人员:${params.userMail}"
}
}
}