forked from xdstone1/java-webapp-docker
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Jenkinsfile
76 lines (69 loc) · 3 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
pipeline{
// 定义groovy脚本中使用的环境变量
environment{
// 本示例中使用DEPLOY_TO_K8S变量来决定把应用部署到哪套容器集群环境中,如“Production Environment”, “Staging001 Environment”等
IMAGE_TAG = sh(returnStdout: true,script: 'echo $image_tag').trim()
ORIGIN_REPO = sh(returnStdout: true,script: 'echo $origin_repo').trim()
REPO = sh(returnStdout: true,script: 'echo $repo').trim()
BRANCH = sh(returnStdout: true,script: 'echo $branch').trim()
}
// 定义本次构建使用哪个标签的构建环境,本示例中为 “slave-pipeline”
agent{
node{
label 'slave-pipeline'
}
}
// "stages"定义项目构建的多个模块,可以添加多个 “stage”, 可以多个 “stage” 串行或者并行执行
stages{
// 定义第一个stage, 完成克隆源码的任务
stage('Git'){
steps{
git branch: '${BRANCH}', credentialsId: '', url: 'https://github.com/echoesian/java-webapp-docker.git'
}
}
// 添加第二个stage, 运行源码打包命令
/*stage('Package'){
steps{
container("maven") {
sh "mvn package -B -DskipTests"
}
}
}*/
// 添加第四个stage, 运行容器镜像构建和推送命令, 用到了environment中定义的groovy环境变量
stage('Image Build And Publish'){
steps{
container("kaniko") {
sh "kaniko -f `pwd`/Dockerfile -c `pwd` --destination=${ORIGIN_REPO}/${REPO}:${IMAGE_TAG} --verbosity debug"
}
}
}
stage('Deploy to Kubernetes') {
parallel {
stage('Deploy to Production Environment') {
when {
expression {
"$BRANCH" == "master"
}
}
steps {
container('kubectl') {
step([$class: 'KubernetesDeploy', authMethod: 'certs', apiServerUrl: 'https://kubernetes.default.svc.cluster.local:443', credentialsId:'k8sCertAuth', config: 'deployment.yaml',variableState: 'ORIGIN_REPO,REPO,IMAGE_TAG'])
}
}
}
stage('Deploy to Staging001 Environment') {
when {
expression {
"$BRANCH" == "latest"
}
}
steps {
container('kubectl') {
step([$class: 'KubernetesDeploy', authMethod: 'certs', apiServerUrl: 'https://kubernetes.default.svc.cluster.local:443', credentialsId:'k8sCertAuth', config: 'deployment.yaml',variableState: 'ORIGIN_REPO,REPO,IMAGE_TAG'])
}
}
}
}
}
}
}