-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathciJenkinsfile
174 lines (149 loc) · 5.81 KB
/
ciJenkinsfile
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
// 声明 CI Pipeline.
pipeline {
// environment 用于设置变量,变量作用域是该管道.
environment {
// 为 Helm Charts 源代码仓库及 Helm Charts Package 仓库设置变量.
// gitSourceRepo 仓库包含了 Helm Charts 的源代码,也有本书其他相关代码,如 Jenkinsfile.
// gitPackageRepo 是 Helm Package 存储库.
// 这两个仓库地址,你都需要修改成 Fork 后的地址.
gitSourceRepo = 'https://github.com/weiwendi/learn-helm.git'
gitPackageRepo = 'https://github.com/weiwendi/charts.git'
// 定义凭证变量,我们在 Jenkins 控制台创建了两个凭证,ID 分别为 github、kubeconfig.
// 在向 gitPackageRepo 推送 Charts 包时会用到 GITAUTH 变量.
// KUBECONFIG 会被挂在到 Pod 中,与 Kubernetes 集群进行交互.
GITAUTH = credentials('github')
KUBECONFIG = credentials('kubeconfig')
}
// 定义执行 Job 的 jenkins agent;
// agent 是运行在 Kubernetes 集群中的一个 Pod.
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
serviceAccountName: jenkins
containers:
# 定义容器的名称
- name: test-and-release-ci
# 指定容器的镜像,该镜像包含了 helm/ct/yamllint/yamale/git/kubectl.
# 镜像 Dockerfile 可以在源码仓库的 docker 目录下查看.
image: registry.cn-beijing.aliyuncs.com/sretech/cttools:latest
command:
- sleep
args:
- infinity
resources:
requests:
memory: "1024Mi"
cpu: "1000m"
limits:
memory: "1024Mi"
cpu: "1000m"
'''
// 设置执行各 stages 时默认使用 test-and-release-ci 容器
defaultContainer 'test-and-release-ci'
}
}
// Jenkins 选项
options {
// 显示执行的时间,需要 Timestamper 插件支持.
timestamps()
// 在 Stage View 中展示近 10 次构建信息
buildDiscarder(logRotator(numToKeepStr: '10'))
}
// 定义管道执行的各个阶段、每个阶段的具体步骤,建议包含至少一个 stage.
stages {
// stage 定义了 pipeline 完成的所有实际工作.
// 首先,我们定义一个打印构建信息的 stage.
stage('Build Messages') {
// steps 定义了管道中具体执行的步骤.
steps {
// container 选择哪个容器执行该 steps,不是用 container 选项的话,会使用默认容器执行 steps.
container("test-and-release-ci") {
echo " workspace: ${WORKSPACE}\n gitPackageRepo: ${gitPackageRepo}\n gitSourceRepo: ${gitSourceRepo}\n branch: ${env.BRANCH_NAME}\n buildId: ${BUILD_ID}"
}
}
}
stage("Copy Kubeconfig") {
steps {
script {
sh '''
mkdir -p ~/.kube
cp ${KUBECONFIG} ~/.kube/config
'''
}
}
}
stage("List Changed Charts") {
steps {
script {
sh "ct list-changed"
}
}
}
stage("Lint") {
steps {
container("test-and-release-ci") {
sh "ct lint"
}
}
}
stage("Install & Test") {
steps {
script {
sh "ct install --upgrade"
}
}
}
stage("Package Charts") {
steps {
script {
sh "helm package --dependency-update helm-charts/charts/*"
sh "ls -l"
}
}
}
stage("Push Charts to Chart Repo") {
steps {
script {
def baseBranch = "main"
// 克隆 Helm Chart Package 包存储库到 chart-repo 目录.
sh "git clone ${env.gitPackageRepo} chart-repo"
// 使用 if 语句,根据分支判断图表包应该推送到 stable 或是 staging 目录.
def repoType
if (env.BRANCH_NAME == baseBranch) {
repoType = "stable"
} else {
repoType = "staging"
}
// 如果不存在,创建 stable 或 staging 目录.
def files = sh(script: "ls chart-repo", returnStdout: true)
if (!files.contains(repoType)) {
sh "mkdir chart-repo/${repoType}"
}
// 移动图表包到 stable 或 staging 目录.
sh "mv *.tgz chart-repo/${repoType}/"
// 更新索引文件.
sh "helm repo index chart-repo/${repoType}"
// 更新 git 配置信息,需要填写你自己的 github 邮箱及用户名.
sh """
git config --global user.email 'weiwendi@aiops.red'
git config --global user.name 'weiwendi'
"""
dir("chart-repo") {
// Add and commit the changes
sh "git add --all"
sh "git commit -m 'pushing charts from branch ${env.BRANCH_NAME}'"
// 推送图表包到 gitPackageRepo 仓库.
script {
// Inject GitHub auth and push to the repo where charts are being served
def authRepo = env.gitPackageRepo.replace('://', '://${GITAUTH_USR}:${GITAUTH_PSW}@')
sh "git push ${authRepo} ${baseBranch}"
}
}
}
}
}
}
}