Loading... ## 为什么使用 1. 程序在发布时步骤繁琐,除了进行编码的开发和测试还要登录运行的主机进行程序启停文件迁移等步骤 2. 人工干预发布流程可能会在文件迁移过程中存在遗忘部分文件等小问题 3. 自动化程度很低,不能快速交付测试等 4. 出现版本发布后不能快速回滚到历史版本的问题 ## 使用自动部署的好处 1. 可以规范自动发布部署流程 2. 可以快速回滚到某个历史版本 3. 可以自动监听代码库变动,自动构建和部署 4. 无需人工参与部署流程 ## 简单开始 ### Gitee - https://gitee.com/ 1. 一个正常使用的Gitee账号 2. 创建一个参与自动部署的仓库 ![SourceRep](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/39f27a97155c470d9c2a66e2d1d9ec27.png) 3. 将仓库克隆到本地 ### 代码 1. 代码可以正常推送到上述仓库 2. 新的SLN ![NewSln](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/5483518fa1814637b545dfd2cf3fb659.png) 3. 新的忽略文件 ![Gitignore](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/b4759a36c7d44ae3aa955c0c73adfe4b.png) 4. 创建新项目 - NET6.0 ![NewProject](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/71b2bea91e2145fc846438c5da9704b1.png) 5. 新项目启动 ![WebAppRun](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/61fa93d571a04bdb8ce659f3755a35ec.png) 6. 代码推送 ![CodePush](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/43f6c695ddee4934b647e74ebd9bdbff.png) ### 云效 - https://flow.aliyun.com/ > 首次登录,填写必要信息等(不再概述) 1. 全局设置 ![GlobalSet](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/239e7c3fb86e465fa9de61b25325e44d.png) 2. 绑定Gitee ![GiteeInformation](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/7c7d136dca064b42973d41709f576540.png) > 点击新建后会打开新标签页打开Gitee登录界面,输入账号密码进行绑定 ![GiteeBinding](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/49d2442376354298b0715a6ba66a098b.png) 3. 绑定服务器 > 服务器需提前安装ASP.NET Core 程序的运行环境 (NET6.0) ![ServerBinding](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/e5a1b5330157450a956723fff492c63e.png) ![MyServer](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/b3ebf37309674851be5f967183b5669f.png) ```shell bash <(curl -L -k http://agent-install-default.oss-cn-hangzhou.aliyuncs.com/production-install.sh) 'http://devops.aliyuncs.com/api/getSnByRegion?regionId=d219de20-0da8-4e0b-90f8-25f611ef04dd&sign=QjgbulOStz4gD1xiu2ECZA%3D%3D&aliyunRegion=default×tamp=1697775102452&scope=flow' http://agent-install-default.oss-cn-hangzhou.aliyuncs.com/production-agent.tgz http://devops.aliyuncs.com/api/verifySnAndRegionId?sign=AFWnJ982NxRAb%2BwMSf7V2Q%3D%3D d219de20-0da8-4e0b-90f8-25f611ef04dd 1697775102452 default ``` > 部署脚本需要Python2环境,服务器为Python3 ![NeedPython2](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/4a950911514e40ae80282a574db7cf61.png) 下载脚本后修改部分代码 ```shell wget http://agent-install-default.oss-cn-hangzhou.aliyuncs.com/production-install.sh ``` ![ReplacePython3_1](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/e5726f597fa44877a4971ca77060ebd6.png) > 将2.7修改为3 ![ReplacePython3_2](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/21aa365b9b0b4f3cbdc0cc553118dbb7.png) 执行(替换掉下载过程,直接复制第二行到最后的命令) ![AddAgent](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/187725e79ef74d3a9bc8f3eb9d783777.png) ```shell bash production-install.sh 'http://devops.aliyuncs.com/api/getSnByRegion?regionId=d219de20-0da8-4e0b-90f8-25f611ef04dd&sign=QjgbulOStz4gD1xiu2ECZA%3D%3D&aliyunRegion=default×tamp=1697775102452&scope=flow' http://agent-install-default.oss-cn-hangzhou.aliyuncs.com/production-agent.tgz http://devops.aliyuncs.com/api/verifySnAndRegionId?sign=AFWnJ982NxRAb%2BwMSf7V2Q%3D%3D d219de20-0da8-4e0b-90f8-25f611ef04dd 1697775102452 default ``` ![SelectServer](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/2ff08296b6ae4c18b22cbb6e5822bfe4.png) ![SaveServer](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/3cc303c3baf240709db492a490eb3a0d.png) 4. 新建流水线 ![NewFlow](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/d476a44635884750841d00142a1272b5.png) ![CreateFlow](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/dfa1b306e15441e998347e14c35d8c64.png) 1. 配置仓库 ![ConfigGitSource](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/2b6056c2311d4129a652285fc5a7f837.png) 2. 制品构建 > 测试构建过程是否正常,建议在构建前手动在服务器上执行构建过程,排除普通问题 ![.NETCoreBuild](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/4aae07e3a47c433991d0bfbc862b2a66.png) 编译制品 ```shell dotnet publish -c Release -o out ``` 3. 主键部署 > 此步骤主要测试文件路径及命令执行情况 ![RunApplicationWeb](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/176e26e423c1442292fa4546a13b3996.png) ```shell mkdir -p /home/auto_run tar zxvf /home/admin/app/package.tgz -C /home/auto_run ``` 4. 保存并运行 ![SaveAndRun](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/53512d90d4774d2b861bb6e807e16471.png) 5. 测试流程是否正常 > 根据异常自行解决问题 ![RunLog](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/fbc69de16398426aad439f0c760ae40f.png) ## 进阶使用 ### 程序中加入健康检查 ![HealthCheck](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/8b660f00d9d145daa94bf5dc04ede895.png) ### 加入启动脚本 > 注意事项:一定要明确文件路径,如果出现部署失败问题大概率由此处引起 ![deploy](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/b39728f40c6a48d2b4d1b6d8bf637bfe.png) ```shell #!/bin/bash # 修改APP_NAME为云效上的应用名 APP_NAME=Auto_Publish_WebApplication PROG_NAME=$0 ACTION=$1 APP_START_TIMEOUT=20 # 等待应用启动的时间 APP_PORT=5569 # 应用端口 HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT}/health # 应用健康检查URL HEALTH_CHECK_FILE_DIR=/home/auto/admin/status # 脚本会在这个目录下生成nginx-status文件 APP_HOME=/home/auto_run # 从package.tgz中解压出来的dll放到这个目录下 DLL_NAME=${APP_HOME}/${APP_NAME}.dll # dll的名字 DLL_OUT=${APP_HOME}/logs/start.log #应用的启动日志 # 创建出相关目录 mkdir -p ${HEALTH_CHECK_FILE_DIR} mkdir -p ${APP_HOME} mkdir -p ${APP_HOME}/logs usage() { echo "Usage: $PROG_NAME {start|stop|restart}" exit 2 } health_check() { exptime=0 echo "checking ${HEALTH_CHECK_URL}" while true do status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code} ${HEALTH_CHECK_URL}` if [ "$?" != "0" ]; then echo -n -e "\rapplication not started" else echo "code is $status_code" if [ "$status_code" == "200" ];then break fi fi sleep 1 ((exptime++)) echo -e "\rWait app to pass health check: $exptime..." if [ $exptime -gt ${APP_START_TIMEOUT} ]; then echo 'app start failed' exit 1 fi done echo "check ${HEALTH_CHECK_URL} success" } start_application() { echo "starting dotnet process" # chmod +x ${DLL_NAME} # chmod +x ${APP_HOME}/appsettings.json # nohup dotnet ${DLL_NAME} Urls=http://*:${APP_PORT} > ${DLL_OUT} 2>&1 & cd ${APP_HOME} nohup dotnet ${APP_NAME}.dll --urls "http://*:${APP_PORT}" > ${DLL_OUT} 2>&1 & echo "started dotnet process" } stop_application() { checkdotnetpid=`ps -ef | grep dotnet | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'` if [[ ! $checkdotnetpid ]];then echo -e "\rno dotnet process" return fi echo "stop dotnet process" times=60 for e in $(seq 60) do sleep 1 COSTTIME=$(($times - $e )) checkdotnetpid=`ps -ef | grep dotnet | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'` if [[ $checkdotnetpid ]];then kill -9 $checkdotnetpid echo -e "\r -- stopping dotnet lasts `expr $COSTTIME` seconds." else echo -e "\rdotnet process has exited" break; fi done echo "" } start() { start_application health_check } stop() { stop_application } case "$ACTION" in start) start ;; stop) stop ;; restart) stop start ;; *) usage ;; esac ``` ### 更新仓库 ![UpdateSource](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/048f47d250864329a24f619ecc019dae.png) ### 添加Webhook > 代码提交时自动触发流水线进行自动构建部署 ![WebHook_1](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/efc5878140fe4f39a5533eb51b6fa976.png) 将复制的链接粘贴到仓库配置中 ![WebHook_2](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/da08b104cbc34cdba3a0874dce2d01b1.png) ![WebHook_3](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/55b468345dde46729ef0801626d28485.png) ![WebHook_4](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/55dd8046f59e4270b1d1bdfeb7e9132f.png) ### 修改主机部署中部署脚本 ![AutoDeploy](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/706fed10a62147db9392cdb7474e09ad.png) ```shell mkdir -p /home/auto_run tar zxvf /home/admin/app/package.tgz -C /home/auto_run bash /home/auto_run/deploy.sh restart ``` ### 钉钉通知构建结果 ![DingTalkBot](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/b89de1cd4d0b413da7689d4b0f086835.png) ![DingTalkBotMessage](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/44f1adbab61a4e12a85f911818f36191.png) ### 代码提交测试自动部署 ![Compare_1](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/9bf7d1b960f8405dbd5482c6f0300bf7.png) ![Compare_2](https://hwcloud.sdqps.top/ShareImages/PicGo/20231020/859d7fc3558b4145ba65177658f0d90a.png) - 参考资料https://mp.weixin.qq.com/s/wzfjaOGMaX8\_f5fVoaXXSQ 最后修改:2023 年 10 月 20 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 1 如果觉得我的文章对你有用,请随意赞赏