Compare commits

...

3 Commits

Author SHA1 Message Date
d5a9939429
draft6
Some checks failed
KMP Build & Package / Build Linux Native on ubuntu-latest (push) Has been cancelled
KMP Build & Package / Build macOS Native on macos-latest (push) Has been cancelled
KMP Build & Package / Build Windows Native on windows-latest (push) Has been cancelled
2025-05-12 09:06:15 +08:00
4c230930b7
draft5 2025-05-12 09:05:05 +08:00
3fec812079
draft4 2025-05-12 09:00:46 +08:00

View File

@ -1,46 +1,159 @@
name: Build and Release name: KMP Build & Package
on: on:
push: push:
branches: [ main, develop ] # 在推送到 main 或 develop 分支时触发
tags: tags:
- 'v*.*.*' # 仅在推送符合语义化版本的标签时触发 - 'v*' # 在推送版本标签时触发 (例如 v1.0.0)
pull_request:
branches: [ main, develop ] # 在向 main 或 develop 分支发起 Pull Request 时触发
workflow_dispatch: # 允许手动触发
jobs: jobs:
build: build:
name: Build All Targets strategy:
runs-on: ubuntu-latest fail-fast: false # 即使一个 job 失败,其他 job 也会继续运行
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ] # 根据你的目标平台选择操作系统
include:
# Android 构建 (通常在 Linux 环境下进行)
- os: ubuntu-latest
target: android
description: "Build Android App"
gradle_task: ":shared:assembleRelease :androidApp:assembleRelease" # 替换成你的 Android 打包任务
artifact_name: "android-app"
artifact_path: |
shared/build/outputs/aar/*.aar
androidApp/build/outputs/apk/release/*.apk
androidApp/build/outputs/bundle/release/*.aab
# iOS 构建 (必须在 macOS 环境下进行)
- os: macos-latest
target: ios
description: "Build iOS Framework"
# 注意: iOS 构建可能需要特定的 Xcode 版本,可以使用 actions/setup-xcode
# gradle_task: ":shared:packForXCFramework" # 常见的 iOS framework 打包任务
gradle_task: ":shared:assembleXCFramework" # 或者使用新的 Kotlin 插件的任务名
artifact_name: "ios-framework"
artifact_path: "shared/build/XCFrameworks/release/*.xcframework" # 检查你的实际输出路径
# JVM/Desktop 构建 (可以在多个操作系统上运行)
- os: ubuntu-latest # 为 Linux 构建
target: jvm_linux
description: "Build JVM/Desktop (Linux)"
gradle_task: ":desktopApp:packageDistributionForCurrentOS" # 替换成你的 Desktop 打包任务
artifact_name: "desktop-app-linux"
artifact_path: "desktopApp/build/compose/binaries/main/app/*" # 检查你的实际输出路径
- os: macos-latest # 为 macOS 构建
target: jvm_macos
description: "Build JVM/Desktop (macOS)"
gradle_task: ":desktopApp:packageDistributionForCurrentOS"
artifact_name: "desktop-app-macos"
artifact_path: "desktopApp/build/compose/binaries/main/app/*" # 检查你的实际输出路径
- os: windows-latest # 为 Windows 构建
target: jvm_windows
description: "Build JVM/Desktop (Windows)"
gradle_task: ":desktopApp:packageDistributionForCurrentOS"
artifact_name: "desktop-app-windows"
artifact_path: "desktopApp/build/compose/binaries/main/app/*" # 检查你的实际输出路径
# JavaScript 构建 (通常在 Linux 环境下进行)
- os: ubuntu-latest
target: js
description: "Build JavaScript"
gradle_task: ":shared:jsBrowserDistribution" # 替换成你的 JS 打包任务
artifact_name: "js-app"
artifact_path: "shared/build/distributions/*" # 检查你的实际输出路径
# Linux Native 构建
- os: ubuntu-latest
target: linux_native
description: "Build Linux Native"
gradle_task: ":shared:linkReleaseExecutableLinuxX64" # 替换成你的 Linux Native 编译任务
artifact_name: "linux-native-executable"
artifact_path: "shared/build/bin/linuxX64/releaseExecutable/*.kexe" # 检查你的实际输出路径
# macOS Native 构建 (也可以针对 arm64)
- os: macos-latest
target: macos_native
description: "Build macOS Native"
gradle_task: ":shared:linkReleaseExecutableMacosX64" # 或者 linkReleaseExecutableMacosArm64
artifact_name: "macos-native-executable"
artifact_path: "shared/build/bin/macosX64/releaseExecutable/*.kexe" # 检查你的实际输出路径
# Windows Native 构建
- os: windows-latest
target: windows_native
description: "Build Windows Native"
gradle_task: ":shared:linkReleaseExecutableMingwX64" # 替换成你的 Windows Native 编译任务
artifact_name: "windows-native-executable"
artifact_path: "shared/build/bin/mingwX64/releaseExecutable/*.exe" # 检查你的实际输出路径
# 如果某些平台不需要在所有操作系统上构建,可以在这里排除
# exclude:
# - os: windows-latest
# target: android
# - os: ubuntu-latest
# target: ios
name: ${{ matrix.description }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps: steps:
# 检出代码 - name: Checkout repository
- name: Checkout code uses: actions/checkout@v4
uses: actions/checkout@v3
- name: Set up JDK 17 # KMP 通常建议使用较新的 JDK 版本
# 设置 JDK 环境 uses: actions/setup-java@v4
- name: Set up JDK
uses: actions/setup-java@v3
with: with:
distribution: 'zulu' distribution: 'temurin' # 或者 'zulu', 'adopt' 等
java-version: '17' java-version: '17'
# 构建 Android APK # 仅在 macOS runner 上为 iOS 构建设置 Xcode 版本 (如果需要特定版本)
- name: Build Android APK - name: Select Xcode version (for iOS builds)
run: ./gradlew :androidApp:assembleRelease if: matrix.os == 'macos-latest' && matrix.target == 'ios'
run: sudo xcode-select -s /Applications/Xcode_15.3.app/Contents/Developer # 替换成你需要的 Xcode 版本路径
# 构建 iOS Framework # 你也可以使用 action 如 maxim-lobanov/setup-xcode@v1
- name: Build iOS Framework
run: ./gradlew :iosApp:build # 设置 Gradle (会自动处理 Gradle Wrapper)
- name: Setup Gradle
# 构建 Desktop 可执行文件 uses: gradle/actions/setup-gradle@v3 # 使用 v3 版本
- name: Build Desktop Executable with:
run: ./gradlew :desktopApp:packageRelease gradle-version: wrapper # 默认使用 Gradle Wrapper
# cache-read-only: ${{ github.ref != 'refs/heads/main' }} # 非 main 分支构建时,缓存只读 (可选)
# 上传构建产物到 GitHub Releases # cache-encryption-key: ${{ secrets.GRADLE_CACHE_ENCRYPTION_KEY }} # 加密缓存 (可选)
- name: Upload Release Assets
uses: actions/upload-release-asset@v2 # Gradle 缓存 (KMP 项目的 .konan 目录和 Gradle 缓存很重要)
- name: Cache Gradle packages
uses: actions/cache@v4
with: with:
name: ${{github.ref_name}}-build
path: | path: |
androidApp/build/outputs/apk/release/*.apk ~/.gradle/caches
iosApp/build/bin/ios/*.framework ~/.gradle/wrapper
desktopApp/build/outputs/*.exe key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
label: "Build for ${{ github.ref_name }}" restore-keys: |
${{ runner.os }}-gradle-
- name: Cache Kotlin Native ($HOME/.konan)
if: matrix.os == 'macos-latest' || matrix.os == 'ubuntu-latest' # .konan 缓存主要用于 Native 编译
uses: actions/cache@v4
with:
path: ~/.konan
key: ${{ runner.os }}-konan-${{ hashFiles('**/build.gradle.kts', '**/gradle.properties') }} # 或者更精确的 hash
restore-keys: |
${{ runner.os }}-konan-
# 授予 Gradle Wrapper 执行权限 (特别是对于 Windows)
- name: Make gradlew executable
if: runner.os != 'windows-latest'
run: chmod +x ./gradlew
- name: Make gradlew.bat executable (Windows)
if: runner.os == 'windows-latest'
run: cmd /c "chmod +x gradlew.bat" # 在 Windows 上,通常 gradlew.bat 默认就有执行权限,此步骤可能非必需
# 执行构建任务
- name: Build with Gradle
run: ./gradlew ${{ matrix.gradle_task }} --no-daemon # --no-daemon 可以在 CI 环境中更稳定
# 上传构建产物
# 注意: actions/upload-artifact@v4 的用法与 v3 不同
- name: Upload Artifact - ${{ matrix.artifact_name }}
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.artifact_name }}
path: ${{ matrix.artifact_path }}
if-no-files-found: error # 如果没有找到文件则报错