-
Notifications
You must be signed in to change notification settings - Fork 914
/
Jenkinsfile
129 lines (118 loc) · 4.4 KB
/
Jenkinsfile
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
@Library('devops') _
pipeline {
agent {label 'jenkins-slave-jnlp'}
environment { // https://stackoverflow.com/questions/53541489/updating-environment-global-variable-in-jenkins-pipeline-from-the-stage-level
NAME="admin-tmpl"
NAMESPACE="oppc-rcyj-web"
SERVER_PORT="80"
}
options {
// timestamps() //日志会有时间
skipDefaultCheckout() //删除隐式checkout scm语句
// disableConcurrentBuilds() //禁止并行,会列队式等前面的任务完才进行下一个任务
timeout(time: 1, unit: 'HOURS') //流水线超时设置1h
}
stages {
stage('Init'){
steps {
script{
// 【测试环境】变量
env.INGRESS_HOST='admin-tmpl-test.rencaiyoujia.cn'
env.INGRESS_TLS_SECRET="rencaiyoujia-tls-secret"
env.REPLICAS="1" //deployment的副本数
KUBECONTEXT = "k8s-test-admin" //测试环境k8s
NPM_RUN_BUILD_CMD="npm run build:test"
DOCKERFILE_NAME="Dockerfile"
// 【生产环境】变量
if( "${BRANCH_NAME}"== "master"){
env.INGRESS_HOST='admin-tmpl.rencaiyoujia.com'
env.INGRESS_TLS_SECRET="rencaiyoujia-tls-secret"
env.REPLICAS="2"
KUBECONTEXT="k8s-prod-admin" //生产环境k8s
NPM_RUN_BUILD_CMD="npm run build:prod"
DOCKERFILE_NAME="Dockerfile"
}
// 【开发环境】变量
if( "${BRANCH_NAME}"== "dev"){
env.INGRESS_HOST='admin-tmpl-dev.rencaiyoujia.cn'
env.INGRESS_TLS_SECRET="rencaiyoujia-tls-secret"
env.REPLICAS="1"
KUBECONTEXT="k8s-dev-admin" //开发环境k8s
NPM_RUN_BUILD_CMD="npm run build:dev"
DOCKERFILE_NAME="Dockerfile"
}
}
}
}
stage('Checkout') {
steps {
checkout scm
}
}
stage('npm打包') {
steps {
script{
// sh 'npm config set registry https://registry.npm.taobao.org'
sh 'npm install --registry=https://registry.npm.taobao.org'
sh "${NPM_RUN_BUILD_CMD}"
}
}
}
stage('DockerBuild') {
steps {
script{
sh "rm -rf docker/dist;mv dist docker"
devops.docker(env.NAME,dockerfile:"docker/${DOCKERFILE_NAME}",context:"docker").build().push()
}
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
parallel {
stage('deploy ingress') {
when {
expression { env.INGRESS_HOST != null }
}
steps{
sh """envsubst < k8smanifests/svc.yaml | kubectl --context ${KUBECONTEXT} apply -f - ;\
envsubst < k8smanifests/ingress.yaml | kubectl --context ${KUBECONTEXT} apply -f - ;\
"""
}
}
stage('Deploy') {
steps{
sh """envsubst < k8smanifests/deploy.yaml ;\
envsubst < k8smanifests/deploy.yaml | kubectl --context ${KUBECONTEXT} apply -f - ;\
"""
}
}
}
}
}
post{
success{
script{
devops.dingtalk(env.NAME,env.DOCKERIMAGE,"构建成功 ✅")
}
}
failure{
script{
devops.dingtalk(env.NAME,env.DOCKERIMAGE,"构建失败 ❌")
}
}
unstable{
script{
devops.dingtalk(env.NAME,env.DOCKERIMAGE,"不稳定构建 ✅")
}
}
aborted{
script{
devops.dingtalk(env.NAME,env.DOCKERIMAGE,"暂停或中断 ❌")
}
}
}
}