今天在学习 sonic
搜索引擎时, 本地环境编译不方便, 甚至官方都说目前不支持 windows
, 但我着实想尝试一下, 所以便尝试使用 github action
来编译. 以下是具体部分.
仅在推送tag
且tag
前缀为 sonic_v
时才触发工作流
1
2
3
4
| on:
push:
tags:
- "sonic_v*.*.*"
|
多平台编译, 使用 matrix
来分别配置其 名称, 系统, rust
版本, rust
编译目标, 以及最后的打包文件名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| name: ${{ matrix.build }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [Linux, macOS, Windows]
include:
- build: Linux
os: ubuntu-latest
rust: nightly
target: x86_64-unknown-linux-gnu
archive-name: sonic-linux
- build: macOS
os: macos-latest
rust: nightly
target: x86_64-apple-darwin
archive-name: sonic-macos
- build: Windows
os: windows-latest
rust: nightly-x86_64-msvc
target: x86_64-pc-windows-msvc
archive-name: sonic-windows
|
拉取 sonic 的代码
1
2
3
| - uses: actions/checkout@v2
with:
repository: valeriansaliou/sonic
|
通过之前设置的变量安装 rust
工具链
1
2
3
4
5
6
7
8
| - name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
profile: minimal
override: true
components: rustfmt, clippy
target: ${{ matrix.target }}
|
删除 C:\msys64\
, 解决C:\msys64\mingw64\bin\libclang.dll
链接错误的工作方法
1
2
3
4
5
| - name: Clean Windows # Workaround to resolve link error with C:\msys64\mingw64\bin\libclang.dll
if: runner.os == 'Windows'
run: |
Remove-Item -LiteralPath "C:\msys64\" -Force -Recurse
choco install llvm -y
|
以详细编译信息的方式编译软件
1
2
3
4
| - name: Build binary
run: cargo build --verbose --release --target ${{ matrix.target }}
env:
RUST_BACKTRACE: 1
|
使用 strip
压缩二进制文件
1
2
3
| - name: Strip binary (Linux and macOS)
if: matrix.build == 'Linux' || matrix.build == 'macOS'
run: strip "target/${{ matrix.target }}/release/sonic"
|
复制文件准备打包
1
2
3
4
5
6
7
8
9
10
| - name: Build archive
shell: bash
run: |
mkdir archive
cd archive
if [ "${{ matrix.build }}" = "Windows" ]; then
cp "../target/${{ matrix.target }}/release/sonic.exe" ./
else
cp "../target/${{ matrix.target }}/release/sonic" ./
fi
|
打包并上传 archive
文件夹
1
2
3
4
5
| - name: Upload archive
uses: actions/upload-artifact@v1
with:
name: ${{ matrix.archive-name }}
path: archive/
|
github action
真方便, 开源赛高!
完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| name: Sonic CI
on:
push:
tags:
- "sonic_v*.*.*"
jobs:
build:
name: ${{ matrix.build }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [Linux, macOS, Windows]
include:
- build: Linux
os: ubuntu-latest
rust: nightly
target: x86_64-unknown-linux-gnu
archive-name: sonic-linux
- build: macOS
os: macos-latest
rust: nightly
target: x86_64-apple-darwin
archive-name: sonic-macos
- build: Windows
os: windows-latest
rust: nightly-x86_64-msvc
target: x86_64-pc-windows-msvc
archive-name: sonic-windows
steps:
- uses: actions/checkout@v2
with:
repository: valeriansaliou/sonic
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
profile: minimal
override: true
components: rustfmt, clippy
target: ${{ matrix.target }}
- name: Clean Windows # Workaround to resolve link error with C:\msys64\mingw64\bin\libclang.dll
if: runner.os == 'Windows'
run: |
Remove-Item -LiteralPath "C:\msys64\" -Force -Recurse
choco install llvm -y
- name: Build binary
run: cargo build --verbose --release --target ${{ matrix.target }}
env:
RUST_BACKTRACE: 1
- name: Strip binary (Linux and macOS)
if: matrix.build == 'Linux' || matrix.build == 'macOS'
run: strip "target/${{ matrix.target }}/release/sonic"
- name: Build archive
shell: bash
run: |
mkdir archive
cd archive
if [ "${{ matrix.build }}" = "Windows" ]; then
cp "../target/${{ matrix.target }}/release/sonic.exe" ./
else
cp "../target/${{ matrix.target }}/release/sonic" ./
fi
- name: Upload archive
uses: actions/upload-artifact@v1
with:
name: ${{ matrix.archive-name }}
path: archive/
|
KISS Keep It Simple, Stupid