Jenkinsfile
4.2 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!groovy
pipeline {
agent { label 'master' }
parameters {
string(name:'callBack', defaultValue: 'noNeedCallBack', description: '回调服务地址')
choice(name:'clusterId',choices:'aws\nyoho-web-gray\nyoho-ops\nyoho-web\nyoho-media\nyoho-java\nyoho-web-new\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='hua.qu@yoho.cn'
NODE_ENV='production'
}
options {
//保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '20'))
}
stages {
stage('init') {
steps {
script {
if(params.projectName==''){
error "no project ${params.projectName}"
}
PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
def date = new Date()
def formattedDate = date.format("yyyMMddHHMMSS")
def version="${formattedDate}-${BUILD_NUMBER}"
QCLOUD_IMAGE_VERSION="${params.imageSource}/${params.deployName}:${version}"
QCLOUD_IMAGE_STORE="ccr.ccs.tencentyun.com/${params.imageSource}/${params.deployName}:${version}"
if(fileExists("${PROJECT_WORK_DIR}/")){
echo "starting delete the dir ${PROJECT_WORK_DIR}"
sh "rm -rf ${PROJECT_WORK_DIR}"
}
}
echo "PRINT THE PARA callBack ${params.callBack}......"
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}......"
echo "PRINT THE PARA clusterId ${params.clusterId}......"
echo "PRINT THE PARA branch ${params.branch}......"
echo "PRINT THE PARA PROJECT_WORK_DIR ${PROJECT_WORK_DIR}......"
echo "PRINT THE PARA QCLOUD_IMAGE_STORE ${QCLOUD_IMAGE_STORE}......"
}
}
stage('git clone project') {
steps {
script {
sh "git clone -b ${params.branch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${PROJECT_WORK_DIR}"
echo "starting check the project {params.projectName}"
if(!fileExists("${PROJECT_WORK_DIR}/docker.sh")){
echo "you need docker.sh file in project ${params.projectName}"
error "you need docker.sh file in project ${params.projectName}"
}
if(!fileExists("${PROJECT_WORK_DIR}/Dockerfile")){
echo "you need Dockerfile file in project ${params.projectName}"
error "you need Dockerfile file in project ${params.projectName}"
}
}
}
}
stage('build project') {
steps {
script {
sh "sh ${PROJECT_WORK_DIR}/docker.sh ${PROJECT_WORK_DIR}"
}
}
}
stage('build and push images to qcloud') {
steps {
script {
sh "docker build -t ${QCLOUD_IMAGE_STORE} ${PROJECT_WORK_DIR}"
sh "docker push ${QCLOUD_IMAGE_STORE}"
}
}
}
stage('deploy to qcloud') {
steps {
script {
if(params.callBack!='noNeedDeploy'){
build job: 'Docker-Image-Deplopy', parameters: [
string(name: 'callBack', value: "noNeedCallBack"),
string(name: 'clusterId', value: "${params.clusterId}"),
string(name: 'serviceName', value: "${params.deployName}"),
string(name: 'containerName', value: "${params.deployName}"),
string(name: 'imageInfo', value: "${QCLOUD_IMAGE_VERSION}"),
string(name: 'deployEnv', value: 'qcloud')]
}
}
}
}
}
post {
always {
script {
echo "Pipeline is done,begin send mail"
deleteDir()
if(params.callBack!='noNeedCallBack'){
sh "curl '${params.callBack}&status=${currentBuild.currentResult}&jobID=${JOB_NAME},${BUILD_NUMBER}'"
}
}
}
}
}