Go 自动拉取 vitepress 文档git仓库并编译部署后加载到nginx

Go 自动拉取 vitepress 文档仓库并编译部署后加载到nginx

基于Go 开发自动拉取 git 的 vitepress 文档并编译部署的简单脚本。

  • 添加了 git 用户名密码。
  • nginx 已配置为前提。

需要使用管理员权限运行以重新加载到nginx。

其中重新加载到 nginx 的代码

cmd2 := exec.Command("/bin/sh", "-c", "nginx -s reload")

为 Linux 版本,其它操作系统需要修改为对应的脚本。

参数

  • 参数1:vitepress 文档git仓库路径
  • 参数2:部署目标路径
  • 参数3:编译结果相对仓库路径(第一个字符必须为路径分割符)
  • 参数4:git 用户名
  • 参数5:git 用户的密码
  • 参数6:可选项。 值为 0 时执行定时任务,每天0点执行。 其它情况为非定时任务,立即执行。
  • Linux 执行例

    非定时任务:

    sudo ./main /home/user/hello_vitepress /home/user/hello_doc /docs/.vitepress/dist username password
    

    定时任务:

    sudo ./main /home/user/hello_vitepress /home/user/hello_doc /docs/.vitepress/dist username password 0
    

    完整代码

    package main
    
    import (
        "fmt"
        "github.com/go-git/go-git/v5"
        "github.com/go-git/go-git/v5/plumbing/transport/http"
        cp "github.com/otiai10/copy"
        "github.com/robfig/cron/v3"
        "log"
        "os"
        "os/exec"
    )
    
    func main() {
        s := os.Args
    
        pathRepo := s[1]   // F:\hello_vitepress
        pathDeploy := s[2] // E:\hello_doc
        pathBuild := s[3]  // `\docs.vitepress\dist`
        gitUserName := s[4]
    
        log.Println(fmt.Sprintf("仓库路径:%s", pathRepo))
        log.Println(fmt.Sprintf("部署路径:%s", pathDeploy))
        log.Println(fmt.Sprintf("编译结果相对仓库路径:%s", pathBuild))
        log.Println(fmt.Sprintf("git用户名:%s", gitUserName))
    
        // === 定时任务 ===
        if len(s) == 7 && s[6] == "0" {
           // 创建定时任务Cron
           c := cron.New()
    
           // 定时任务周期及处理函数
           Info("=== 定时任务(每天) ===")
           c.AddFunc("@daily", PullBuildDeploy)
    
           // 启动定时任务
           c.Start()
    
           ch := make(chan string)