xChar
·4 months ago

当我们的 Rust 程序需要编译到不同的平台,不同的架构上的时候,一个一个编译是非常麻烦的。但是如果使用GitHub Actions来编译的话,就非常轻松。特别是当你没有条件在其他系统环境上编译的时候,GitHub Actions简直是神器。

GitHub Actions 配置文件

image

# GitHub Actions工作流配置文件

name: Build and Release

on:
  push:
    tags:
      - "v*.*.*" # 当推送符合模式v*.*.*的标签时触发工作流

jobs:
  build:
    runs-on: ${{ matrix.os }} # 定义作业运行的操作系统
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest] # 定义操作系统矩阵
        arch: [x86_64, aarch64] # 定义架构矩阵

    steps:
      - name: Checkout code
        uses: actions/checkout@v2 # 检出仓库代码

      - name: Set up Rust
        uses: actions-rs/toolchain@v1 # 设置Rust工具链
        with:
          toolchain: stable # 使用稳定版Rust工具链
          profile: minimal # 使用最小化配置文件
          override: true # 覆盖任何现有的Rust工具链设置

      - name: Build project
        run: cargo build --release # 构建项目的发布版本

      # 上传Linux x86_64平台的构建产物
      - name: Upload artifact (Linux)
        if: matrix.os == 'ubuntu-latest' && matrix.arch == 'x86_64'
        uses: actions/upload-artifact@v2
        with:
          name: ubuntu-latest-heaven
          path: target/release/heaven

      # 上传Linux aarch64平台的构建产物
      - name: Upload artifact (Linux aarch64)
        if: matrix.os == 'ubuntu-latest' && matrix.arch == 'aarch64'
        uses: actions/upload-artifact@v2
        with:
          name: ubuntu-latest-heaven-aarch64
          path: target/release/heaven

      # 上传macOS x86_64平台的构建产物
      - name: Upload artifact (macOS)
        if: matrix.os == 'macos-latest' && matrix.arch == 'x86_64'
        uses: actions/upload-artifact@v2
        with:
          name: macos-latest-heaven
          path: target/release/heaven

      # 上传macOS aarch64平台的构建产物
      - name: Upload artifact (macOS aarch64)
        if: matrix.os == 'macos-latest' && matrix.arch == 'aarch64'
        uses: actions/upload-artifact@v2
        with:
          name: macos-latest-heaven-aarch64
          path: target/release/heaven

      # 上传Windows x86_64平台的构建产物
      - name: Upload artifact (Windows)
        if: matrix.os == 'windows-latest' && matrix.arch == 'x86_64'
        uses: actions/upload-artifact@v2
        with:
          name: windows-latest-heaven
          path: target/release/heaven.exe

      # 上传Windows aarch64平台的构建产物
      - name: Upload artifact (Windows aarch64)
        if: matrix.os == 'windows-latest' && matrix.arch == 'aarch64'
        uses: actions/upload-artifact@v2
        with:
          name: windows-latest-heaven-aarch64
          path: target/release/heaven.exe

  release:
    runs-on: ubuntu-latest # 定义作业运行在Ubuntu系统上
    needs: build # 依赖于build作业

    steps:
      - name: Checkout code
        uses: actions/checkout@v2 # 检出仓库代码

      # 下载Linux x86_64平台的构建产物
      - name: Download artifact (Linux-x86_64)
        uses: actions/download-artifact@v2
        with:
          name: ubuntu-latest-heaven
          path: artifacts/ubuntu

      # 下载macOS x86_64平台的构建产物
      - name: Download artifact (macOS-x86_64)
        uses: actions/download-artifact@v2
        with:
          name: macos-latest-heaven
          path: artifacts/macos

      # 下载Windows x86_64平台的构建产物
      - name: Download artifact (Windows-x86_64)
        uses: actions/download-artifact@v2
        with:
          name: windows-latest-heaven
          path: artifacts/windows

      # 下载Linux aarch64平台的构建产物
      - name: Download artifact (Linux-aarch64)
        uses: actions/download-artifact@v2
        with:
          name: ubuntu-latest-heaven-aarch64
          path: artifacts/ubuntu

      # 下载macOS aarch64平台的构建产物
      - name: Download artifact (macOS-aarch64)
        uses: actions/download-artifact@v2
        with:
          name: macos-latest-heaven-aarch64
          path: artifacts/macos

      # 下载Windows aarch64平台的构建产物
      - name: Download artifact (Windows-aarch64)
        uses: actions/download-artifact@v2
        with:
          name: windows-latest-heaven-aarch64
          path: artifacts/windows

      - name: Create release
        id: create_release
        uses: actions/create-release@v1 # 创建GitHub release
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # 使用GitHub令牌进行身份验证
        with:
          tag_name: ${{ github.ref }} # 使用推送的标签名
          release_name: Release ${{ github.ref }} # 使用推送的标签名作为发布名称
          body: |
            Changes in this Release
            - First Change
            - Second Change # 发布说明
          draft: false # 是否为草稿
          prerelease: false # 是否为预发布

      # 上传Linux x86_64平台的构建产物到发布页面
      - name: Upload Linux x86_64 artifact
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # 使用GitHub令牌进行身份验证
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # 使用创建发布步骤生成的上传URL
          asset_path: artifacts/ubuntu/heaven # 上传的文件路径
          asset_name: heaven-linux-x86_64 # 上传的文件名
          asset_content_type: application/octet-stream # 文件内容类型

      # 上传macOS x86_64平台的构建产物到发布页面
      - name: Upload macOS x86_64 artifact
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # 使用GitHub令牌进行身份验证
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # 使用创建发布步骤生成的上传URL
          asset_path: artifacts/macos/heaven # 上传的文件路径
          asset_name: heaven-macos-x86_64 # 上传的文件名
          asset_content_type: application/octet-stream # 文件内容类型

      # 上传Windows x86_64平台的构建产物到发布页面
      - name: Upload Windows x86_64 artifact
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # 使用GitHub令牌进行身份验证
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # 使用创建发布步骤生成的上传URL
          asset_path: artifacts/windows/heaven.exe # 上传的文件路径
          asset_name: heaven-windows-x86_64.exe # 上传的文件名
          asset_content_type: application/octet-stream # 文件内容类型

      # 上传Linux aarch64平台的构建产物到发布页面
      - name: Upload Linux aarch64 artifact
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # 使用GitHub令牌进行身份验证
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # 使用创建发布步骤生成的上传URL
          asset_path: artifacts/ubuntu/heaven # 上传的文件路径
          asset_name: heaven-linux-aarch64 # 上传的文件名
          asset_content_type: application/octet-stream # 文件内容类型

      # 上传macOS aarch64平台的构建产物到发布页面
      - name: Upload macOS aarch64 artifact
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # 使用GitHub令牌进行身份验证
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # 使用创建发布步骤生成的上传URL
          asset_path: artifacts/macos/heaven # 上传的文件路径
          asset_name: heaven-macos-aarch64 # 上传的文件名
          asset_content_type: application/octet-stream # 文件内容类型

      # 上传Windows aarch64平台的构建产物到发布页面
      - name: Upload Windows aarch64 artifact
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # 使用GitHub令牌进行身份验证
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # 使用创建发布步骤生成的上传URL
          asset_path: artifacts/windows/heaven.exe # 上传的文件路径
          asset_name: heaven-windows-aarch64.exe # 上传的文件名
          asset_content_type: application/octet-stream # 文件内容类型

