使用Velopack ,基于github的action,avalonia项目,windows、linux、mac的打包

Avalonia + Velopack 跨平台自动打包(GitHub Actions)

只需把下面两份文件原样放进仓库,即可在推送 v* 标签后,自动生成

  • Windows .exe(含自动更新)
  • Linux .AppImage
  • macOS .dmg

并发布到 GitHub Release。


1. 项目前置(一次性)

# 引入 Velopack 库
dotnet add package Velopack

Program.cs 顶部加一行:

using Velopack;
VelopackApp.Build().Run();

using System;
using Avalonia;
using Velopack;

namespace CSharpAvalonia;

class Program
{
    public static MemoryLogger Log { get; private set; } = new();

    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args)
    {
        try {
            // It's important to Run() the VelopackApp as early as possible in app startup.
            VelopackApp.Build()
                .OnFirstRun((v) => { /* Your first run code here */ })
                .SetLogger(Log)
                .Run();

            // Now it's time to run Avalonia
            BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);

        } catch (Exception ex) {
            string message = "Unhandled exception: " + ex.ToString();
            Console.WriteLine(message);
            throw;
        }
    }

    // Avalonia configuration, don't remove method; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
    {
        return AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace();
    }
}

2. GitHub Actions .github/workflows/release.yml

name: Release with Velopack

on:
  push:
    tags: [ "v*" ]      # 推送 v1.x.x 标签时触发

env:
  DOTNET_NOLOGO: true
  CSPROJ: src/MyApp/MyApp.csproj   # <-- 改成你的 .csproj
  RID_WIN: win-x64
  RID_LIN: linux-x64
  RID_MAC: osx-x64

jobs:
  build:
    strategy:
      matrix:
        include:
          - os: windows-latest
            rid: win-x64
            ext: .exe
          - os: ubuntu-latest
            rid: linux-x64
            ext: .AppImage
          - os: macos-latest
            rid: osx-x64
            ext: .dmg

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 8.0.x

      - name: dotnet publish
        run: |
          dotnet publish ${{ env.CSPROJ }} \
            -c Release \
            -r ${{ matrix.rid }} \
            --self-contained true \
            -p:PublishSingleFile=true \
            -p:IncludeNativeLibrariesInSingleFile=true \
            -o publish/${{ matrix.rid }}

      - name: Install Velopack CLI
        shell: pwsh
        run: dotnet tool install --global vpk

      - name: Velopack pack
        shell: pwsh
        run: |
          vpk pack \
            --packId "MyApp" \
            --packVersion "${{ github.ref_name }}" \
            --packDir publish/${{ matrix.rid }} \
            --mainExe "MyApp${{ matrix.ext == '.exe' && '.exe' || '' }}" \
            --outputDir dist/${{ matrix.rid }} \
            --icon src/MyApp/Assets/icon.ico

      - uses: actions/upload-artifact@v4
        with:
          name: MyApp-${{ matrix.rid }}
          path: dist/${{ matrix.rid }}/*

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: dist

      - name: GitHub Release
        uses: ncipollo/release-action@v1
        with:
          tag: ${{ github.ref }}
          artifacts: "dist/**/*"
          generateReleaseNotes: true
          prerelease: ${{ contains(github.ref_name, '-') }}

3. 使用步骤

把 CSPROJ / packId / 图标路径改成自己的。
推送标签触发:

git tag v1.2.3
git push origin v1.2.3

几分钟后,在 GitHub → Releases 即可下载安装包。

4. 本地验证

dotnet tool install --global vpk
vpk pack --packId MyApp --packVersion 1.0.0-local \
         --packDir bin/Release/net8.0/win-x64/publish \
         --mainExe MyApp.exe --outputDir _local

双击 _local/MyApp-1.0.0-local-setup.exe 体验安装。

posted @ 2025-08-25 14:06  Timskt  阅读(140)  评论(0)    收藏  举报