Jenkinsfile
4.67 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
126
127
128
129
130
131
132
133
134
135
#!groovy
pipeline {
agent { label 'master' }
parameters {
string(name:'callBack', defaultValue: 'noNeedCallBack', description: '回调服务地址')
choice(name:'clusterId',choices:'aws\nyoho-media\nyoho-ops\nyoho-web\nyoho-web-gray', description: '集群的信息')
string(name:'projectName', defaultValue: 'yoho-now', description: 'GITLAB应用名称')
string(name:'projectGitGroup', defaultValue: 'YOHONow', description: 'GITLAB所在的组')
string(name:'deployName', defaultValue: 'java-yoho-now', description: '应用名称')
string(name:'branch', defaultValue: 'master', description: 'git分支名称')
string(name:'userMail', defaultValue: 'jimi.ji@yoho.cn', description: '触发的人员名单')
choice(name:'nodeEnv',choices:'yoho-gray\nyoho-online\n', description: '选择环境')
string(name:'imageSource', defaultValue: 'yoho-ops', description: '镜像存储的仓库')
}
environment{
CRED_ID='2cb1761b-b258-4e3b-843f-8f0dab16d770'
QA_EMAIL='jimi.ji@yoho.cn'
DOCKER_BUILD_GIT='git@git.yoho.cn:test/docker-shell.git'
}
//保持构建的最大个数
options {
buildDiscarder(logRotator(numToKeepStr: '20'))
}
//pipeline的各个阶段场景
stages {
stage('init && check parameters') {
steps {
script {
if(params.projectName==''){
error 'no projectName'
}
if(params.deployName==''){
error 'no deployName'
}
if(params.nodeEnv==''){
error 'no nodeEnv'
}
if(params.branch==''){
error 'no branch'
}
if(params.imageSource==''){
error 'no imageSource'
}
def date = new Date()
def formattedDate = date.format("yyyMMddHHMMSS")
def version="${formattedDate}-${BUILD_NUMBER}"
QCLOUD_IMAGE_VERSION="${params.imageSource}/${params.deployName}:${version}"
PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
QCLOUD_IMAGE_STORE="ccr.ccs.tencentyun.com/${QCLOUD_IMAGE_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 clusterId ${params.clusterId}......"
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 branch ${params.branch}......"
echo "print the para nodeEnv ${params.nodeEnv}......"
echo "print the para userMail ${params.userMail}......"
echo "print the para imageSource ${params.imageSource}......"
echo "PRINT THE PARA PROJECT_WORK_DIR ${PROJECT_WORK_DIR}......"
echo "PRINT THE PARA QCLOUD_IMAGE_STORE ${QCLOUD_IMAGE_STORE}......"
}
}
stage('download package script') {
steps {
git credentialsId:CRED_ID, url:DOCKER_BUILD_GIT
script {
sh "chmod +x ${env.WORKSPACE}/java-new/packageDockerImage.sh ${env.WORKSPACE}/java-new/expectPackage.exp"
}
}
}
stage('package') {
steps{
echo "starting to build package"
script {
sh "sh ${env.WORKSPACE}/java-new/packageDockerImage.sh -g ${params.projectGitGroup} -b ${params.branch} -a ${params.projectName} -s ${params.nodeEnv} -w ${env.WORKSPACE}"
}
}
}
stage('buid && push images') {
steps{
echo "starting to buid && push images"
script {
sh "docker build -t ${QCLOUD_IMAGE_STORE} ${PROJECT_WORK_DIR}"
sh "docker push ${QCLOUD_IMAGE_STORE}"
}
}
}
stage('deploy to qcloud') {
steps{
echo "starting to deploy to qcloud"
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 notify && clean workspace"
deleteDir() /* clean up our workspace */
if(params.callBack!='noNeedCallBack'){
sh "curl '${params.callBack}&status=${currentBuild.currentResult}&jobID=${JOB_NAME},${BUILD_NUMBER}'"
}
mail to: QA_EMAIL,
subject: "PineLine '${JOB_NAME}' (${BUILD_NUMBER}),构建结果: ${currentBuild.currentResult}",
body: "构建的内容为:${params.projectName},提交人员:${params.userMail}"
}
}
}
}