44 push :
55 tags :
66 - " v*.*.*"
7- workflow_dispatch :
7+ workflow_dispatch : {}
88
99jobs :
10- build :
11- name : build ${{ matrix.file }} on ${{ matrix.os }}
10+ # 创建 Github Releases
11+ create-release :
12+ if : startsWith(github.ref, 'refs/tags/')
13+ runs-on : ubuntu-latest
14+ steps :
15+ - uses : actions/checkout@master
16+ # https://github.com/taiki-e/create-gh-release-action
17+ - uses : taiki-e/create-gh-release-action@v1
18+ with :
19+ # (optional) Path to changelog.
20+ # changelog: CHANGELOG.md
21+ # (required) GitHub token for creating GitHub Releases.
22+ token : ${{ secrets.GITHUB_TOKEN }}
23+
24+ # 自动构建跨平台 Rust 二进制文件并上传到 GitHub Release
25+ upload-assets :
26+ needs : create-release
27+ runs-on : ${{ matrix.os }}
1228 strategy :
1329 fail-fast : false
1430 matrix :
15- os :
16- - windows-latest
17- - ubuntu-latest # target: x86_64-unknown-linux-musl
18- - macos-latest
19- file :
20- - ${{ github.event.repository.name }}
21- runs-on : ${{ matrix.os }}
22- defaults :
23- run :
24- shell : bash
25- env :
26- # 不同构建目标对应的 release 目录
27- TARGET : ${{ matrix.os == 'ubuntu-latest' && 'target/x86_64-unknown-linux-musl/release' || 'target/release' }}
28- # 构建文件名, Windows 平台有 .exe 后缀
29- NAME : ${{ format('{0}{1}', matrix.file, startsWith(matrix.os, 'windows') && '.exe' || '') }}
30- # 上传制品文件名
31- ARTIFACT_NAME : Binary-${{ matrix.file }}-${{ matrix.os }}
31+ include :
32+ # 非本机三元组, 由 corss 通过容器提供交叉编译支持: https://github.com/cross-rs/cross?#supported-targets
33+ # cross 具有与 Cargo 完全相同的 CLI, 但依赖于 Docker 或 Podman
34+ - target : x86_64-unknown-linux-musl
35+ os : ubuntu-latest
36+ - target : aarch64-unknown-linux-musl
37+ os : ubuntu-latest
38+ # 本机三元组, cargo 直接编译
39+ - target : aarch64-apple-darwin
40+ os : macos-latest
41+ - target : x86_64-pc-windows-msvc
42+ os : windows-latest
3243 steps :
3344 - uses : actions/checkout@master
3445 with :
3546 submodules : recursive # 递归检出git子模块(submodules)
3647
37- - name : Cache
38- id : cache
39- uses : actions/cache@v3
48+ # 安装交叉编译工具链: https://github.com/taiki-e/setup-cross-toolchain-action
49+ # 如果安装 target 对应的工具链, 则下一步不会再安装并调用 cross 创建容器来编译, 可大幅缩短构建时间
50+ - name : Install cross-compilation tools
51+ continue-on-error : true
52+ uses : taiki-e/setup-cross-toolchain-action@v1
4053 with :
41- path : |
42- ~/.cargo
43- ~/.rustup
44- ./target
45- key : ${{ runner.os }}-${{ matrix.file }}-cache
46-
47- - name : musl-tools
48- if : matrix.os == 'ubuntu-latest'
49- run : |
50- sudo apt install -y musl-tools
54+ target : ${{ matrix.target }}
5155
52- - name : Rustup
53- if : steps.cache.outputs.cache-hit != 'true' && matrix.os == 'ubuntu-latest'
54- run : |
55- rustup target add x86_64-unknown-linux-musl
56-
57- - name : Build
58- env :
59- CARGO_TERM_COLOR : always
60- run : |
61- cargo build --release --verbose \
62- ${{ matrix.os == 'ubuntu-latest' && '--target x86_64-unknown-linux-musl' || '--' }}
63-
64- - name : Naming ${{ env.NAMING }}
65- id : naming
66- env :
67- NAMING : >-
68- ${{
69- format('{0}-{1}-{2}-{3}{4}',
70- matrix.file, github.ref_name, runner.os, runner.arch,
71- startsWith(matrix.os, 'windows') && '.exe' || ''
72- )
73- }}
74- run : |
75- export NAMING="$( echo ${{ env.NAMING }} | tr [:upper:] [:lower:] )"
76- echo "FILEPATH=$TARGET/$NAMING" >> $GITHUB_OUTPUT
77-
78- - name : Rename file
79- run : |
80- mv "$TARGET/$NAME" ${{ steps.naming.outputs.FILEPATH }}
81-
82- - name : Upload
83- uses : actions/upload-artifact@master
84- id : artifact-upload-step
85- with :
86- name : ${{ env.ARTIFACT_NAME }}
87- path : ${{ steps.naming.outputs.FILEPATH }}
88-
89- - name : GH Release on ${{ matrix.os }}
90- uses : softprops/action-gh-release@v2.0.2
91- if : startsWith(github.ref, 'refs/tags/')
56+ # 构建并发布 Rust 程序: https://github.com/taiki-e/upload-rust-binary-action
57+ - uses : taiki-e/upload-rust-binary-action@v1
9258 with :
59+ # (required) Comma-separated list of binary names (non-extension portion of filename) to build and upload.
60+ bin : ${{ github.event.repository.name }}
61+ # (optional) Target triple, default is host triple.
62+ target : ${{ matrix.target }}
63+ # (optional) Tool to build binaries (cargo, cross, or cargo-zigbuild)
64+ # build-tool: cargo-zigbuild # 使用 zig 作为链接器编译 Cargo 项目, 以便于交叉编译, 仅 Linux 平台支持性较好
65+ # (optional) Archive name (non-extension portion of filename) to be uploaded.
66+ # When multiple binary names are specified, default archive name or $bin variable cannot be used.
67+ archive : ${{ github.event.repository.name }}-$tag-$target
68+ # (optional) On which platform to distribute the `.tar.gz` file.
69+ tar : unix
70+ # (optional) On which platform to distribute the `.zip` file.
71+ zip : windows
72+ # (required) GitHub token for uploading assets to GitHub Releases.
9373 token : ${{ secrets.GITHUB_TOKEN }}
94- files : ${{ steps.naming.outputs.FILEPATH }}
95-
96- - name : Done
97- if : startsWith(github.ref, 'refs/tags/')
98- run : |
99- echo "### [${{ github.ref_name }}](https://github.com/$GITHUB_REPOSITORY/releases/tag/${{ github.ref_name }}) Released! :rocket:" \
100- >> $GITHUB_STEP_SUMMARY
10174
10275permissions :
10376 contents : write
0 commit comments