You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

拆分GitHub Workflow:构建/测试与发布流程触发问题排查

问题

我刚接触GitHub Workflow,相关知识比较欠缺。我想创建两个独立的Workflow:一个在所有分支上运行,完成Go应用的构建和单元测试;另一个在推送至main分支时创建并发布可执行文件。现在推送分支时,test.yml能正常执行构建与测试,但deploy.yml始终无法触发发布,求解决办法。

现有Workflow配置

test.yml

# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Test

on:
  pull_request_target:
    types:
      - opened
      - synchronize
      - reopened
      - ready_for_review
  push:
    branches:
      - main
      - develop
      - "*"
    tags-ignore:
      - "*"

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Install required packages
        run: sudo apt-get update && sudo apt-get install -y libgl1-mesa-dev xorg-dev

      - uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: "1.20"

      - name: Build
        run: go build -v ./...

      - name: Test
        run: go test -v ./...

deploy.yml

name: Deploy

on:
  workflow_run:
    workflows: Test
    # This will eventually be limited to just main
    branches:
      - main
      - develop
      - "*"

    types: completed

jobs:
  deploy:
    runs-on: ubuntu-latest
    # `if` required because a workflow run is triggered regardless of
    # the result of the previous workflow (see the documentation page)
    # if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}

    steps:
      - name: Install required packages
        run: sudo apt-get update && sudo apt-get install -y libgl1-mesa-dev xorg-dev

      - name: Checkout code
        uses: actions/checkout@v3
        # with:
        #   ref: ${{ github.ref }}

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: 1.x

      - name: Build the Go application
        run: go build -o myapp

      - name: Build and release executables
        run: |
          GOOS=darwin GOARCH=amd64 go build -o MastoTool
          GOOS=windows GOARCH=amd64 go build -o MastoTool.exe
          GOOS=linux GOARCH=amd64 go build -o mastotool

      - name: Upload executables as artifacts
        if: always() # Upload executables regardless of build success
        uses: actions/upload-artifact@v3
        with:
          name: release-artifacts
          path: |
            MastoTool
            MastoTool.exe
            mastotool

排查结果与优化方案

经排查发现两个核心问题:

  • GitHub仅运行默认分支(main分支)上定义的Workflow,若修改在特性分支中,新的Workflow配置不会生效,导致deploy.yml无法触发。解决办法是在默认分支的Workflow文件中添加on: pull_request,允许所有拉取请求分支执行Workflow。
  • 原有的多Workflow联动方案过于复杂,容易出现触发问题,因此简化为单一Workflow,整合测试与发布逻辑:
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Test

on:
  pull_request:
  pull_request_target:
    types:
      - opened
      - synchronize
      - reopened
      - ready_for_review
  push:
    branches:
      - main
      - develop
      - "*"
    tags-ignore:
      - "*"

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Install required packages
        run: sudo apt-get update && sudo apt-get install -y libgl1-mesa-dev xorg-dev

      - uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: "1.20"

      - name: Build
        run: go build -v ./...

      - name: Test
        run: go test -v ./...

      - name: Release
        # if: startswith(github.ref,'refs/heads/master')
        # if: startswith(github.ref,'feature/deployment')
        run: |
          GOOS=darwin GOARCH=amd64 go build -o MastoTool
          GOOS=windows GOARCH=amd64 go build -o MastoTool.exe
          GOOS=linux GOARCH=amd64 go build -o mastotool
          ls -l

      - name: Upload executables as artifacts
        # if: startswith(github.ref,'refs/heads/master')
        uses: actions/upload-artifact@v3
        with:
          name: release-artifacts
          path: |
            MastoTool
            MastoTool.exe
            mastotool

内容的提问来源于stack exchange,提问作者Paul Waldo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:33:13