-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
149 lines (142 loc) · 4.69 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def startNotify(){
sh '''
curl -X POST \
'https://oapi.dingtalk.com/robot/send?access_token='''+DING_DING_TOKEN+'''' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"markdown": {
"title":"Jenkins工程构建开始",
"text": "##### 构建开始 \n > 服务: '''+HELM_NAME+''' \n\n > 分支: '''+GIT_BRANCH+'''"
}
}
'
'''
}
def failureNotify(){
sh '''
curl -X POST \
'https://oapi.dingtalk.com/robot/send?access_token='''+DING_DING_TOKEN+'''' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"markdown": {
"title":"Jenkins工程构建失败",
"text": "##### 构建<font color=Red>失败</font> \n > 服务: '''+HELM_NAME+''' \n\n > 分支: '''+GIT_BRANCH+'''"
}
}
'
'''
}
def successNotify(){
sh '''
curl -X POST \
'https://oapi.dingtalk.com/robot/send?access_token='''+DING_DING_TOKEN+'''' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"markdown": {
"title":"Jenkins工程构建成功",
"text": "##### 构建<font color=Green>成功</font> \n > 服务: '''+HELM_NAME+''' \n\n > 分支: '''+GIT_BRANCH+'''"
}
}
'
'''
}
pipeline{
agent {
node { label env.NODE }
}
environment {
IMAGE_NAME_DEFAULT = "registry-vpc.cn-shanghai.aliyuncs.com/epiboly/demo"
APP_NAME = "triones-admin-frontend"
HELM_NAME = "${APP_NAME}"
DING_DING_TOKEN_DEFAULT = "ed73760cff1f973b16f14401e9cc9b48cbd9a877034251ff5645a2d0e00db596"
DOCKER_REPOSITORY_URL_DEFAULT = 'registry-vpc.cn-shanghai.aliyuncs.com'
ENV_DEFAULT = "dev"
}
stages {
stage('Clone') {
steps{
checkout scm
script {
if( env.DING_DING_TOKEN == null){
env.DING_DING_TOKEN = DING_DING_TOKEN_DEFAULT
}
if( env.IMAGE_NAME == null ){
env.IMAGE_NAME = IMAGE_NAME_DEFAULT
}
if(env.ENV == null){
env.ENV = ENV_DEFAULT
}
if( env.TAG != null || env.TAG != '' ){
env.TAG = "${APP_NAME}-${env.TAG}"
}
if( env.TAG == null || env.TAG == '' ){
env.TAG = "${APP_NAME}-${GIT_COMMIT}"
}
if( env.DOCKER_REPOSITORY_URL == null ){
env.DOCKER_REPOSITORY_URL= DOCKER_REPOSITORY_URL_DEFAULT
}
}
startNotify()
}
}
stage('Build Code') {
when {
allOf{
expression { env.BUILD =='true' }
expression { env.ENV != 'prod' }
}
}
steps{
sh "pnpm install"
sh "pnpm run build"
}
}
stage('Build Image') {
when {
allOf{
expression { env.BUILD=='true' }
expression { env.ENV != 'prod' }
}
}
steps{
echo "当前分支 ${GIT_BRANCH} TAG:${TAG}"
sh "docker build -f Dockerfile --force-rm=true --rm -t ${IMAGE_NAME}:${env.TAG} ."
withCredentials([usernamePassword(credentialsId: "${env.DOCKER_CREDENTIALS_ID}", usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]){
sh "docker login --username=${DOCKER_USERNAME} --password=${DOCKER_PASSWORD} ${DOCKER_REPOSITORY_URL}"
sh "docker push ${IMAGE_NAME}:${TAG}"
}
sh "docker images | grep none |awk '{print \$3}' |xargs -i docker rmi {}"
}
}
stage('Install Helm App') {
when {
expression { env.INSTALL=='true' }
}
steps{
sh "helm upgrade --install --namespace ${env.ENV} --create-namespace --set image.repository=${IMAGE_NAME} --set image.tag=${env.TAG} ${HELM_NAME} kubernetes/chart -f kubernetes/env/${ENV}.yaml"
}
}
stage('Uninstall Helm App') {
when {
expression { env.INSTALL=='false' }
}
steps{
sh "helm uninstall ${HELM_NAME} --namespace ${env.ENV}"
}
}
}
post {
failure {
failureNotify()
}
success {
successNotify()
}
}
}