|
| 1 | +# 在离线环境使用 DevStream 搭建 GitLab + Jenkins + Harbor 工具链,管理 Java Spring Boot 项目开发生命周期全流程 |
| 2 | + |
| 3 | +在“[这个文档](./gitlab-jenkins-harbor-java-springboot.zh.md)”里我们介绍了怎样通过 DevStream 在本地部署 `GitLab + Jenkins + Harbor` 工具链,并且以 Java Spring Boot 项目为例,演示如何使用 DevStream 快速创建 Java Spring Boot 项目脚手架,同时在 Jenkins 上自动创建对应的 Pipeline 实现 Java Spring Boot 项目的 CI 流程。 |
| 4 | + |
| 5 | +本文将要演示的流水线最终效果与上面这个文档中介绍的几乎完全一致,唯一的区别就是本文假设你的服务器是离线的,你只有一台可以访问互联网的 PC,这台 PC 可以通过企业内部网络访问到你要使用 DevStream 的服务器,类似下图这样: |
| 6 | + |
| 7 | +<figure markdown> |
| 8 | + { width="500" } |
| 9 | + <figcaption></figcaption> |
| 10 | +</figure> |
| 11 | + |
| 12 | +!!! info "提醒" |
| 13 | + |
| 14 | + 关于本文最终将要搭建的流水线及其工作效果,请查看“[这个文档](./gitlab-jenkins-harbor-java-springboot.zh.md)”。 |
| 15 | + |
| 16 | +## 1、下载 dtm 和 DevStream Plugins |
| 17 | + |
| 18 | +首先你需要下载 DevStream 的命令行(CLI)工具 `dtm` 和所需的 DevStream 插件(plugins)。 |
| 19 | + |
| 20 | +### 1.1、下载 dtm |
| 21 | + |
| 22 | +如果你的 PC 和服务器操作系统以及 CPU 架构一致,比如都是 Linux/amd64,那么你可以直接在 PC 上运行如下命令完成 dtm 的下载: |
| 23 | + |
| 24 | +```shell |
| 25 | +sh -c "$(curl -fsSL https://download.devstream.io/download.sh)" |
| 26 | +``` |
| 27 | + |
| 28 | +如果使用的 shell 不是 bash,可能会执行上述命令失败,这时候你也可以选择直接用 curl 下载 dtm。这时候你需要先访问 DevStream 的 [Release](https://github.com/devstream-io/devstream/releases/) 页面找到当前最新版本 `dtm`,然后找到和你的服务器匹配的版本。当前 `dtm` 提供了多个版本,分别是: |
| 29 | + |
| 30 | +1. Darwin/arm64 |
| 31 | +2. Darwin/amd64 |
| 32 | +3. Linux/amd64 |
| 33 | + |
| 34 | +这时候,相信你可以很容易拼接出类似下面这个命令: |
| 35 | + |
| 36 | +```shell |
| 37 | +curl -o dtm https://download.devstream.io/v0.10.2/dtm-linux-amd64 |
| 38 | +``` |
| 39 | + |
| 40 | +注意:这里的版本、系统类型、CPU 架构等信息需要灵活调整。比如你用的是 arm64 的服务器,这里就需要将命令改成: |
| 41 | + |
| 42 | +```shell |
| 43 | +curl -o dtm https://download.devstream.io/v0.10.2/dtm-linux-arm64 |
| 44 | +``` |
| 45 | + |
| 46 | +等 dtm 下载到你的 PC 后,你需要通过内网将其传输到某一台内部服务器上,移入包含在"$PATH"的目录里并赋予其可执行权限,比如在 Linux 上你可以执行如下命令完成这些操作: |
| 47 | + |
| 48 | +```shell |
| 49 | +chmod +x dtm |
| 50 | +mv dtm /usr/local/bin/ |
| 51 | +``` |
| 52 | + |
| 53 | +然后在服务器上你可以通过如下命令验证 dtm 的权限以及版本等是否正确: |
| 54 | + |
| 55 | +```shell |
| 56 | +$ dtm version |
| 57 | +0.10.2 |
| 58 | +``` |
| 59 | + |
| 60 | +### 1.2、下载 plugins |
| 61 | + |
| 62 | +继续在你的 PC 上执行如下命令来下载 DevStream plugins: |
| 63 | + |
| 64 | +```shell |
| 65 | +dtm init --download-only --plugins="gitlab-ce-docker, helm-installer, repo-scaffolding, jenkins-pipeline" -d=plugins |
| 66 | +``` |
| 67 | + |
| 68 | +这条命令执行成功后,你可以在本地 plugins 目录下看到如下文件: |
| 69 | + |
| 70 | +```shell |
| 71 | +$ ls plugins/ |
| 72 | +gitlab-ce-docker-linux-amd64_0.10.2.md5 |
| 73 | +helm-installer-linux-amd64_0.10.2.md5 |
| 74 | +jenkins-pipeline-linux-amd64_0.10.2.md5 |
| 75 | +repo-scaffolding-linux-amd64_0.10.2.md5 |
| 76 | +gitlab-ce-docker-linux-amd64_0.10.2.so |
| 77 | +helm-installer-linux-amd64_0.10.2.so |
| 78 | +jenkins-pipeline-linux-amd64_0.10.2.so |
| 79 | +repo-scaffolding-linux-amd64_0.10.2.so |
| 80 | +``` |
| 81 | + |
| 82 | +## 2、下载镜像 |
| 83 | + |
| 84 | +因为 DevStream 需要使用容器化方式部署 GitLab、Jenkins 和 Harbor,那么在开始离线部署前,你需要先下载这几个工具对应的容器镜像。DevStream 提供了这几个工具对应的镜像列表,并且帮你准备了工具脚本从而更加容易地完成镜像离线工作: |
| 85 | + |
| 86 | +1. [GitLab CE images](../plugins/gitlab-ce-docker/gitlab-ce-images.txt) |
| 87 | +2. [Jenkins images](../plugins/helm-installer/jenkins/jenkins-images.txt) |
| 88 | +3. [Harbor images](../plugins/helm-installer/harbor/harbor-images.txt) |
| 89 | + |
| 90 | +你可以通过如下命令将镜像列表下载到本地: |
| 91 | + |
| 92 | +```shell |
| 93 | +curl -o jenkins-images.txt https://raw.githubusercontent.com/devstream-io/devstream/main/docs/plugins/helm-installer/jenkins/jenkins-images.txt |
| 94 | +curl -o harbor-images.txt https://raw.githubusercontent.com/devstream-io/devstream/main/docs/plugins/helm-installer/harbor/harbor-images.txt |
| 95 | +curl -o jenkins-images.txt https://raw.githubusercontent.com/devstream-io/devstream/main/docs/plugins/gitlab-ce-docker/gitlab-ce-images.txt |
| 96 | +``` |
| 97 | + |
| 98 | +可以通过如下命令下载 DevStream 提供的工具脚本,这个脚本可以帮助你快速将这些镜像下载到本地并且上传到私有镜像仓库: |
| 99 | + |
| 100 | +```shell |
| 101 | +curl -o image-pull-push.sh https://raw.githubusercontent.com/devstream-io/devstream/main/hack/image-pull-push.sh |
| 102 | +chmod +x image-pull-push.sh |
| 103 | +``` |
| 104 | + |
| 105 | +如果你还没有一个私有镜像仓库,可以参考[这篇文章](./image-registry.zh.md)快速部署一个 Docker Registry。 |
| 106 | + |
| 107 | +接下来,你就可以通过下述命令快速完成镜像的下载和上传了: |
| 108 | + |
| 109 | +```shell |
| 110 | +# 查看工具脚本的使用方法和注意事项等 |
| 111 | +./image-pull-push.sh -h # (1) |
| 112 | +# 设置镜像仓库地址,按需修改 |
| 113 | +export IMAGE_REPO_ADDR=registry.devstream.io |
| 114 | +# 下载 xxx-images.txt 中所有镜像并保存到本地压缩包中 |
| 115 | +./image-pull-push.sh -f harbor-images.txt -r ${IMAGE_REPO_ADDR} -s |
| 116 | +./image-pull-push.sh -f jenkins-images.txt -r ${IMAGE_REPO_ADDR} -s |
| 117 | +./image-pull-push.sh -f gitlab-ce-images.txt -r ${IMAGE_REPO_ADDR} -s |
| 118 | +# 从压缩包中 load 镜像并 push 到私有镜像仓库(如果镜像仓库需要登录,则需要先手动执行 docker login) |
| 119 | +./image-pull-push.sh -f harbor-images.txt -r ${IMAGE_REPO_ADDR} -l -u |
| 120 | +./image-pull-push.sh -f jenkins-images.txt -r ${IMAGE_REPO_ADDR} -l -u |
| 121 | +./image-pull-push.sh -f gitlab-ce-images.txt -r ${IMAGE_REPO_ADDR} -l -u |
| 122 | +``` |
| 123 | + |
| 124 | +1. 强烈建议你先看下本脚本的使用说明和示例 |
| 125 | + |
| 126 | +!!! note "注意" |
| 127 | + |
| 128 | + 如果你下载镜像的机器和内部私有镜像仓库之间网络隔离,那么你可以在镜像下载到本地压缩包后,先将该压缩包复制到能够访问镜像仓库的机器上,然后再执行 load 和 push 等操作。 |
| 129 | + |
| 130 | +## 3、下载 Helm Chart 包 |
| 131 | + |
| 132 | +你可以通过如下命令下载 Harbor 和 Jenkins 的 Helm chart 包: |
| 133 | + |
| 134 | +```shell |
| 135 | +helm repo add harbor https://helm.goharbor.io |
| 136 | +helm repo update |
| 137 | +helm search repo harbor -l |
| 138 | +helm pull harbor/harbor --version=1.10.0 |
| 139 | +``` |
| 140 | + |
| 141 | +```shell |
| 142 | +helm repo add jenkins https://charts.jenkins.io |
| 143 | +helm repo update |
| 144 | +helm search repo jenkins -l |
| 145 | +helm pull jenkins/jenkins --version=4.2.5 |
| 146 | +``` |
| 147 | + |
| 148 | +执行完上述命令后,你可以在本地看到如下文件: |
| 149 | + |
| 150 | +```shell |
| 151 | +$ ls |
| 152 | +harbor-1.10.0.tgz jenkins-4.2.5.tgz |
| 153 | +``` |
| 154 | + |
| 155 | +## 4、准备配置文件 |
| 156 | + |
| 157 | +这时候需要联网下载的各种“物料”你就准备好了。接着你可以开始编写 DevStream 的配置文件了: |
| 158 | + |
| 159 | +```yaml title="DevStream Config" |
| 160 | +config: |
| 161 | + state: |
| 162 | + backend: local |
| 163 | + options: |
| 164 | + stateFile: devstream.state |
| 165 | +vars: |
| 166 | + imageRepo: registry.devstream.io |
| 167 | + gitlabHostname: gitlab.example.com |
| 168 | + jenkinsHostname: jenkins.example.com |
| 169 | + harborHostname: harbor.example.com |
| 170 | + harborURL: http://harbor.example.com |
| 171 | + jenkinsAdminUser: admin |
| 172 | + jenkinsAdminPassword: changeme |
| 173 | + gitlabSSHPort: 30022 |
| 174 | + gitlabHttpPort: 30080 |
| 175 | + gitlabHttpsPort: 30443 |
| 176 | + |
| 177 | +tools: |
| 178 | +- name: gitlab-ce-docker |
| 179 | + instanceID: default |
| 180 | + dependsOn: [] |
| 181 | + options: |
| 182 | + hostname: [[ gitlabHostname ]] |
| 183 | + gitlabHome: /srv/gitlab |
| 184 | + sshPort: [[ gitlabSSHPort ]] |
| 185 | + httpPort: [[ gitlabHttpPort ]] |
| 186 | + httpsPort: [[ gitlabHttpsPort ]] |
| 187 | + rmDataAfterDelete: false |
| 188 | + imageTag: "rc" |
| 189 | +- name: helm-installer |
| 190 | + instanceID: jenkins-001 |
| 191 | + dependsOn: [] |
| 192 | + options: |
| 193 | + chartPath: "./jenkins-4.2.5.tgz" |
| 194 | + valuesYaml: | |
| 195 | + serviceAccount: |
| 196 | + create: true |
| 197 | + name: jenkins |
| 198 | + controller: |
| 199 | + image: [[ imageRepo ]]/devstreamdev/jenkins |
| 200 | + tag: 2.361.1-jdk11-dtm-0.1 |
| 201 | + imagePullPolicy: "IfNotPresent" |
| 202 | + sidecars: |
| 203 | + configAutoReload: |
| 204 | + image: [[ imageRepo ]]/kiwigrid/k8s-sidecar:1.15.0 |
| 205 | + adminUser: [[ jenkinsAdminUser ]] |
| 206 | + adminPassword: [[ jenkinsAdminPassword ]] |
| 207 | + ingress: |
| 208 | + enabled: true |
| 209 | + hostName: [[ jenkinsHostname ]] |
| 210 | + enableRawHtmlMarkupFormatter: true |
| 211 | + JCasC: |
| 212 | + defaultConfig: true |
| 213 | +- name: helm-installer |
| 214 | + instanceID: harbor-001 |
| 215 | + dependsOn: [] |
| 216 | + options: |
| 217 | + chartPath: "./harbor-1.10.0.tgz" |
| 218 | + valuesYaml: | |
| 219 | + externalURL: [[ harborURL ]] |
| 220 | + expose: |
| 221 | + type: ingress |
| 222 | + tls: |
| 223 | + enabled: false |
| 224 | + ingress: |
| 225 | + hosts: |
| 226 | + core: [[ harborHostname ]] |
| 227 | + nginx: |
| 228 | + image: |
| 229 | + repository: [[ imageRepo ]]/goharbor/nginx-photon |
| 230 | + tag: v2.5.3 |
| 231 | + portal: |
| 232 | + image: |
| 233 | + repository: [[ imageRepo ]]/goharbor/harbor-portal |
| 234 | + tag: v2.5.3 |
| 235 | + core: |
| 236 | + image: |
| 237 | + repository: [[ imageRepo ]]/goharbor/harbor-core |
| 238 | + tag: v2.5.3 |
| 239 | + jobservice: |
| 240 | + image: |
| 241 | + repository: [[ imageRepo ]]/goharbor/harbor-jobservice |
| 242 | + tag: v2.5.3 |
| 243 | + registry: |
| 244 | + registry: |
| 245 | + image: |
| 246 | + repository: [[ imageRepo ]]/goharbor/registry-photon |
| 247 | + tag: v2.5.3 |
| 248 | + controller: |
| 249 | + image: |
| 250 | + repository: [[ imageRepo ]]/goharbor/harbor-registryctl |
| 251 | + tag: v2.5.3 |
| 252 | + chartmuseum: |
| 253 | + enabled: false |
| 254 | + image: |
| 255 | + repository: [[ imageRepo ]]/goharbor/chartmuseum-photon |
| 256 | + tag: v2.5.3 |
| 257 | + trivy: |
| 258 | + enabled: false |
| 259 | + image: |
| 260 | + repository: [[ imageRepo ]]/goharbor/trivy-adapter-photon |
| 261 | + tag: v2.5.3 |
| 262 | + notary: |
| 263 | + enabled: false |
| 264 | + server: |
| 265 | + image: |
| 266 | + repository: [[ imageRepo ]]/goharbor/notary-server-photon |
| 267 | + tag: v2.5.3 |
| 268 | + signer: |
| 269 | + image: |
| 270 | + repository: [[ imageRepo ]]/goharbor/notary-signer-photon |
| 271 | + tag: v2.5.3 |
| 272 | + database: |
| 273 | + internal: |
| 274 | + image: |
| 275 | + repository: [[ imageRepo ]]/goharbor/harbor-db |
| 276 | + tag: v2.5.3 |
| 277 | + redis: |
| 278 | + internal: |
| 279 | + image: |
| 280 | + repository: [[ imageRepo ]]/goharbor/redis-photon |
| 281 | + tag: v2.5.3 |
| 282 | + exporter: |
| 283 | + image: |
| 284 | + repository: [[ imageRepo ]]/goharbor/harbor-exporter |
| 285 | + tag: v2.5.3 |
| 286 | + persistence: |
| 287 | + persistentVolumeClaim: |
| 288 | + registry: |
| 289 | + storageClass: "" |
| 290 | + accessMode: ReadWriteOnce |
| 291 | + size: 5Gi |
| 292 | + jobservice: |
| 293 | + storageClass: "" |
| 294 | + accessMode: ReadWriteOnce |
| 295 | + size: 1Gi |
| 296 | + database: |
| 297 | + storageClass: "" |
| 298 | + accessMode: ReadWriteOnce |
| 299 | + size: 1Gi |
| 300 | + redis: |
| 301 | + storageClass: "" |
| 302 | + accessMode: ReadWriteOnce |
| 303 | + size: 1Gi |
| 304 | +``` |
| 305 | +
|
| 306 | +## 5、开始部署 |
| 307 | +
|
| 308 | +现在你可以通过如下命令开始部署 Jenkins 和 Harbor 了: |
| 309 | +
|
| 310 | +```shell |
| 311 | +dtm apply -f config.yaml -y |
| 312 | +``` |
| 313 | + |
| 314 | +剩下的步骤就联网部署没有什么区别了,你可以查看“[这个文档](./gitlab-jenkins-harbor-java-springboot.zh.md)”继续学习如何验证或清理这条工具链。 |
0 commit comments