當我們的 Rust
程式需要編譯到不同的平台,不同的架構上的時候,一個一個編譯是非常麻煩的。但是如果使用GitHub Actions來編譯的話,就非常輕鬆。特別是當你沒有條件在其他系統環境上編譯的時候,GitHub Actions 簡直是神器。
GitHub Actions 配置文件
# 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 # 文件內容類型
在這個 GitHub Actions 配置文件中,我使用了以下模塊(actions):
-
actions/checkout@v2:用於檢出倉庫的代碼。
- name: Checkout code uses: actions/checkout@v2
-
actions-rs/toolchain@v1:用於設置 Rust 工具鏈。
- name: Set up Rust uses: actions-rs/toolchain@v1 with: toolchain: stable profile: minimal override: true
-
actions/upload-artifact@v2:用於上傳構建產物作為工作流的工件。
- name: Upload artifact (Linux) uses: actions/upload-artifact@v2 with: name: ubuntu-latest-heaven path: target/release/heaven
-
actions/download-artifact@v2:用於下載在之前步驟中上傳的工件。
- name: Download artifact (Linux-x86_64) uses: actions/download-artifact@v2 with: name: ubuntu-latest-heaven path: artifacts/ubuntu
-
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
-
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 的完整流程。