DevOps :Development和Operations的组合词
它是一种重视“软件开发人员(Dev)”和“IT运维技术人员(Ops)”之间沟通合作的文化、运动或惯例。透过自动化“软件交付”和“架构变更”的流程,来使得构建、测试、发布软件能够更加地快捷、频繁和可靠。 ——来自百度百科
接下来,我将把我搭建该技术框架的入门过程记录下来:
commit & push
,自动部署更新到服务器上并启动。就可以测试了。/*
* @Author: Hifun
* @Date: 2020/1/3 16:07
*/
package main
import (
"fmt"
"io"
"net/http"
"os/exec"
)
func reLaunch() {
path := `../deploy.sh` // shell脚本
cmd := exec.Command("/bin/bash", "-c", path)
err := cmd.Start() // 这里引入了"os/exec"包,来操作外部的命令和可执行文件
if err != nil {
fmt.Printf("Execute Shell:%s failed with error:%s", path, err.Error())
//return
}
fmt.Printf("Executing command: %v ...\n", path)
cmd.Wait()
}
func firstPage(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello ,this is auto deploy server!
") // 给前端展示
reLaunch() // 这里利用的简单的异步操作
}
func main() {
http.HandleFunc("/", firstPage) // 绑定方法
http.ListenAndServe(":5000", nil) // 添加监听
}
这里使用url的方式来实现自动部署:他的方便之处在于:强大的github支持一种服务Webhooks,这个在后面说。
reLaunch方法里调用了外部shell脚本,来帮我们执行操作:杀掉当前已经运行的服务进程(如果存在的话)-> 拿到最新的代码 -> 启动服务.
#!/bin/bash
kill $(pgrep webserver)
cd ~/devops/
git pull https://github.com/hifuny/devops.git
cd webserver/
chmod 777 webserver
./webserver &
/*
* @Author: Hifun
* @Date: 2020/1/3 11:48
*/
package main
import (
"io"
"net/http"
)
func firstPage(w http.ResponseWriter, r *http.Request) {
io.WriteString(w,"Hello! ,this is my firstPage!
")
w.Write([]byte("Show!"))
}
func main() {
http.HandleFunc("/", firstPage)
http.ListenAndServe(":8000", nil)
}
业务逻辑服务写好后传到服务器并启动,我们能通过url访问:
看到已经启动成功。
io.WriteString(w,"Hello! ,i am changed by deploy!
")
w.Write([]byte("Success!"))
执行commit, push
可以看到我们的脚本已经自动执行:
——来自一个程序员小学生,欢迎学习交流。
点赞,并关注我,让我们一起变得更强。