DevOps+Go 古城微笑少年丶 2022-02-27 11:22 237阅读 0赞 go程序的自动发布,在某课网上看到了这么个课程,看了下内容,大致意思是:用某个触发机制或者钩子,调用shell脚本,对服务器里的程序进行更新执行,这里的触发机制或者钩子可以是手动执行shell脚本,利用git的钩子。 下面是大致实现步骤: 1 简单网页程序: package main import ( "net/http" "io" ) func firstPage(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "<h1>hello,this is firstPage</h1>") } func main() { http.HandleFunc("/", firstPage) http.ListenAndServe(":8000", nil) } 2.生成在centos内执行的文件: E:\GoProgram\awsomeproject\src\webserver\webserver>set GOARCH=amd64 E:\GoProgram\awsomeproject\src\webserver\webserver>set GOOS=linux E:\GoProgram\awsomeproject\src\webserver\webserver>go install 3.写一个触发事件,并生成在centos内执行的文件:大致意思是,访问这个页面触发执行deploy.sh脚本 package main import ( "net/http" "io" "os/exec" "log" ) func reLaunch(){ cmd:=exec.Command("sh","./deploy.sh") err:=cmd.Start() if err!=nil{ log.Fatal(err) } err=cmd.Wait() } func firstPage(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "<h1>hello,this is deployPage</h1>") reLaunch() } func main() { http.HandleFunc("/", firstPage) http.ListenAndServe(":5000", nil) } 4.写deploy.sh脚本,在win平台上写的shell脚本并不能直接在centos上执行,[解决办法][Link 1]: #! /bin/sh echo "----------------------START----------------------------" kill -9 $(pgrep webserver) cd /home/newserver rm -rf webserver git clone https://github.com/sahongzhi/webserver.git cd /home/newserver/webserver/webserver chmod 777 webserver ./webserver & echo "----------------------END------------------------------" 将相关程序上传到服务器。 访问[http://111.231.73.14:8000/][http_111.231.73.14_8000]: ![20190327155141221.png][] 修改为secondPage,访问[http://111.231.73.14:5000/][http_111.231.73.14_5000]之后,再访问[http://111.231.73.14:8000/][http_111.231.73.14_8000]: ![20190327155609557.png][] 触发生效! 还有一种是用git的钩子,在settings里面有个webhook,在那里设置: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIwODY3OTgx_size_16_color_FFFFFF_t_70][] 这样在上传代码至git上时,自动更新服务器程序: 修改为thirdPage,并push到git,访问[http://111.231.73.14:8000/][http_111.231.73.14_8000]: ![20190327160224303.png][] 生效! [Link 1]: https://blog.csdn.net/qq598535550/article/details/72867885 [http_111.231.73.14_8000]: http://111.231.73.14:8000/ [20190327155141221.png]: /images/20220227/0f732952b1fd4d6a91cb2e444bcc5a4c.png [http_111.231.73.14_5000]: http://111.231.73.14:5000/ [20190327155609557.png]: /images/20220227/b4a384753da1497e9126943283907cac.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIwODY3OTgx_size_16_color_FFFFFF_t_70]: /images/20220227/07833f5e17324c3e9dd588aaae91170e.png [20190327160224303.png]: /images/20220227/8e092f1b305c47c18edcd1e08cae8d6b.png
还没有评论,来说两句吧...