Merge pull request #2 from keaz/copilot/fix-ci-build-issue-macos #16
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: Build and Release | |
| on: | |
| push: | |
| branches: | |
| - main | |
| tags: | |
| - "v*" | |
| jobs: | |
| build: | |
| name: Build and Release Rust Binary | |
| runs-on: ${{ matrix.os }} | |
| permissions: | |
| contents: write | |
| packages: write | |
| strategy: | |
| matrix: | |
| os: [ubuntu-latest, macos-latest] | |
| include: | |
| - os: macos-latest | |
| target_x86: x86_64-apple-darwin | |
| target_arm: aarch64-apple-darwin | |
| - os: ubuntu-latest | |
| target: x86_64-unknown-linux-gnu | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 5 | |
| - name: Install system dependencies on Ubuntu | |
| if: matrix.os == 'ubuntu-latest' | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y clang libclang-dev llvm-dev | |
| - name: Verify Ubuntu dependencies | |
| if: matrix.os == 'ubuntu-latest' | |
| run: | | |
| echo "Clang version:" | |
| clang --version | |
| echo "libclang location:" | |
| find /usr -name "libclang.so*" 2>/dev/null | head -3 | |
| echo "LLVM config:" | |
| llvm-config --version 2>/dev/null || echo "llvm-config not in PATH" | |
| - name: Install dependencies on macOS | |
| if: matrix.os == 'macos-latest' | |
| run: | | |
| which brew || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
| # Install required dependencies for rdkafka with system librdkafka | |
| brew install openssl librdkafka pkg-config zstd lz4 zlib | |
| # Set environment variables for OpenSSL and librdkafka | |
| export OPENSSL_DIR=$(brew --prefix openssl@3) | |
| export PKG_CONFIG_PATH="$OPENSSL_DIR/lib/pkgconfig:$(brew --prefix librdkafka)/lib/pkgconfig:$(brew --prefix zstd)/lib/pkgconfig:$(brew --prefix lz4)/lib/pkgconfig" | |
| # Set environment variables to use system librdkafka | |
| export LDFLAGS="-L$(brew --prefix librdkafka)/lib -L$(brew --prefix zstd)/lib -L$(brew --prefix lz4)/lib -L$OPENSSL_DIR/lib" | |
| export CPPFLAGS="-I$(brew --prefix librdkafka)/include -I$(brew --prefix zstd)/include -I$(brew --prefix lz4)/include -I$OPENSSL_DIR/include" | |
| # Add to GitHub environment | |
| echo "OPENSSL_DIR=$OPENSSL_DIR" >> $GITHUB_ENV | |
| echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH" >> $GITHUB_ENV | |
| echo "LDFLAGS=$LDFLAGS" >> $GITHUB_ENV | |
| echo "CPPFLAGS=$CPPFLAGS" >> $GITHUB_ENV | |
| - name: Verify macOS dependencies | |
| if: matrix.os == 'macos-latest' | |
| run: | | |
| echo "OpenSSL location:" | |
| brew --prefix openssl@3 | |
| echo "librdkafka location:" | |
| brew --prefix librdkafka | |
| echo "PKG_CONFIG_PATH:" | |
| echo $PKG_CONFIG_PATH | |
| echo "LDFLAGS:" | |
| echo $LDFLAGS | |
| echo "CPPFLAGS:" | |
| echo $CPPFLAGS | |
| echo "pkg-config test for librdkafka:" | |
| pkg-config --exists rdkafka && echo "rdkafka found" || echo "rdkafka not found" | |
| echo "pkg-config version for librdkafka:" | |
| pkg-config --modversion rdkafka || echo "rdkafka version not found" | |
| echo "Brew packages installed:" | |
| brew list openssl librdkafka pkg-config zstd lz4 | |
| - name: Install Rust | |
| uses: dtolnay/rust-toolchain@stable | |
| with: | |
| targets: ${{ matrix.target || matrix.target_x86 }} | |
| - name: Add macOS universal binary targets | |
| if: matrix.os == 'macos-latest' | |
| run: | | |
| rustup target add x86_64-apple-darwin aarch64-apple-darwin | |
| - name: Add Linux and Windows targets | |
| if: matrix.os != 'macos-latest' | |
| run: rustup target add ${{ matrix.target }} | |
| - name: Build with Cargo | |
| env: | |
| OPENSSL_DIR: ${{ env.OPENSSL_DIR }} | |
| PKG_CONFIG_PATH: ${{ env.PKG_CONFIG_PATH }} | |
| LDFLAGS: ${{ env.LDFLAGS }} | |
| CPPFLAGS: ${{ env.CPPFLAGS }} | |
| run: | | |
| if [[ "${{ matrix.os }}" == "macos-latest" ]]; then | |
| # Set additional environment variables for macOS build | |
| export MACOSX_DEPLOYMENT_TARGET=10.15 | |
| export CC=clang | |
| export CXX=clang++ | |
| # Debug: Print environment variables | |
| echo "OPENSSL_DIR: $OPENSSL_DIR" | |
| echo "PKG_CONFIG_PATH: $PKG_CONFIG_PATH" | |
| echo "LDFLAGS: $LDFLAGS" | |
| echo "CPPFLAGS: $CPPFLAGS" | |
| # Build for x86_64 only for now to test the fix | |
| echo "Building for x86_64 macOS target..." | |
| cargo build --release --target ${{ matrix.target_x86 }} | |
| # Copy the x86_64 binary as our macOS binary | |
| cp ./target/${{ matrix.target_x86 }}/release/kfcli ./target/release/macos_binary | |
| else | |
| cargo build --release --target ${{ matrix.target }} | |
| fi | |
| shell: bash | |
| - name: Set prerelease flag | |
| id: prerelease_check | |
| run: | | |
| if [[ "${GITHUB_REF#refs/tags/}" == *"alpha"* || "${GITHUB_REF#refs/tags/}" == *"beta"* ]]; then | |
| echo "prerelease=true" >> $GITHUB_ENV | |
| else | |
| echo "prerelease=false" >> $GITHUB_ENV | |
| fi | |
| - name: Check if release exists | |
| id: check_release | |
| run: | | |
| if gh release view "${GITHUB_REF_NAME}" &>/dev/null; then | |
| echo "release_exists=true" >> $GITHUB_ENV | |
| else | |
| echo "release_exists=false" >> $GITHUB_ENV | |
| fi | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} | |
| - name: Create a GitHub Release | |
| if: env.release_exists == 'false' | |
| id: create_release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: ${{ github.ref_name }} | |
| name: Release ${{ github.ref_name }} | |
| body: | | |
| This release includes the latest Rust binary for version ${{ github.ref_name }}. | |
| draft: false | |
| prerelease: ${{ env.prerelease }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} | |
| - name: Check if macOS binary exists | |
| if: matrix.os == 'macos-latest' | |
| run: ls -la ./target/release/macos_binary | |
| - name: Upload macOS Binary Release Asset | |
| if: matrix.os == 'macos-latest' | |
| run: | | |
| cp ./target/release/macos_binary ./kfcli-macos | |
| gh release upload ${{ github.ref_name }} ./kfcli-macos --clobber | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} | |
| - name: Upload Linux Release Asset | |
| if: matrix.os == 'ubuntu-latest' | |
| run: | | |
| cp ./target/${{ matrix.target }}/release/kfcli ./kfcli-linux | |
| gh release upload ${{ github.ref_name }} ./kfcli-linux --clobber | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} | |
| - name: Calculate SHA256 for macOS release asset | |
| if: matrix.os == 'macos-latest' | |
| id: calculate_sha | |
| run: | | |
| SHA256=$(shasum -a 256 ./target/release/macos_binary | awk '{print $1}') | |
| echo "sha256=$SHA256" >> $GITHUB_ENV | |
| - name: Trigger Homebrew Formula Update via cURL | |
| if: matrix.os == 'macos-latest' | |
| run: | | |
| URL="https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/kfcli-macos" | |
| JSON_PAYLOAD=$(jq -n --arg url "$URL" '{"event_type": "update-kcli-formula", "client_payload": {"tag": "${{ github.ref_name }}", "download_url": $url, "sha": "${{ env.sha256 }}"}}') | |
| echo "Payload: $JSON_PAYLOAD" | |
| curl -L \ | |
| -X POST \ | |
| -H "Accept: application/vnd.github+json" \ | |
| -H "Authorization: Bearer ${{ secrets.PAT_TOKEN }}" \ | |
| -H "X-GitHub-Api-Version: 2022-11-28" \ | |
| https://api.github.com/repos/keaz/homebrew-homebrew/dispatches \ | |
| -d "$JSON_PAYLOAD" | |