-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
205 lines (193 loc) · 6.94 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
@Library('jenkins-multibranch-pipeline-library-demo')_
def label = "slave-${UUID.randomUUID().toString()}"
def helmLint(String chartDir) {
println "校验 chart 模板"
sh "helm lint ${chartDir}"
}
// def helmDeploy(Map args) {
// if (args.debug) {
// println "Debug 应用"
// sh "helm upgrade --dry-run --debug --install ${args.name} ${args.chartDir} -f ${args.valuePath} --set image.tag=${args.imageTag} --namespace ${args.namespace}"
// } else {
// println "部署应用"
// sh "helm upgrade --install ${args.name} ${args.chartDir} -f ${args.valuePath} --set image.tag=${args.imageTag} --namespace ${args.namespace}"
// echo "应用 ${args.name} 部署成功. 可以使用 helm status ${args.name} 查看应用状态"
// }
// }
podTemplate(label: label, containers: [
containerTemplate(name: 'golang', image: 'golang:1.14.2-alpine3.11', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'kaniko', image: 'gcr.io/kaniko-project/executor:debug', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'helm', image: 'ntops/helm', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'kubectl', image: 'cnych/kubectl', command: 'cat', ttyEnabled: true)
], serviceAccount: 'jenkins'
// , volumes: [
// hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
// ]
)
{
node(label) {
def myRepo = checkout scm
// 获取 git commit id 作为镜像标签
def imageTag = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
// 仓库地址
def registryUrl = "weekendr01.azurecr.io"
def imageEndpoint = "devops-demo"
// 镜像
def image = "${registryUrl}/${imageEndpoint}:${imageTag}"
stage('下载variables') {
dir('env_config') {
git branch: 'master',credentialsId: 'github-ssh-key',url: 'ssh://git@github.com/plusices/devops-demo.git'
// git branch: 'master', url: 'git@github.com:plusices/devops-demo.git'
}
// steps {
// sh 'find $P_PATH -name mytestfile'
// sh """
// cd ../
// find `pwd` -name mytestfile
// """
// }
sh """
ls env_config
"""
sayHello 'Tux'
sh "cp env_config/*.yaml helm/templates/"
}
stage('代码编译打包') {
try {
container('golang') {
echo "2.代码编译打包阶段"
sh """
export GOPROXY=https://goproxy.cn
GOOS=linux GOARCH=amd64 go build -v -o demo-app
"""
}
} catch (exc) {
println "构建失败 - ${currentBuild.fullDisplayName}"
throw(exc)
}
}
stage('构建 Docker 镜像') {
// dockerBuildPush("${image}")
dockerBuildPush(
regcred: 'regcred-uat',
image: "${image}",
kubeconfig: 'kubeconfig'
)
}
stage('helm打包'){
// when {
// branch 'master'
// // anyOf {
// // environment name: 'DEPLOY_TO', value: 'production'
// // environment name: 'DEPLOY_TO', value: 'staging'
// // }
// }
if (env.BRANCH_NAME ==~ /(master|release)/) {
echo "${env.BRANCH_NAME}"
helmPackage(
regcred: 'agile168',
registryUrl: "${registryUrl}"
)
}
}
// stage('构建 Docker 镜像') {
// withCredentials([file(credentialsId: 'regcred-uat', variable: 'REGCRED'),file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
// container('kaniko') {
// echo "3. 构建 Docker 镜像阶段"
// sh """
// mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config
// cp ${REGCRED} /kaniko/.docker/config.json
// cat /kaniko/.docker/config.json
// /kaniko/executor -f `pwd`/Dockerfile -c `pwd` --destination=${image} -v=debug
// """
// }
// }
// }
// stage('构建 Docker 镜像') {
// withCredentials([[$class: 'UsernamePasswordMultiBinding',
// credentialsId: 'docker-auth',
// usernameVariable: 'DOCKER_USER',
// passwordVariable: 'DOCKER_PASSWORD']]) {
// container('docker') {
// echo "3. 构建 Docker 镜像阶段"
// sh """
// /kaniko/executor -f `pwd`/Dockerfile -c `pwd`/src --cache=true --destination=${image} --insecure --skip-tls-verify -v=debug
// // cat /etc/resolv.conf
// // docker login ${registryUrl} -u ${DOCKER_USER} -p ${DOCKER_PASSWORD}
// // docker build -t ${image} .
// // docker push ${image}
// """
// }
// }
// }
stage('运行 Helm') {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
container('helm') {
sh "mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config"
echo "4.开始 Helm 部署"
def userInput = input(
id: 'userInput',
message: '选择一个部署环境',
parameters: [
[
$class: 'ChoiceParameterDefinition',
choices: "Dev\nQA\nProd",
name: 'Env'
]
]
)
echo "部署应用到 ${userInput} 环境"
// 选择不同环境下面的 values 文件
if (userInput == "Dev") {
// deploy dev stuff
} else if (userInput == "QA"){
// deploy qa stuff
} else {
// deploy prod stuff
}
if (env.BRANCH_NAME ==~ /develop/) {
helmDeploy(
debug : false,
name : "devops-demo",
chartDir : "./helm",
namespace : "kube-ops",
valuePath : "./helm/my-values.yaml",
imageTag : "${imageTag}",
image : "${registryUrl}/${imageEndpoint}"
)
}
}
}
}
stage('运行 Kubectl') {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
container('kubectl') {
sh "mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config"
echo "5.查看应用"
sh "kubectl get all -n kube-ops -l app=devops-demo"
}
}
}
stage('快速回滚?') {
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
container('helm') {
sh "mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config"
def userInput = input(
id: 'userInput',
message: '是否需要快速回滚?',
parameters: [
[
$class: 'ChoiceParameterDefinition',
choices: "Y\nN",
name: '回滚?'
]
]
)
if (userInput == "Y") {
sh "helm rollback devops-demo --namespace kube-ops"
}
}
}
}
}
}