沙滩星空的博客沙滩星空的博客

使用GitHub Actions自动构建和发布资源到Release

  • 推送标签时触发(git push --tags):
on:
  push:
    tags:
      - "v*"
  • 新建Release后触发
on:
  release:
    types: [created] # 表示在创建新的 Release 时触发
    # - created
jobs:
  build-go-binary:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        goos: [linux, windows, darwin] # 需要打包的系统
        goarch: [amd64, arm64] # 需要打包的架构
        exclude: # 排除某些平台和架构
          - goarch: arm64
            goos: windows
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - goarch: amd64
            goos: linux
          - goarch: amd64
            goos: darwin

例子:

name: CreateRelease

on:
  release:
    types:
      - prereleased
      - released
  push:
    tags:
      - "v*"

jobs:
  create_release:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        if: github.event_name == 'push'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref_name }}
          body: Please refer to [CHANGELOG.md](https://github.com/iotames/v2raypool/blob/master/CHANGELOG.md) for details.
          draft: false
          prerelease: false

  build:
    strategy:
      matrix:
        goos: [linux, darwin, windows]
        goarch: [amd64, 386]
        exclude:
          - goarch: 386
            goos: darwin
    
    permissions: # 需要设置写权限才能自动发布
      contents: write

    runs-on: ubuntu-latest
    env:
      GOOS: ${{ matrix.goos }}
      GOARCH: ${{ matrix.goarch }}
      CGO_ENABLED: 0
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.22.x'
          
      - name: Install dependencies
        run: |
          go env -w GO111MODULE=on
          go env -w GOPROXY=https://goproxy.cn,direct
          go mod tidy
      
      - name: Get File Name
        id: get_filename
        run: |
          export _NAME=v2raypool-${{ matrix.goos }}-${{ matrix.goarch }}
          echo "GOOS: $GOOS, GOARCH: $GOARCH, RELEASE_NAME: $_NAME"
          echo "ASSET_NAME=$_NAME" >> $GITHUB_OUTPUT
          echo "ASSET_NAME=$_NAME" >> $GITHUB_ENV
        
      - name: Build
        run: |
          mkdir -p build_assets
          go build -v -o build_assets/v2raypool -trimpath -ldflags "-s -w -buildid=" ./main
      
      - name: Rename Windows File
        if: matrix.goos == 'windows'
        run: |
          cd ./build_assets || exit 1
          mv v2raypool v2raypool.exe

      - name: Download geo files
        run: |
          wget -O main/bin/geoip.dat "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
          wget -O main/bin/geoip-only-cn-private.dat "https://raw.githubusercontent.com/v2fly/geoip/release/geoip-only-cn-private.dat"
          wget -O main/bin/geosite.dat "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"

      - name: Prepare package
        run: |
          cp -rv ./main/resource ./build_assets/
          cp -rv ./main/bin ./build_assets/
          touch ./build_assets/subscribe_data.txt

      - name: Prepare package for Linux
        if: matrix.goos == 'linux'
        run: cp -rv ./release/config/systemd ./build_assets/
          
      - name: Package
        run: tar zcvf ${{ steps.get_filename.outputs.ASSET_NAME }}.tar.gz ./build_assets

        # 上传附件
      - name: Upload file to Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.get_filename.outputs.ASSET_NAME }}.tar.gz
          path: ${{ steps.get_filename.outputs.ASSET_NAME }}.tar.gz    

      - name: Upload files to GitHub release
        uses: svenstaro/upload-release-action@v2
        if: github.event_name == 'release'
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file_glob: true
          file: ./${{ steps.get_filename.outputs.ASSET_NAME }}.tar.gz
          tag: ${{ github.ref }}
          overwrite: true

跨平台编译

如果是非 CGO 项目,可以使用Go内置的 交叉编译,但如果是 CGO 项目,上述的交叉编译大概率会失败(相关问题:go-libsass), 所以我们需要基于不同的平台来进行编译,幸运的是,Github Actions 对此支持的非常好。

name: Build and Test
on:
  push:
    branches:
      - master
  pull_request:
jobs:
  lint:
    strategy:
      matrix:
        platform: [ubuntu-latest, windows-latest, macos-latest]
    runs-on: ${{ matrix.platform }}
    steps:
    - name: hello world
      run: |
        echo "Hello World"

Artifact

GitHub Actions 的 jobs 之间是分布式的,相互独立的。因此上一个 job 中生成的文件不能直接传到下一个 job 中,这其中需要用到一个名为Artifact的东西。你可以把它理解成一块共享的公共空间,第一个 job 把构建好的中间文件上传到Artifact上,下一个 job 再从Artifact中下载。这里使用 GitHub 官方的 Action actions/upload-artifact

本地调试工具: Act

Act源码仓库

使用 Act 本地运行 GitHub Actions

安装Act

  1. 直接下载二进制文件:https://github.com/nektos/act/releases
  2. go install 源码安装
git clone https://github.com/nektos/act.git
cd act
go install
act --version

# act version 0.2.68

使用Act

输入 act --help 能看帮助内容

  • -n:Dry run,用于校验语法,查看基本运行逻辑;
  • -j:直接指定触发 Job;
  • g:图形化的方式来展示 Action 的流程;
  • -e:可以编写一个 JSON 文件来描述 Github 事件,例如一个 PR:

使用Github Actions自动打包Go项目 https://honmaple.me/articles/2023/03/%E4%BD%BF%E7%94%A8Github%20Actions%E8%87%AA%E5%8A%A8%E6%89%93%E5%8C%85Go%E9%A1%B9%E7%9B%AE.html
GitHub Action 自动构建 并release https://cloud.tencent.com/developer/article/1970730
用 Github Action 自动发布二进制包 https://blog.fleeto.us/post/auto-build-with-github-action/
GitHub Action - 版本发布时获取 tag 做为版本号 http://www.javashuo.com/article/p-kwvtubgo-ny.html
未经允许不得转载:沙滩星空的博客 » 使用GitHub Actions自动构建和发布资源到Release

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址