2024-03-22 10:11 / 技术分享 Rust 项目在 GitHub Actions 跨平台自动化编译构建 macOS(ARM)/Windows/Linux 发行版并带有 Changelog ### 1. 配置 GitHub 项目可写权限 登录 GitHub 并打开要进行构建的 GitHub 项目主页,找到 [Setttings - Actions - General](https://github.com/godcheese/codegen/settings/actions) - Workflow permissions ,然后将 Read and write permissions 选中并 Save,如下图所示:  ### 2. 安装 FlyCI 以构建 macOS ARM 发行版(无需构建 macOS ARM 发行版可忽略) 1. 打开 [安装 FlyCI-prod](https://github.com/apps/flyci-prod),点击 Install,如下图所示:  2. 默认 All repositories,你可以自由选择 Only select repositories,选择你要构建的项目即可,然后点击 Install & Authorize 进行安装,如下图所示:  ### 3. 新建 workflow 构建文件 1. 在项目根目录下新建 `CHANGELOG.md`(记得提交到仓库),模板如下: ```markdown # Changelog ## v1.0.0 ### Features * 首个发行版 ``` 模板中的 v1.0.0 后带有一个空格,这个很重要,不然取不到对应版本的 changelog。 2. 在项目根目录下新建 `.github/workflows/release.yml` 文件(记得提交到仓库), 文件内容如下: ```yaml name: Build Release on: push: tags: - "v*" env: CARGO_TERM_COLOR: always # 可更换成自己项目编译后的名称 BIN_NAME: "codegen" jobs: build: name: build runs-on: ${{ matrix.os }} strategy: matrix: # 不需要构建 macOS ARM 发行版可删除 macos-arm 构建项 build: [linux, macos, macos-arm, windows] include: - build: linux os: ubuntu-20.04 run: sudo apt-get -y install musl-tools rust: stable target: x86_64-unknown-linux-musl release-file-suffix: x86_64-linux.tar.gz - build: macos os: macos-12 rust: stable target: x86_64-apple-darwin release-file-suffix: x86_64-macos.tar.gz # 不需要构建 macOS ARM 发行版可删除 macos-arm 构建项 - build: macos-arm os: flyci-macos-large-latest-m1 rust: stable target: aarch64-apple-darwin release-file-suffix: arm64-macos.tar.gz - build: windows os: windows-2019 rust: stable-x86_64-msvc target: x86_64-pc-windows-msvc release-file-suffix: x86_64-windows.7z fail-fast: false steps: - name: Checkout repository uses: actions/checkout@v2 with: fetch-depth: 0 - name: Get Tag Name id: tagName uses: olegtarasov/get-tag@v2.1.1 - name: Install Rust uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.rust }} profile: minimal override: true target: ${{ matrix.target }} - name: Install prerequisites if: matrix.run != '' run: ${{ matrix.run }} - 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' || matrix.build == 'macos-arm' run: strip "target/${{ matrix.target }}/release/${{ env.BIN_NAME }}" - name: Generate Changelog id: changelog uses: MatteoCampinoti94/changelog-to-release@v1.0.0 with: version-name: ${{ steps.tagName.outputs.tag }} - name: Build Release shell: bash run: | mkdir -p dist temp # 自定义拷贝要打包到压缩包里的文件 cp -r LICENSE README.md CHANGELOG.md config.yaml ./templates temp/ cd temp version="${{ steps.tagName.outputs.tag }}" if [ "${{ matrix.build }}" = "windows" ]; then cp "../target/${{ matrix.target }}/release/${{ env.BIN_NAME }}.exe" ./ 7z a -r "../dist/${{ env.BIN_NAME }}-$version-${{ matrix.release-file-suffix }}" * else cp "../target/${{ matrix.target }}/release/${{ env.BIN_NAME }}" ./ tar czf "../dist/${{ env.BIN_NAME }}-$version-${{ matrix.release-file-suffix }}" * fi - name: Upload Release Assets uses: softprops/action-gh-release@v1 if: startsWith(github.ref, 'refs/tags/') with: tag_name: ${{ steps.tagName.outputs.tag }} name: ${{ steps.changelog.outputs.title }} draft: false prerelease: false body: ${{ steps.changelog.outputs.body }} files: "dist/**" ``` ### 4. 新建 tag 以触发 workflow ```bash # 本地新建 tag git tag "v1.0.0" # 将新建 tag 推送到远程 git push origin "v1.0.0" ``` 此时打开 [GitHub Actions](https://github.com/godcheese/codegen/actions) 即可看到有 workflow 正在运行,如下图所示(这是已经运行完成的 workflow): 
发表评论 取消回复