When our Rust
program needs to be compiled for different platforms and architectures, compiling one by one can be very cumbersome. However, using GitHub Actions for compilation makes it very easy. Especially when you don't have the conditions to compile in other system environments, GitHub Actions is simply a magical tool.
GitHub Actions Configuration File
# GitHub Actions workflow configuration file
name: Build and Release
on:
push:
tags:
- "v*.*.*" # Trigger the workflow when a tag matching the pattern v*.*.* is pushed
jobs:
build:
runs-on: ${{ matrix.os }} # Define the operating system on which the job runs
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest] # Define the operating system matrix
arch: [x86_64, aarch64] # Define the architecture matrix
steps:
- name: Checkout code
uses: actions/checkout@v2 # Check out the repository code
- name: Set up Rust
uses: actions-rs/toolchain@v1 # Set up the Rust toolchain
with:
toolchain: stable # Use the stable Rust toolchain
profile: minimal # Use the minimal configuration file
override: true # Override any existing Rust toolchain settings
- name: Build project
run: cargo build --release # Build the release version of the project
# Upload build artifacts for Linux x86_64 platform
- 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
# Upload build artifacts for Linux aarch64 platform
- 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
# Upload build artifacts for macOS x86_64 platform
- 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
# Upload build artifacts for macOS aarch64 platform
- 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
# Upload build artifacts for Windows x86_64 platform
- 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
# Upload build artifacts for Windows aarch64 platform
- 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 # Define the job to run on Ubuntu
needs: build # Depends on the build job
steps:
- name: Checkout code
uses: actions/checkout@v2 # Check out the repository code
# Download build artifacts for Linux x86_64 platform
- name: Download artifact (Linux-x86_64)
uses: actions/download-artifact@v2
with:
name: ubuntu-latest-heaven
path: artifacts/ubuntu
# Download build artifacts for macOS x86_64 platform
- name: Download artifact (macOS-x86_64)
uses: actions/download-artifact@v2
with:
name: macos-latest-heaven
path: artifacts/macos
# Download build artifacts for Windows x86_64 platform
- name: Download artifact (Windows-x86_64)
uses: actions/download-artifact@v2
with:
name: windows-latest-heaven
path: artifacts/windows
# Download build artifacts for Linux aarch64 platform
- name: Download artifact (Linux-aarch64)
uses: actions/download-artifact@v2
with:
name: ubuntu-latest-heaven-aarch64
path: artifacts/ubuntu
# Download build artifacts for macOS aarch64 platform
- name: Download artifact (macOS-aarch64)
uses: actions/download-artifact@v2
with:
name: macos-latest-heaven-aarch64
path: artifacts/macos
# Download build artifacts for Windows aarch64 platform
- 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 # Create a GitHub release
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # Authenticate using GitHub token
with:
tag_name: ${{ github.ref }} # Use the pushed tag name
release_name: Release ${{ github.ref }} # Use the pushed tag name as the release name
body: |
Changes in this Release
- First Change
- Second Change # Release notes
draft: false # Whether it is a draft
prerelease: false # Whether it is a pre-release
# Upload Linux x86_64 build artifacts to the release page
- name: Upload Linux x86_64 artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # Authenticate using GitHub token
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # Use the upload URL generated in the create release step
asset_path: artifacts/ubuntu/heaven # Path to the file to upload
asset_name: heaven-linux-x86_64 # Name of the file to upload
asset_content_type: application/octet-stream # File content type
# Upload macOS x86_64 build artifacts to the release page
- name: Upload macOS x86_64 artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # Authenticate using GitHub token
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # Use the upload URL generated in the create release step
asset_path: artifacts/macos/heaven # Path to the file to upload
asset_name: heaven-macos-x86_64 # Name of the file to upload
asset_content_type: application/octet-stream # File content type
# Upload Windows x86_64 build artifacts to the release page
- name: Upload Windows x86_64 artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # Authenticate using GitHub token
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # Use the upload URL generated in the create release step
asset_path: artifacts/windows/heaven.exe # Path to the file to upload
asset_name: heaven-windows-x86_64.exe # Name of the file to upload
asset_content_type: application/octet-stream # File content type
# Upload Linux aarch64 build artifacts to the release page
- name: Upload Linux aarch64 artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # Authenticate using GitHub token
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # Use the upload URL generated in the create release step
asset_path: artifacts/ubuntu/heaven # Path to the file to upload
asset_name: heaven-linux-aarch64 # Name of the file to upload
asset_content_type: application/octet-stream # File content type
# Upload macOS aarch64 build artifacts to the release page
- name: Upload macOS aarch64 artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # Authenticate using GitHub token
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # Use the upload URL generated in the create release step
asset_path: artifacts/macos/heaven # Path to the file to upload
asset_name: heaven-macos-aarch64 # Name of the file to upload
asset_content_type: application/octet-stream # File content type
# Upload Windows aarch64 build artifacts to the release page
- name: Upload Windows aarch64 artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} # Authenticate using GitHub token
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # Use the upload URL generated in the create release step
asset_path: artifacts/windows/heaven.exe # Path to the file to upload
asset_name: heaven-windows-aarch64.exe # Name of the file to upload
asset_content_type: application/octet-stream # File content type
In this GitHub Actions configuration file, I used the following modules (actions):
-
actions/checkout@v2: Used to check out the repository code.
- name: Checkout code uses: actions/checkout@v2
-
actions-rs/toolchain@v1: Used to set up the Rust toolchain.
- name: Set up Rust uses: actions-rs/toolchain@v1 with: toolchain: stable profile: minimal override: true
-
actions/upload-artifact@v2: Used to upload build artifacts as workflow artifacts.
- name: Upload artifact (Linux) uses: actions/upload-artifact@v2 with: name: ubuntu-latest-heaven path: target/release/heaven
-
actions/download-artifact@v2: Used to download artifacts uploaded in previous steps.
- name: Download artifact (Linux-x86_64) uses: actions/download-artifact@v2 with: name: ubuntu-latest-heaven path: artifacts/ubuntu
-
actions/create-release@v1: Used to create a 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: Used to upload build artifacts to 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
These modules (actions) provide a complete workflow from code checkout, Rust environment setup, artifact upload and download, to creating a GitHub Release and publishing artifacts to the Release.