|
| 1 | +name: Build and Publish Native Ruby Gem |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +jobs: |
| 7 | + build-shared-library-binaries: |
| 8 | + strategy: |
| 9 | + matrix: |
| 10 | + os: [ubuntu-latest, macos-13, windows-latest] |
| 11 | + include: |
| 12 | + # Config for Linux |
| 13 | + - os: ubuntu-latest |
| 14 | + platform_tasks: "compile:aarch64-linux compile:x86_64-linux" |
| 15 | + artifact_name: "linux-binaries" |
| 16 | + artifact_path: "spannerlib/wrappers/spannerlib-ruby/lib/spannerlib/*-linux/" |
| 17 | + |
| 18 | + # Config for macOS (Apple Silicon + Intel) |
| 19 | + # Note: macos-13 is an Intel runner (x86_64) but can cross-compile to Apple Silicon (aarch64) |
| 20 | + - os: macos-13 |
| 21 | + platform_tasks: "compile:aarch64-darwin compile:x86_64-darwin" |
| 22 | + artifact_name: "darwin-binaries" |
| 23 | + artifact_path: "spannerlib/wrappers/spannerlib-ruby/lib/spannerlib/*-darwin/" |
| 24 | + |
| 25 | + # Config for Windows |
| 26 | + - os: windows-latest |
| 27 | + platform_tasks: "compile:x64-mingw32" |
| 28 | + artifact_name: "windows-binaries" |
| 29 | + artifact_path: "spannerlib/wrappers/spannerlib-ruby/lib/spannerlib/x64-mingw32/" |
| 30 | + |
| 31 | + runs-on: ${{ matrix.os }} |
| 32 | + |
| 33 | + steps: |
| 34 | + - name: Checkout code |
| 35 | + uses: actions/checkout@v4 |
| 36 | + |
| 37 | + - name: Set up Go |
| 38 | + uses: actions/setup-go@v5 |
| 39 | + with: |
| 40 | + go-version: 1.25.x |
| 41 | + |
| 42 | + - name: Set up Ruby |
| 43 | + uses: ruby/setup-ruby@v1 |
| 44 | + with: |
| 45 | + ruby-version: '3.3' |
| 46 | + bundler-cache: true |
| 47 | + working-directory: 'spannerlib/wrappers/spannerlib-ruby' |
| 48 | + |
| 49 | + - name: Install cross-compilers (Linux) |
| 50 | + if: matrix.os == 'ubuntu-latest' |
| 51 | + run: | |
| 52 | + sudo apt-get update |
| 53 | + # GCC cross toolchain and binutils for aarch64 |
| 54 | + sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu |
| 55 | + # Mingw-w64 cross toolchain for Windows target (x86_64) |
| 56 | + sudo apt-get install -y gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64 |
| 57 | + echo "=== cross compiler versions ===" |
| 58 | + aarch64-linux-gnu-gcc --version || true |
| 59 | + aarch64-linux-gnu-g++ --version || true |
| 60 | + x86_64-w64-mingw32-gcc --version || true |
| 61 | + which aarch64-linux-gnu-gcc || true |
| 62 | + which x86_64-w64-mingw32-gcc || true |
| 63 | +
|
| 64 | + # The cross-compiler step for macOS is removed, |
| 65 | + # as the built-in Clang on macOS runners can handle both Intel and ARM. |
| 66 | + |
| 67 | + - name: Compile Binaries |
| 68 | + working-directory: spannerlib/wrappers/spannerlib-ruby |
| 69 | + run: | |
| 70 | + # This runs the specific Rake tasks for this OS |
| 71 | + # e.g., "rake compile:aarch64-linux compile:x86_64-linux" |
| 72 | + bundle exec rake ${{ matrix.platform_tasks }} |
| 73 | +
|
| 74 | + - name: Upload Binaries as Artifact |
| 75 | + uses: actions/upload-artifact@v4 |
| 76 | + with: |
| 77 | + name: ${{ matrix.artifact_name }} |
| 78 | + path: ${{ matrix.artifact_path }} |
| 79 | + |
| 80 | + publish: |
| 81 | + name: Package and Publish Gem |
| 82 | + # This job runs only after all 'build' jobs have succeeded |
| 83 | + needs: build-shared-library-binaries |
| 84 | + runs-on: ubuntu-latest |
| 85 | + |
| 86 | + # This gives the job permission to publish to RubyGems |
| 87 | + permissions: |
| 88 | + id-token: write |
| 89 | + contents: read |
| 90 | + |
| 91 | + steps: |
| 92 | + - name: Checkout code |
| 93 | + uses: actions/checkout@v4 |
| 94 | + |
| 95 | + - name: Set up Ruby |
| 96 | + uses: ruby/setup-ruby@v1 |
| 97 | + with: |
| 98 | + ruby-version: '3.3' |
| 99 | + bundler-cache: true |
| 100 | + working-directory: 'spannerlib/wrappers/spannerlib-ruby' |
| 101 | + |
| 102 | + - name: Download all binaries |
| 103 | + uses: actions/download-artifact@v4 |
| 104 | + with: |
| 105 | + # No name means it downloads ALL artifacts from this workflow |
| 106 | + # The binaries will be placed in their original paths |
| 107 | + path: spannerlib/wrappers/spannerlib-ruby/lib/spannerlib/ |
| 108 | + |
| 109 | + - name: List downloaded files (for debugging) |
| 110 | + run: ls -R spannerlib/wrappers/spannerlib-ruby/lib/spannerlib/ |
| 111 | + |
| 112 | + - name: Build Gem |
| 113 | + working-directory: spannerlib/wrappers/spannerlib-ruby |
| 114 | + run: gem build spannerlib-ruby.gemspec |
| 115 | + |
| 116 | + - name: Publish to RubyGems |
| 117 | + working-directory: spannerlib/wrappers/spannerlib-ruby |
| 118 | + run: | |
| 119 | + # Make all built .gem files available to be pushed |
| 120 | + mkdir -p $HOME/.gem |
| 121 | + touch $HOME/.gem/credentials |
| 122 | + chmod 0600 $HOME/.gem/credentials |
| 123 | +
|
| 124 | + # This uses the new "Trusted Publishing" feature. |
| 125 | + # https://guides.rubygems.org/publishing/#publishing-with-github-actions |
| 126 | + printf -- "---\n:rubygems_api_key: Bearer ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials |
| 127 | +
|
| 128 | + # Push the gem |
| 129 | + gem push *.gem |
| 130 | + env: |
| 131 | + GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }} |
| 132 | + |
0 commit comments