Jenkinsfile
5.44 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
#!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: '镜像存储的版本')
string(name:'isNeedCdn',defaultValue:'YES', 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 workdir="${env.WORKSPACE}/$params.projectName"
echo "print the para projectName is : ${params.projectName}......"
echo "print the para workdir is : ${workdir}......"
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}......"
def localdir = "${workdir}/docker.sh"
if(fileExists("$localdir")){
echo "starting delete the dir ${workdir}"
sh "rm -rf ${workdir}"
}
}
}
}
stage('git clone project'){
steps{
script {
def workdir="${env.WORKSPACE}/$params.projectName"
sh "git clone -b ${params.projectBranch} git@git.yoho.cn:${params.projectGitGroup}/${params.projectName}.git ${workdir}"
}
}
}
stage('build project'){
steps{
script {
def workdir="${env.WORKSPACE}/$params.projectName"
sh "cd ${workdir} && sh ${workdir}/docker.sh ${params.isNeedCdn} ${workdir}"
}
}
}
stage('upload to CDN and nfs'){
steps{
script {
println "begin upload to cdn"
if(params.isNeedCdn=='YES'){
def workdir="${env.WORKSPACE}/$params.projectName"
def olddir = "/data/var-lib/jenkins/workspace/replace"
def repalceDir = "${workdir}/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 {
echo "begin bake image......"
def workdir="${env.WORKSPACE}/$params.projectName"
def qcloudimage = "${env.QCLOUD_IMAGE}/${params.imageSource}/${params.deployName}:${params.imageVersion}"
sh "docker build -t ${qcloudimage} ${workdir}/dist/node"
sh "docker push ${qcloudimage}"
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");
return lst.contains(project_name);
}