使用Github Actions部署Golang应用的最佳实践

使用Github Actions部署Golang应用的最佳实践 请帮助我 我正在尝试通过 GitHub Actions 实现 CI/CD,我想在流程中构建应用程序并将其部署到服务器上。我尝试了以下方法,但遇到了错误。

jobs:
# 此工作流包含一个名为“build”的作业
build:
  # 作业将运行的运行器类型
  runs-on: ubuntu-latest

  # 步骤代表将作为作业一部分执行的一系列任务
  steps:
    # 在 $GITHUB_WORKSPACE 下检出您的仓库,以便您的作业可以访问它
    - name: Checkout the repository
      uses: actions/checkout@v2

    - name: Set up Go 1.13
      uses: actions/setup-go@v1
      with:
        go-version: 1.13
      id: go

    - name: Deploy and rebuild on server
      uses: appleboy/ssh-action@master
      with:
        host: 167.172.187.75
        username: root
        key: ${{ secrets.serverKey }}
        port: 22
        script: |
          cd go/victorydash/ &&
          git pull origin master &&
          git status &&
          echo $PATH &&
          go build -o app main.go && # 此处出现错误
          systemctl restart goweb.service &&
          systemctl status goweb

这是错误信息: 输出:nothing to commit, working tree clean 输出:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 错误:bash: go: command not found 有办法解决这个问题吗? 谢谢


更多关于使用Github Actions部署Golang应用的最佳实践的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于使用Github Actions部署Golang应用的最佳实践的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在你的配置中,服务器上缺少 Go 环境。虽然你在 GitHub Actions 运行器中设置了 Go,但 ssh-action 是在远程服务器上执行命令,而远程服务器没有安装 Go。

以下是修正后的工作流配置:

name: Deploy Go Application

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    
    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.21'
    
    - name: Build Go binary
      run: |
        GOOS=linux GOARCH=amd64 go build -o app main.go
    
    - name: Deploy to server
      uses: appleboy/scp-action@v0.1.4
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.SSH_KEY }}
        port: 22
        source: "app"
        target: "/opt/your-app/"
    
    - name: Restart service on server
      uses: appleboy/ssh-action@v0.1.6
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.SSH_KEY }}
        port: 22
        script: |
          cd /opt/your-app/
          chmod +x app
          systemctl restart your-app.service
          systemctl status your-app.service

或者,如果你希望在服务器上构建,需要先在服务器上安装 Go:

name: Deploy Go Application

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    
    - name: Deploy and build on server
      uses: appleboy/ssh-action@v0.1.6
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.SSH_KEY }}
        port: 22
        script: |
          # 在服务器上安装 Go(如果尚未安装)
          if ! command -v go &> /dev/null; then
            wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
            rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.6.linux-amd64.tar.gz
            echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile
            source ~/.profile
          fi
          
          cd /opt/your-app/
          git pull origin main
          go version
          go build -o app main.go
          systemctl restart your-app.service
          systemctl status your-app.service

在 GitHub 仓库的 Settings → Secrets and variables → Actions 中添加以下 secrets:

  • HOST: 服务器 IP 地址
  • USERNAME: SSH 用户名
  • SSH_KEY: 服务器 SSH 私钥

第一个方案是推荐做法,它在 CI 环境中构建二进制文件,然后只将编译好的二进制文件部署到服务器,这样服务器不需要安装 Go 工具链。

回到顶部