Jenkinsfile
5.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
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
136
137
138
139
140
141
142
143
#!groovy
pipeline {
agent { label 'master' }
parameters {
string(name:'projectName', defaultValue: 'yoho-app-web', description: 'GITLAB应用名称')
string(name:'projectGitGroup', defaultValue: 'fe', description: 'GITLAB所在的组')
string(name:'projectBranch', defaultValue: 'release/1.0', description: 'git分支名称')
string(name:'deployName', defaultValue: 'yoho-app-web', description: '应用名称')
string(name:'imageSource', defaultValue: 'yoho-online', description: '镜像存储的仓库')
string(name:'imageVersion', defaultValue: 'release-1.0-20181227144324', description: '镜像存储的版本')
choice(name:'isNeedCdn',choices:'NO\nYES\n', description: '是否需要上传CDN')
}
//常量参数,初始确定后一般不需更改
environment{
CRED_ID='2cb1761b-b258-4e3b-843f-8f0dab16d770'
QA_EMAIL='jimi.ji@yoho.cn'
AWS_IMAGE='860721417875.dkr.ecr.cn-north-1.amazonaws.com.cn'
QCLOUD_IMAGE='ccr.ccs.tencentyun.com'
}
options {
//保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '20'))
}
stages {
stage('Define para && clean workdir'){
steps{
script {
def PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
echo "print the para projectName is : ${params.projectName}......"
echo "print the para workdir is : ${PROJECT_WORK_DIR}......"
echo "print the para projectGitGroup is : ${params.projectGitGroup}......"
echo "print the para deployName is : ${params.deployName}......"
echo "print the para branch is : ${params.projectBranch}......"
echo "print the para isNeedCdn is : ${params.isNeedCdn}......"
echo "print the para imageSource is : ${params.imageSource}......"
echo "print the para imageVersion is : ${params.imageVersion}......"
if(fileExists("${PROJECT_WORK_DIR}/")){
echo "starting delete the dir ${PROJECT_WORK_DIR}"
sh "rm -rf ${PROJECT_WORK_DIR}"
}
}
}
}
stage('git clone project'){
steps{
script {
def PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
sh "git clone -b ${params.projectBranch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${PROJECT_WORK_DIR}"
}
}
}
stage('build project'){
steps{
script {
def PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
sh "cd ${PROJECT_WORK_DIR} && sh ${PROJECT_WORK_DIR}/docker.sh ${params.isNeedCdn} ${PROJECT_WORK_DIR}"
}
}
}
stage('upload to CDN and nfs'){
steps{
script {
def PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
println "begin upload to cdn"
if(params.isNeedCdn=='YES'){
def olddir = "this_is_replace_for_qshell_upload_to_cdn"
def repalceDir = "${PROJECT_WORK_DIR}/dist/statics"
println repalceDir
sh "sed -i 's?${olddir}?${repalceDir}?g' ${env.WORKSPACE}/Docker-Node-Build/LocalUploadConfig"
sh "/home/soft/qshell qupload 3 ${env.WORKSPACE}/Docker-Node-Build/LocalUploadConfig"
sh "cp -r ${repalceDir} /nfs-ops/Data/app/node-ci/data"
}else {
println "params.isNeedCdn is not yes,no upload"
}
}
}
}
stage('build and push images to qcloud') {
steps {
script {
def PROJECT_WORK_DIR="${env.WORKSPACE}/${params.projectName}"
echo "begin bake image......"
def qcloudimage = "${env.QCLOUD_IMAGE}/${params.imageSource}/${params.deployName}:${params.imageVersion}"
sh "docker build -t ${qcloudimage} ${PROJECT_WORK_DIR}/dist/node"
sh "docker push ${qcloudimage}"
if(params.projectName=='yoho-activity-platform'){
def qcloudimage_activity = "${env.QCLOUD_IMAGE}/${params.imageSource}/yoho-activity-platform-admin:${params.imageVersion}"
sh "docker tag ${qcloudimage} ${qcloudimage_activity}"
sh "docker push ${qcloudimage_activity}"
}
echo "end bake qcloud image......"
}
}
}
stage('build and push images to aws') {
steps {
script {
if(check_push_aws_images(params.projectName)){
def qcloudimage = "${env.QCLOUD_IMAGE}/${params.imageSource}/${params.deployName}:${params.imageVersion}"
def awsimage = "${env.AWS_IMAGE}/${params.imageSource}/${params.deployName}:${params.imageVersion}"
//aws docker login session timeout
sh '$(/root/.local/bin/aws ecr get-login --no-include-email)'
sh "docker tag ${qcloudimage} ${awsimage} "
sh "docker push ${awsimage}"
echo "upload images to aws : ${params.projectName}"
}else {
println "there is no need to upload images to aws : ${params.projectName}"
}
}
}
}
}
post {
always {
echo "clean up our workspace"
deleteDir() /* clean up our workspace */
}
}
}
def check_push_aws_images(project_name){
println "begin check push aws images";
def lst = [];
lst.add("yohobuywap-node");
lst.add("yohobuy-node");
lst.add("yoho-app-web");
lst.add("ufo-app-web");
return lst.contains(project_name);
}