Jenkinsfile
3.69 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!groovy
pipeline {
agent { label 'master' }
parameters {
string(name:'ticketID', defaultValue: 'auto', description: '工单ID')
string(name:'projectName', defaultValue: 'yoho-api-doc', description: 'GITLAB应用名称')
string(name:'projectGitGroup', defaultValue: 'fe', description: 'GITLAB所在的组')
string(name:'projectBranch', defaultValue: 'master', description: 'git分支名称')
//deployName 对应了镜像名称,服务名称,aws中任务,以及服务总称
string(name:'deployName', defaultValue: 'yoho-api-doc', description: '应用名称,对应k8s的服务名,aws中任务以及服务的名称')
string(name:'userMail', defaultValue: 'jimi.ji@yoho.cn', description: '触发的人员邮箱')
string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
}
//常量参数,初始确定后一般不需更改
environment{
QA_EMAIL='jimi.ji@yoho.cn'
NODE_ENV='production'
OPS_CALL_BACK='http://www.yohops.com/outer/updateJenkinsStatus?'
}
options {
//保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '50'))
}
stages {
stage("Define the basis para"){
steps{
script {
}
}
}
stage("git clone project : ${params.projectName}"){
steps{
script {
}
}
}
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}......"
}
}
}
}
post {
always {
echo "Pipeline is done,begin send mail"
deleteDir() /* clean up our workspace */
println "###############################"
sh "curl '${OPS_CALL_BACK}ticketID=${params.ticketID}&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}"
}
}
}