image

image

在这个GitHub Actions配置文件中,我使用了以下模块(actions):

  1. actions/checkout@v2:用于检出仓库的代码。

    - name: Checkout code
      uses: actions/checkout@v2
    
  2. actions-rs/toolchain@v1:用于设置Rust工具链。

    - name: Set up Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        profile: minimal
        override: true
    
  3. actions/upload-artifact@v2:用于上传构建产物作为工作流的工件。

    - name: Upload artifact (Linux)
      uses: actions/upload-artifact@v2
      with:
        name: ubuntu-latest-heaven
        path: target/release/heaven
    
  4. actions/download-artifact@v2:用于下载在之前步骤中上传的工件。

    - name: Download artifact (Linux-x86_64)
      uses: actions/download-artifact@v2
      with:
        name: ubuntu-latest-heaven
        path: artifacts/ubuntu
    
  5. actions/create-release@v1:用于创建GitHub Release。

    - name: Create release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        body: |
          Changes in this Release
          - First Change
          - Second Change
        draft: false
        prerelease: false
    
  6. actions/upload-release-asset@v1:用于将构建产物上传到GitHub Release。

    - name: Upload Linux x86_64 artifact
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: artifacts/ubuntu/heaven
        asset_name: heaven-linux-x86_64
        asset_content_type: application/octet-stream
    

这些模块(actions)提供了从代码检出、Rust环境设置、构建产物上传和下载,到创建GitHub Release以及将产物发布到Release的完整流程。

Loading comments...