Rust 项目使用 Github Action 自动构建 跨平台二进制并上传

581 阅读3分钟

前言:

最近想要把 Nping 的构建打包给自动化掉, Nping 作为一个Rust 编写命令行二进制工具本身就支持跨平台, 再加上开源一周 已经收货了 160+ star。有人提 issue 需要帮忙提供 windows, 或者 linux arm 等不同操作系统不同架构的二进制, 最早我是在我的 mac 电脑上本地构建好这些二进制打包并上传的,为此我还写了一个本地打包的 bash 脚本用于帮我自动化。 这样做的坏处是所有的构建基本上都需要在我的本地特定的电脑构建并手工打包上传,每次发版本非常的麻烦。于是想到是否可以用 Github Action 自动打包并 Release。

🏎 Nping 介绍:

Nping 顾名思义, 牛批Ping , 是一个用 Rust 开发的实时可视化终端 Ping 命令。具有多地址,实时折线图展示,实时展示延迟/抖动率/最大/最小延迟等常见指标。

开源地址:

github.com/hanshuaikan…

可以参考 Nping 来配置构建你的 Github Action。

目标:

我们的目标主要提供 Macos, Windows, Linux 这三种不同平台的二进制。每次打 tag 可以自动 build 并 release。

  • Windows x86_64
  • Macos x86_64
  • Macos arm64
  • Linux x86_64
  • Linux arm64

打包过程中主要会用到这些 Target:

# linux
aarch64-unknown-linux-gnu
x86_64-unknown-linux-gnu

# macos
x86_64-apple-darwin
universal-apple-darwin

# windwos
x86_64-pc-windows-msvc

明确了主要的编译平台之后漫长的踩坑之路就开始了

🧨 踩坑一: Rust 语言圣经

最早参考的是 Rust 语言圣经提供的教程,上面提高的 yml 已经是几年前的了, 尝试之后发现 windows 和 mac 的编译没有什么问题,但是 linux 死活不会成功,报错:

error: linking with cc failed: exit status: 1

奈何怎么也找不到原因,也不知道这些 action 背后是如何工作的,遂放弃。

🧨 踩坑二:

于是继续 Google 搜索,搜到 [rust-build.action](https://github.com/rust-build/rust-build.action) 这个开源的 Action

试了下,诶,发现可以,但是可惜的是这个库只支持有限的几个 Target, 对于 Macos aarch64 和 Linux aarch64 并不支持,遂再次放弃。

🎉 终于成功了

在失败了十多次之后,已经开始想要摆烂了,这个时候我开始在 Github 乱翻, 终于让我找到了这个 可用的 Action。

github.com/taiki-e/upl…

能满足我们的需求, 并且支持非常多的 target, 于是打了个 tag 试了一下,一次成功, 非常完美。

附上完整的 Yaml

你需要把 yaml 中的 nping 改成你自己的 二进制名称。

当然这篇文章发布之后意味着总有一天它也会过期, 未来可以参考 nping 的 github workflow 实现,我会保证它永远是可运行的。

地址:github.com/hanshuaikan…

name: Release  
  
on:  
  push:  
    tags:  
      - 'v*.*.*'  
  
permissions:  
  contents: write  
  
jobs:  
  create-release:  
    runs-on: ubuntu-latest  
    steps:  
      - uses: actions/checkout@v4  
      - uses: taiki-e/create-gh-release-action@v1  
        with:  
          # (required) GitHub token for creating GitHub Releases.  
          token: ${{ secrets.GITHUB_TOKEN }}  
  
  upload-assets:  
    needs: create-release  
    strategy:  
      matrix:  
        include:  
          - target: aarch64-unknown-linux-gnu  
            os: ubuntu-latest  
          - target: aarch64-apple-darwin  
            os: macos-latest  
          - target: x86_64-unknown-linux-gnu  
            os: ubuntu-latest  
          - target: x86_64-apple-darwin  
            os: macos-latest  
          - target: universal-apple-darwin  
            os: macos-latest  
          - target: x86_64-pc-windows-msvc  
            os: windows-latest  
    runs-on: ${{ matrix.os }}  
    steps:  
      - uses: actions/checkout@v4  
      - uses: taiki-e/upload-rust-binary-action@v1  
        with:  
          # (required) Comma-separated list of binary names (non-extension portion of filename) to build and upload.  
          # Note that glob pattern is not supported yet.          bin: nping  
          # (optional) Target triple, default is host triple.  
          target: ${{ matrix.target }}  
          # (required) GitHub token for uploading assets to GitHub Releases.  
          token: ${{ secrets.GITHUB_TOKEN }}