Skip to content

Update build.yml

Update build.yml #4

Workflow file for this run

name: Cross-Platform Build
on:
push:
branches: [ main, master, develop ]
pull_request:
branches: [ main, master, develop ]
release:
types: [published]
jobs:
build:
name: Build ${{ matrix.os }}-${{ matrix.arch }}
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
matrix:
include:
# Linux builds
- os: linux
arch: x86_64
runner: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
executable_suffix: ""
cmake_arch: ""
- os: linux
arch: aarch64
runner: ubuntu-latest
c_compiler: aarch64-linux-gnu-gcc
cpp_compiler: aarch64-linux-gnu-g++
executable_suffix: ""
cmake_arch: "-DCMAKE_SYSTEM_PROCESSOR=aarch64"
- os: linux
arch: i386
runner: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
executable_suffix: ""
cmake_arch: "-DCMAKE_CXX_FLAGS=-m32 -DCMAKE_C_FLAGS=-m32"
# Windows builds
- os: windows
arch: x86_64
runner: windows-latest
c_compiler: cl
cpp_compiler: cl
executable_suffix: ".exe"
cmake_arch: "-A x64"
- os: windows
arch: aarch64
runner: windows-latest
c_compiler: cl
cpp_compiler: cl
executable_suffix: ".exe"
cmake_arch: "-A ARM64"
- os: windows
arch: i386
runner: windows-latest
c_compiler: cl
cpp_compiler: cl
executable_suffix: ".exe"
cmake_arch: "-A Win32"
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: recursive
- name: Install dependencies (Linux)
if: matrix.os == 'linux'
run: |
sudo apt-get update
sudo apt-get install -y build-essential cmake ninja-build
# ARM64 交叉编译工具链
if [ "${{ matrix.arch }}" == "aarch64" ]; then
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
fi
# i386 支持
if [ "${{ matrix.arch }}" == "i386" ]; then
sudo apt-get install -y gcc-multilib g++-multilib
fi
- name: Install dependencies (Windows)
if: matrix.os == 'windows'
run: |
choco install ninja
- name: Configure CMake (Linux)
if: matrix.os == 'linux'
run: |
cmake -B ${{ github.workspace }}/build \
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} \
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }} \
-DCMAKE_BUILD_TYPE=Release \
-G Ninja \
${{ matrix.cmake_arch }}
- name: Configure CMake (Windows)
if: matrix.os == 'windows'
run: |
cmake -B ${{ github.workspace }}/build \
-DCMAKE_BUILD_TYPE=Release \
${{ matrix.cmake_arch }}
- name: Build
run: |
cmake --build ${{ github.workspace }}/build --config Release
- name: Test
if: matrix.arch == 'x86_64' # 只在x86_64架构上运行测试
working-directory: ${{ github.workspace }}/build
run: |
ctest --build-config Release --output-on-failure
- name: Package artifacts
run: |
mkdir -p artifacts
# 查找并复制可执行文件
if [ "${{ matrix.os }}" == "windows" ]; then
# Windows 构建输出路径
find build -name "*minecraft-mod-classifier*${{ matrix.executable_suffix }}" -type f -exec cp {} artifacts/ \;
find build/Release -name "*minecraft-mod-classifier*${{ matrix.executable_suffix }}" -type f -exec cp {} artifacts/ \; 2>/dev/null || true
else
# Linux 构建输出路径
find build -name "*minecraft-mod-classifier*${{ matrix.executable_suffix }}" -type f -exec cp {} artifacts/ \;
fi
# 复制数据文件
if [ -f "assets/mods-data.json" ]; then
cp assets/mods-data.json artifacts/
fi
# 重命名文件以包含架构信息
cd artifacts
for file in *${{ matrix.executable_suffix }}; do
if [ -f "$file" ]; then
mv "$file" "${file%${{ matrix.executable_suffix }}}-${{ matrix.os }}-${{ matrix.arch }}${{ matrix.executable_suffix }}"
fi
done
shell: bash
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: minecraft-mod-classifier-${{ matrix.os }}-${{ matrix.arch }}
path: artifacts/
retention-days: 30
release:
if: github.event_name == 'release'
needs: build
runs-on: ubuntu-latest
steps:
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: release-artifacts
- name: Create release archives
run: |
cd release-artifacts
for dir in */; do
if [ -d "$dir" ]; then
cd "$dir"
if [[ "$dir" == *"windows"* ]]; then
# Windows 平台使用 zip
zip -r "../${dir%/}.zip" *
else
# Linux 平台使用 tar.gz
tar -czf "../${dir%/}.tar.gz" *
fi
cd ..
fi
done
- name: Upload release assets
uses: softprops/action-gh-release@v2
with:
files: |
release-artifacts/*.tar.gz
release-artifacts/*.zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}