|
| 1 | +# 自动部署 |
| 2 | + |
| 3 | +部署自动化其实不是一个框架的刚需,有很多方式可以将一个服务进行自动化部署,比如现在比较流行的 Docker 化或者 CI/CD |
| 4 | +流程。但是一些比较个人比较小的项目,比如一个博客、一个官网网站,这些部署流程往往都太庞大了,更需要一个服务,能快速将在开发机器上写好、调试好的程序上传到目标服务器,并且更新应用程序。这就是hade框架实现的发布自动化。 |
| 5 | + |
| 6 | +## SSH |
| 7 | + |
| 8 | +所有的部署自动化工具,基本都依赖本地与远端服务器的连接,这个连接可以是 FTP,可以是 HTTP,但是更经常的连接是 SSH 连接。 |
| 9 | +基本上,SSH 账号是我们拿到 Web 服务器的首要凭证,所以要设计的自动化发布系统也是依赖 SSH 的。 |
| 10 | + |
| 11 | +应的配置文件如下 config/testing/ssh.yaml,你可以看看每个配置的说明: |
| 12 | + |
| 13 | +```yaml |
| 14 | + |
| 15 | +timeout: 1s |
| 16 | +network: tcp |
| 17 | +web-01: |
| 18 | + host: 118.190.3.55 # ip地址 |
| 19 | + port: 22 # 端口 |
| 20 | + username: yejianfeng # 用户名 |
| 21 | + password: "123456" # 密码 |
| 22 | +web-02: |
| 23 | + network: tcp |
| 24 | + host: localhost # ip地址 |
| 25 | + port: 3306 # 端口 |
| 26 | + username: jianfengye # 用户名 |
| 27 | + rsa_key: "/Users/user/.ssh/id_rsa" |
| 28 | + known_hosts: "/Users/user/.ssh/known_hosts" |
| 29 | +``` |
| 30 | +
|
| 31 | +SSH 的连接方式有两种,一种是直接使用用户名密码来连接远程服务器,还有一种是使用 rsa key 文件来连接远端服务器,所以这里的配置需要同时支持两种配置。对于使用 |
| 32 | +rsa key 文件的方式,需要设置 rsk_key 的私钥地址和负责安全验证的 known_hosts。 |
| 33 | +
|
| 34 | +## deploy |
| 35 | +
|
| 36 | +我们的 hade 框架是同时支持前后端的开发框架,所以自动化部署是需要同时支持前后端部署的,也就是说它的命令也需要支持前后端的部署,这里我们设计一个显示帮助信息的一级命令./hade |
| 37 | +deploy 和四个二级命令: |
| 38 | +
|
| 39 | +```markdown |
| 40 | +./hade deploy frontend ,部署前端 |
| 41 | +./hade deploy backend ,部署后端 |
| 42 | +./hade deploy all ,同时部署前后端 |
| 43 | +./hade deploy rollback ,部署回滚 |
| 44 | +``` |
| 45 | + |
| 46 | +完整的配置文件在 config/development/deploy.yaml 中: |
| 47 | + |
| 48 | +```yaml |
| 49 | + |
| 50 | +connections: # 要自动化部署的连接 |
| 51 | + - ssh.web-01 |
| 52 | + |
| 53 | +remote_folder: "/home/yejianfeng/coredemo/" # 远端的部署文件夹 |
| 54 | + |
| 55 | +frontend: # 前端部署配置 |
| 56 | + pre_action: # 部署前置命令 |
| 57 | + - "pwd" |
| 58 | + post_action: # 部署后置命令 |
| 59 | + - "pwd" |
| 60 | + |
| 61 | +backend: # 后端部署配置 |
| 62 | + goos: linux # 部署目标操作系统 |
| 63 | + goarch: amd64 # 部署目标cpu架构 |
| 64 | + pre_action: # 部署前置命令 |
| 65 | + - "pwd" |
| 66 | + post_action: # 部署后置命令 |
| 67 | + - "chmod 777 /home/yejianfeng/coredemo/hade" |
| 68 | + - "/home/yejianfeng/coredemo/hade app restart" |
| 69 | +``` |
| 70 | +
|
| 71 | +### 部署前端 |
| 72 | +
|
| 73 | +你可以通过命令 |
| 74 | +
|
| 75 | +```shell |
| 76 | +./hade deploy frontend |
| 77 | +``` |
| 78 | + |
| 79 | +或者 跳过编译环节 |
| 80 | + |
| 81 | +```shell |
| 82 | +./hade deploy frontend -s=true |
| 83 | +``` |
| 84 | + |
| 85 | +第一个方法会直接运行npm run build,把前端代码生成在dist目录下,然后把dist目录下的文件上传到远端服务器,然后执行前置命令和后置命令。 |
| 86 | +而第二个方法会掉过编译,直接把dist目录下的文件上传到远端服务器,然后执行前置命令和后置命令。 |
| 87 | + |
| 88 | +下面就是我们的hade官网的部署过程。 |
| 89 | +我每次都在本地通过vuepress编译docs目录下的markdown到dist目录下,然后通过hade部署到远端服务器。 |
| 90 | + |
| 91 | +``` markdown |
| 92 | +➜ hade git:(main) ✗ ./hade deploy frontend -s=true |
| 93 | +[Info] 2022-12-14T20:37:46+08:00 "execute pre action start" map[cmd:pwd connection:ssh.web-01] |
| 94 | +[Info] 2022-12-14T20:37:46+08:00 "execute pre action" map[cmd:pwd connection:ssh.web-01 out:/home/yejianfeng] |
| 95 | +[Info] 2022-12-14T20:37:46+08:00 "mkdir: /webroot/hade_doc/dist" map[] |
| 96 | +[Info] 2022-12-14T20:37:46+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/404.html to remote file: /webroot/hade_doc/dist/404.html finish" map[] |
| 97 | +[Info] 2022-12-14T20:37:46+08:00 "mkdir: /webroot/hade_doc/dist/assets" map[] |
| 98 | +[Info] 2022-12-14T20:37:46+08:00 "mkdir: /webroot/hade_doc/dist/assets/css" map[] |
| 99 | +[Info] 2022-12-14T20:37:46+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/css/0.styles.fb3ee4f4.css to remote file: /webroot/hade_doc/dist/assets/css/0.styles.fb3ee4f4.css finish" map[] |
| 100 | +[Info] 2022-12-14T20:37:46+08:00 "mkdir: /webroot/hade_doc/dist/assets/img" map[] |
| 101 | +[Info] 2022-12-14T20:37:46+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/img/search.83621669.svg to remote file: /webroot/hade_doc/dist/assets/img/search.83621669.svg finish" map[] |
| 102 | +[Info] 2022-12-14T20:37:46+08:00 "mkdir: /webroot/hade_doc/dist/assets/js" map[] |
| 103 | +[Info] 2022-12-14T20:37:47+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/10.95277eaa.js to remote file: /webroot/hade_doc/dist/assets/js/10.95277eaa.js finish" map[] |
| 104 | +[Info] 2022-12-14T20:37:47+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/11.ce56aea9.js to remote file: /webroot/hade_doc/dist/assets/js/11.ce56aea9.js finish" map[] |
| 105 | +[Info] 2022-12-14T20:37:47+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/12.1ab2b8bd.js to remote file: /webroot/hade_doc/dist/assets/js/12.1ab2b8bd.js finish" map[] |
| 106 | +[Info] 2022-12-14T20:37:47+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/13.2165b0a4.js to remote file: /webroot/hade_doc/dist/assets/js/13.2165b0a4.js finish" map[] |
| 107 | +[Info] 2022-12-14T20:37:47+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/14.62f5324b.js to remote file: /webroot/hade_doc/dist/assets/js/14.62f5324b.js finish" map[] |
| 108 | +[Info] 2022-12-14T20:37:47+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/15.af7aad9d.js to remote file: /webroot/hade_doc/dist/assets/js/15.af7aad9d.js finish" map[] |
| 109 | +[Info] 2022-12-14T20:37:47+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/16.e3cf5518.js to remote file: /webroot/hade_doc/dist/assets/js/16.e3cf5518.js finish" map[] |
| 110 | +[Info] 2022-12-14T20:37:48+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/17.2fe064fe.js to remote file: /webroot/hade_doc/dist/assets/js/17.2fe064fe.js finish" map[] |
| 111 | +[Info] 2022-12-14T20:37:48+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/18.0f1b26f9.js to remote file: /webroot/hade_doc/dist/assets/js/18.0f1b26f9.js finish" map[] |
| 112 | +[Info] 2022-12-14T20:37:48+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/19.90ea3d02.js to remote file: /webroot/hade_doc/dist/assets/js/19.90ea3d02.js finish" map[] |
| 113 | +[Info] 2022-12-14T20:37:48+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/2.be84f03d.js to remote file: /webroot/hade_doc/dist/assets/js/2.be84f03d.js finish" map[] |
| 114 | +[Info] 2022-12-14T20:37:48+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/20.5d7d2f00.js to remote file: /webroot/hade_doc/dist/assets/js/20.5d7d2f00.js finish" map[] |
| 115 | +[Info] 2022-12-14T20:37:48+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/21.c5cbe54a.js to remote file: /webroot/hade_doc/dist/assets/js/21.c5cbe54a.js finish" map[] |
| 116 | +[Info] 2022-12-14T20:37:49+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/22.0150d521.js to remote file: /webroot/hade_doc/dist/assets/js/22.0150d521.js finish" map[] |
| 117 | +[Info] 2022-12-14T20:37:49+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/23.58cff40f.js to remote file: /webroot/hade_doc/dist/assets/js/23.58cff40f.js finish" map[] |
| 118 | +[Info] 2022-12-14T20:37:49+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/24.562746b5.js to remote file: /webroot/hade_doc/dist/assets/js/24.562746b5.js finish" map[] |
| 119 | +[Info] 2022-12-14T20:37:49+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/25.7d55bc4b.js to remote file: /webroot/hade_doc/dist/assets/js/25.7d55bc4b.js finish" map[] |
| 120 | +[Info] 2022-12-14T20:37:49+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/26.fab7722a.js to remote file: /webroot/hade_doc/dist/assets/js/26.fab7722a.js finish" map[] |
| 121 | +[Info] 2022-12-14T20:37:49+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/27.ae482208.js to remote file: /webroot/hade_doc/dist/assets/js/27.ae482208.js finish" map[] |
| 122 | +[Info] 2022-12-14T20:37:49+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/28.5f461182.js to remote file: /webroot/hade_doc/dist/assets/js/28.5f461182.js finish" map[] |
| 123 | +[Info] 2022-12-14T20:37:50+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/29.035f9ced.js to remote file: /webroot/hade_doc/dist/assets/js/29.035f9ced.js finish" map[] |
| 124 | +[Info] 2022-12-14T20:37:50+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/3.928ce6a6.js to remote file: /webroot/hade_doc/dist/assets/js/3.928ce6a6.js finish" map[] |
| 125 | +[Info] 2022-12-14T20:37:50+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/4.f177e320.js to remote file: /webroot/hade_doc/dist/assets/js/4.f177e320.js finish" map[] |
| 126 | +[Info] 2022-12-14T20:37:50+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/5.529ffd1a.js to remote file: /webroot/hade_doc/dist/assets/js/5.529ffd1a.js finish" map[] |
| 127 | +[Info] 2022-12-14T20:37:50+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/6.a92ad380.js to remote file: /webroot/hade_doc/dist/assets/js/6.a92ad380.js finish" map[] |
| 128 | +[Info] 2022-12-14T20:37:50+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/7.47c47502.js to remote file: /webroot/hade_doc/dist/assets/js/7.47c47502.js finish" map[] |
| 129 | +[Info] 2022-12-14T20:37:50+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/8.8ae34656.js to remote file: /webroot/hade_doc/dist/assets/js/8.8ae34656.js finish" map[] |
| 130 | +[Info] 2022-12-14T20:37:51+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/9.cc18f0e9.js to remote file: /webroot/hade_doc/dist/assets/js/9.cc18f0e9.js finish" map[] |
| 131 | +[Info] 2022-12-14T20:37:51+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/assets/js/app.5ce4c1ae.js to remote file: /webroot/hade_doc/dist/assets/js/app.5ce4c1ae.js finish" map[] |
| 132 | +[Info] 2022-12-14T20:37:51+08:00 "mkdir: /webroot/hade_doc/dist/guide" map[] |
| 133 | +[Info] 2022-12-14T20:37:51+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/app.html to remote file: /webroot/hade_doc/dist/guide/app.html finish" map[] |
| 134 | +[Info] 2022-12-14T20:37:51+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/build.html to remote file: /webroot/hade_doc/dist/guide/build.html finish" map[] |
| 135 | +[Info] 2022-12-14T20:37:51+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/command.html to remote file: /webroot/hade_doc/dist/guide/command.html finish" map[] |
| 136 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/cron.html to remote file: /webroot/hade_doc/dist/guide/cron.html finish" map[] |
| 137 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/dev.html to remote file: /webroot/hade_doc/dist/guide/dev.html finish" map[] |
| 138 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/env.html to remote file: /webroot/hade_doc/dist/guide/env.html finish" map[] |
| 139 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/index.html to remote file: /webroot/hade_doc/dist/guide/index.html finish" map[] |
| 140 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/install.html to remote file: /webroot/hade_doc/dist/guide/install.html finish" map[] |
| 141 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/introduce.html to remote file: /webroot/hade_doc/dist/guide/introduce.html finish" map[] |
| 142 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/middleware.html to remote file: /webroot/hade_doc/dist/guide/middleware.html finish" map[] |
| 143 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/model.html to remote file: /webroot/hade_doc/dist/guide/model.html finish" map[] |
| 144 | +[Info] 2022-12-14T20:37:52+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/provider.html to remote file: /webroot/hade_doc/dist/guide/provider.html finish" map[] |
| 145 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/structure.html to remote file: /webroot/hade_doc/dist/guide/structure.html finish" map[] |
| 146 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/swagger.html to remote file: /webroot/hade_doc/dist/guide/swagger.html finish" map[] |
| 147 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/todo.html to remote file: /webroot/hade_doc/dist/guide/todo.html finish" map[] |
| 148 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/util.html to remote file: /webroot/hade_doc/dist/guide/util.html finish" map[] |
| 149 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/guide/version.html to remote file: /webroot/hade_doc/dist/guide/version.html finish" map[] |
| 150 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/index.html to remote file: /webroot/hade_doc/dist/index.html finish" map[] |
| 151 | +[Info] 2022-12-14T20:37:53+08:00 "mkdir: /webroot/hade_doc/dist/provider" map[] |
| 152 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/provider/app.html to remote file: /webroot/hade_doc/dist/provider/app.html finish" map[] |
| 153 | +[Info] 2022-12-14T20:37:53+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/provider/config.html to remote file: /webroot/hade_doc/dist/provider/config.html finish" map[] |
| 154 | +[Info] 2022-12-14T20:37:54+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/provider/env.html to remote file: /webroot/hade_doc/dist/provider/env.html finish" map[] |
| 155 | +[Info] 2022-12-14T20:37:54+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/provider/index.html to remote file: /webroot/hade_doc/dist/provider/index.html finish" map[] |
| 156 | +[Info] 2022-12-14T20:37:54+08:00 "upload local file: /Users/jianfengye/Documents/workspace/gohade/hade/deploy/20221214203745/dist/provider/log.html to remote file: /webroot/hade_doc/dist/provider/log.html finish" map[] |
| 157 | +[Info] 2022-12-14T20:37:54+08:00 "upload folder success" map[] |
| 158 | +[Info] 2022-12-14T20:37:54+08:00 "execute post action start" map[cmd:pwd connection:ssh.web-01] |
| 159 | +[Info] 2022-12-14T20:37:54+08:00 "execute post action finish" map[cmd:pwd connection:ssh.web-01 out:/home/yejianfeng] |
| 160 | +``` |
| 161 | + |
| 162 | +### 部署后端 |
| 163 | + |
| 164 | +命令 `./hade deploy backend` |
| 165 | +会自动编译hade二进制文件,然后上传到服务器上。 |
| 166 | +如果你的post_action 设置的是重启远端服务器进程,那么实际上就是一个完整的cd行为了。 |
| 167 | + |
| 168 | +### 前后端一起部署 |
| 169 | + |
| 170 | +命令 `./hade deploy all` |
| 171 | + |
| 172 | +### 部署回滚 |
| 173 | + |
| 174 | +每次部署执行,都会在本地的deploy目录下创建一个目录,目录名为当前时间戳,比如`20221214203745`。 |
| 175 | + |
| 176 | +如果你想回滚到上一次部署的版本,可以执行命令 `./hade deploy rollback 20221214203745 backend`。 |
| 177 | + |
| 178 | +实际上做的事情就是将deploy目录下的时间戳对应的文件再进行一次发布。 |
| 179 | + |
0 commit comments