build:test #8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: 构建跨平台应用 | |
| on: | |
| push: | |
| branches: [ main, master ] | |
| tags: [ 'v*' ] | |
| pull_request: | |
| branches: [ main, master ] | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| name: 构建 ${{ matrix.os }} | |
| runs-on: ${{ matrix.os }} | |
| permissions: | |
| contents: write | |
| strategy: | |
| matrix: | |
| os: [ubuntu-latest, windows-latest, macos-latest] | |
| include: | |
| - os: ubuntu-latest | |
| output_name: xiaozhi-client-linux | |
| asset_name: xiaozhi-client-linux | |
| - os: windows-latest | |
| output_name: xiaozhi-client-windows.exe | |
| asset_name: xiaozhi-client-windows | |
| - os: macos-latest | |
| output_name: xiaozhi-client-macos | |
| asset_name: xiaozhi-client-macos | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: 设置 Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '1.20' | |
| cache: true | |
| - name: 安装依赖 (Linux) | |
| if: matrix.os == 'ubuntu-latest' | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y portaudio19-dev libasound2-dev libopus-dev pkg-config libopusfile-dev | |
| # 验证opus库是否可用 | |
| pkg-config --libs --cflags opus || true | |
| pkg-config --libs --cflags opusfile || true | |
| # 输出安装的库信息 | |
| dpkg -l | grep opus | |
| env: | |
| DEBIAN_FRONTEND: noninteractive | |
| - name: 安装依赖 (macOS) | |
| if: matrix.os == 'macos-latest' | |
| run: | | |
| brew install portaudio | |
| brew install opus | |
| brew install opusfile | |
| brew install pkg-config | |
| # 设置PKG_CONFIG_PATH确保能找到opus和opusfile | |
| echo "PKG_CONFIG_PATH=$(brew --prefix)/lib/pkgconfig:$PKG_CONFIG_PATH" >> $GITHUB_ENV | |
| # 验证opus和opusfile库是否可用 | |
| echo "====== Opus库信息 ======" | |
| pkg-config --libs --cflags opus || true | |
| echo "====== OpusFile库信息 ======" | |
| pkg-config --libs --cflags opusfile || true | |
| # 显示库位置 | |
| echo "====== Opus库位置 ======" | |
| brew list opus || true | |
| echo "====== OpusFile库位置 ======" | |
| brew list opusfile || true | |
| # 手动创建opus.pc文件(如果pkg-config找不到) | |
| mkdir -p $HOME/pkgconfig | |
| if ! pkg-config --exists opus; then | |
| BREW_PREFIX=$(brew --prefix) | |
| cat > $HOME/pkgconfig/opus.pc << EOF | |
| Name: opus | |
| Description: Opus IETF audio codec | |
| Version: 1.3.1 | |
| Libs: -L${BREW_PREFIX}/lib -lopus | |
| Cflags: -I${BREW_PREFIX}/include/opus | |
| EOF | |
| echo "PKG_CONFIG_PATH=$HOME/pkgconfig:$(brew --prefix)/lib/pkgconfig:$PKG_CONFIG_PATH" >> $GITHUB_ENV | |
| fi | |
| # 尝试直接指定opus头文件和库文件位置 | |
| BREW_PREFIX=$(brew --prefix) | |
| echo "CGO_CFLAGS=-I${BREW_PREFIX}/include -I${BREW_PREFIX}/include/opus" >> $GITHUB_ENV | |
| echo "CGO_LDFLAGS=-L${BREW_PREFIX}/lib -lopus" >> $GITHUB_ENV | |
| # 为Windows设置MSYS2 | |
| - name: 设置MSYS2 | |
| if: matrix.os == 'windows-latest' | |
| uses: msys2/setup-msys2@v2 | |
| with: | |
| msystem: MINGW64 | |
| update: true | |
| install: >- | |
| mingw-w64-x86_64-gcc | |
| mingw-w64-x86_64-portaudio | |
| mingw-w64-x86_64-pkg-config | |
| mingw-w64-x86_64-opus | |
| mingw-w64-x86_64-opusfile | |
| - name: 为Windows设置环境变量 | |
| if: matrix.os == 'windows-latest' | |
| shell: bash | |
| run: | | |
| # 设置CGO环境变量和路径 | |
| echo "CGO_ENABLED=1" >> $GITHUB_ENV | |
| echo "PATH=$PATH:/msys64/mingw64/bin" >> $GITHUB_ENV | |
| echo "PKG_CONFIG_PATH=/msys64/mingw64/lib/pkgconfig" >> $GITHUB_ENV | |
| # 验证opus库是否可用 | |
| export PATH=$PATH:/msys64/mingw64/bin | |
| pkg-config --libs --cflags opus || true | |
| pkg-config --libs --cflags opusfile || true | |
| - name: 获取依赖 | |
| run: go mod tidy | |
| - name: 构建应用 | |
| run: | | |
| VERSION=$(echo $GITHUB_REF | sed -e 's/refs\/tags\/v//' -e 's/refs\/heads\///') | |
| if [ -z "$VERSION" ]; then | |
| VERSION="dev" | |
| fi | |
| # 设置CGO_ENABLED环境变量 | |
| export CGO_ENABLED=1 | |
| # 调试输出Go环境 | |
| go env | |
| # 针对macOS的特殊构建设置 | |
| if [[ "$RUNNER_OS" == "macOS" ]]; then | |
| # 尝试使用特定标志构建 | |
| go build -v -x -o ${{ matrix.output_name }} -ldflags "-X main.Version=${VERSION}" -tags=dynamic ./cmd/client | |
| else | |
| # 其他平台正常构建 | |
| go build -v -o ${{ matrix.output_name }} -ldflags "-X main.Version=${VERSION}" ./cmd/client | |
| fi | |
| shell: bash | |
| env: | |
| CGO_ENABLED: 1 | |
| - name: 压缩构建产物 (Unix-like) | |
| if: matrix.os != 'windows-latest' | |
| run: | | |
| tar -czvf ${{ matrix.asset_name }}.tar.gz ${{ matrix.output_name }} | |
| shell: bash | |
| - name: 压缩构建产物 (Windows) | |
| if: matrix.os == 'windows-latest' | |
| run: | | |
| 7z a ${{ matrix.asset_name }}.zip ${{ matrix.output_name }} | |
| shell: bash | |
| - name: 上传构建产物 | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ matrix.asset_name }} | |
| path: | | |
| ${{ matrix.asset_name }}.tar.gz | |
| ${{ matrix.asset_name }}.zip | |
| if-no-files-found: warn | |
| retention-days: 5 | |
| release: | |
| name: 创建发布 | |
| needs: build | |
| if: startsWith(github.ref, 'refs/tags/v') | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: 下载所有构建产物 | |
| uses: actions/download-artifact@v4 | |
| - name: 展示下载的文件 | |
| run: ls -R | |
| shell: bash | |
| - name: 创建发布 | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| files: | | |
| xiaozhi-client-linux/xiaozhi-client-linux.tar.gz | |
| xiaozhi-client-windows/xiaozhi-client-windows.zip | |
| xiaozhi-client-macos/xiaozhi-client-macos.tar.gz | |
| draft: false | |
| prerelease: false |