BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置

Jenkinsfile是Jenkins中对于Pipeline as code的实现,可以通过一个配置文件告诉Jenkins该如何运行流水线。同时允许我们将流水线配置和代码一起保存在代码库中,确保流水线的设置也可以被版本管理。

01 - 添加环境变量和密钥配置

进入 系统管理 | 系统配置,在 全局属性 | 环境变量 中添加如下变量和值,点击保存。

  • REGISTRY_URL: 你的私有镜像仓库服务器地址,如果使用阿里云镜像服务则直接填写 registry.cn-hangzhou.aliyuncs.com
  • REGISTRY_NS: 你所创建的镜像仓库命名空间名称
  • REGISTRY_USER: 你用来登录镜像仓库的用户名
  • REGISTRY_PWD: 你用例登录镜像仓库的密码
  • DEVOPSBOX_HOST_IP: DevOpsBox的IP地址,应该为:192.168.99.102

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图1

进入 系统管理 | Manage Credentials 中,添加密钥信息

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图2

在 凭据 | Jenkins | 全局 上点击 添加凭据

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图3

添加如下凭据,并点击 确定

  • 用户名和密码:我们的标准用户名 localadmin 和密码
  • ID: CREDS_DEVOPSBOX

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图4

02 - 添加用于环境部署的docker-compose-template文件

使用vscode在hello-boathouse中添加 docker-compose-template.yaml 文件

  1. version: "2.1"
  2. services:
  3. web:
  4. image: #{REGISTRY_URL}#/#{REGISTRY_NS}#/hello-boathouse:latest
  5. ports:
  6. - "3001:3000"

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图5

03 - 添加Jenkinsfile文件

使用vscode在hello-boathouse中添加 Jenkinsfile 文件

  1. def getHost() {
  2. def remote = [:]
  3. remote.name = 'server-dev'
  4. remote.host = "${DEVOPSBOX_HOST_IP}"
  5. remote.user = "${env.CREDS_DEVOPSBOX_USR}"
  6. remote.password = "${env.CREDS_DEVOPSBOX_PSW}"
  7. remote.port = 22
  8. remote.allowAnyHosts = true
  9. return remote
  10. }
  11. pipeline {
  12. agent{
  13. label 'vm-slave'
  14. }
  15. environment {
  16. CREDS_DEVOPSBOX = credentials('CREDS_DEVOPSBOX')
  17. }
  18. stages {
  19. stage('Output Env Variables'){
  20. steps {
  21. sh "printenv"
  22. }
  23. }
  24. stage('Docker Build') {
  25. steps {
  26. sh 'docker build -f Dockerfile -t ${REGISTRY_URL}/${REGISTRY_NS}/hello-boathouse:latest .'
  27. }
  28. }
  29. stage('Docker Push') {
  30. steps {
  31. echo "login to docker registry ..."
  32. sh 'docker login ${REGISTRY_URL} -u ${REGISTRY_USER} -p ${REGISTRY_PWD}'
  33. echo "push the image ..."
  34. sh 'docker push ${REGISTRY_URL}/${REGISTRY_NS}/hello-boathouse:latest'
  35. }
  36. post {
  37. success{
  38. echo "clean up local image ..."
  39. sh "docker rmi ${REGISTRY_URL}/${REGISTRY_NS}/hello-boathouse:latest"
  40. }
  41. }
  42. }
  43. // dev 环境
  44. stage('deploy-dev') {
  45. steps {
  46. sh "sed -i 's/#{REGISTRY_URL}#/${REGISTRY_URL}/g' docker-compose-template.yaml"
  47. sh "sed -i 's/#{REGISTRY_NS}#/${REGISTRY_NS}/g' docker-compose-template.yaml"
  48. script {
  49. server = getHost()
  50. echo "copy docker-compose file to remote server..."
  51. sshRemove remote: server, path: "./docker-compose-template.yaml" // 先删除远程服务器上的文件,已确保是最新的文件
  52. sshPut remote: server, from: './docker-compose-template.yaml', into: '.'
  53. echo "stopping previous docker containers..."
  54. sshCommand remote: server, command: "docker login ${REGISTRY_URL} -u ${REGISTRY_USER} -p ${REGISTRY_PWD}"
  55. sshCommand remote: server, command: "docker-compose -f docker-compose-template.yaml -p hello-boathouse down"
  56. echo "pulling newest docker images..."
  57. sshCommand remote: server, command: "docker-compose -f docker-compose-template.yaml -p hello-boathouse pull"
  58. echo "restarting new docker containers..."
  59. sshCommand remote: server, command: "docker-compose -f docker-compose-template.yaml -p hello-boathouse up -d"
  60. echo "successfully started!..."
  61. }
  62. }
  63. }
  64. }
  65. }

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图6

04 - 提交配置文件并触发流水线

运行以下命令提交文件到Gitea

  1. git add .
  2. git commit -m "adde pipeline configurations"
  3. git push origin main

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图7

在 Jenkins 上进入我们创建的 boathouse 任务页面,并点击 立即扫描 Gitea Oraganziation

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图8

刷新页面,Jenkins已经将hello-boathouse添加到流水线列表中

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图9

进入 hello-boathouse 任务界面,查看任务进展

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图10

如果任务已经成功完成,打开 http://192.168.99.102:3001 即可访问 hello-boathouse 应用

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图11

05 - 在Gitea上添加Webhook实现提交触发

我们刚才的设置因为使用了1分钟的检查机制,Jenkins基本上可以及时的发现改动并构建,但是更好的方式是由Gitea通知Jenkins。

在 boathouse/hello-boathouse 仓库设置 | 管理Web钩子 中点击 添加 Web钩子

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图12

将目标URL设置为 http://192.168.99.102:8080/gitea-webhook/post,也就是我们Jenkins服务器上的钩子地址,点击 添加Web钩子

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图13

完成后可以通过 测试推送 按钮来验证钩子工作正常

BHOL603 - 为hello-boathouse添加Jenkinsfile并完成基础流水线配置 - 图14

现在,我们就可以实现推送代码后自动触发流水线了。

小结

本节实验完成了一个基本的Jenkins流水线的搭建。