From 2b296198071b48736d04993981f9a27b5bd5b32c Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Mon, 6 Feb 2023 19:13:32 -0500 Subject: [PATCH 1/3] Add new cross-gem action --- .github/workflows/test-cross-gem.yml | 54 +++++++++++++++++++ cross-gem/action.yml | 60 ++++++++++++++++++++++ cross-gem/readme.md | 77 ++++++++++++++++++++++++++++ fetch-ci-data/readme.md | 13 +---- readme.md | 7 +++ setup-ruby-and-rust/action.yml | 27 +--------- 6 files changed, 201 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/test-cross-gem.yml create mode 100644 cross-gem/action.yml create mode 100644 cross-gem/readme.md diff --git a/.github/workflows/test-cross-gem.yml b/.github/workflows/test-cross-gem.yml new file mode 100644 index 0000000..29868cc --- /dev/null +++ b/.github/workflows/test-cross-gem.yml @@ -0,0 +1,54 @@ +--- +name: test-cross-gem +on: + push: + paths: + - "cross-gem/**/*" + - ".github/workflows/test-cross-gem.yml" +env: + SCCACHE_LOG: debug +jobs: + test: + name: Testing ${{ matrix.repo.slug }} + strategy: + fail-fast: false + matrix: + repo: + - name: "oxidize-rb/oxi-test" + slug: oxi-test + ref: main + ruby-versions: "2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2" + ruby-platform: + - "x86_64-linux" + - "arm64-darwin" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + repository: ${{ matrix.repo.name }} + ref: ${{ matrix.repo.ref }} + + - uses: actions/checkout@v3 + with: + path: ./tmp/actions + + - uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.1" + + - uses: ./tmp/actions/cross-gem + id: cross-gem + with: + ruby-versions: ${{ matrix.repo.ruby-versions }} + platform: ${{ matrix.ruby-platform }} + + - name: Run tests + run: | + : Run tests + + if ! $(find pkg -name "oxi-test-*-${{ matrix.ruby-platform }}.gem" | wc -l | grep -q 1); then + echo "Gem not found in pkg/ directory" + echo "Found:" + ls -l pkg + exit 1 + fi diff --git a/cross-gem/action.yml b/cross-gem/action.yml new file mode 100644 index 0000000..30ab666 --- /dev/null +++ b/cross-gem/action.yml @@ -0,0 +1,60 @@ +--- +name: "Cross Gem" +description: "A GitHub action to cross-compile a Ruby gem for multiple platforms using `rb-sys-dock`." +author: "Ian Ker-Seymer" +branding: + icon: "download-cloud" + color: "gray-dark" +inputs: + platform: + description: "The platform to cross-compile for (e.g. `x86_64-linux`)" + required: true + ruby-versions: + description: "The Ruby versions to cross-compile for (e.g. `2.7,3.0,3.1`)" + required: true + cache-version: + default: "v0" + description: "Arbitrary string that will be added to the cache key of the bundler cache. Set or change it if you need to invalidate the cache." + working-directory: + default: "." + description: "The working directory to run the action in." +outputs: + gem-path: + description: "The path to the cross-compiled gem" + value: ${{ steps.set-outputs.outputs.gem-path }} +runs: + using: "composite" + steps: + - name: Configure environment + shell: bash + run: | + : Configure environment + echo "RB_SYS_DOCK_UID=$(id -u)" >> $GITHUB_ENV + echo "RB_SYS_DOCK_GID=$(id -g)" >> $GITHUB_ENV + rb_sys_dock_cache_dir="$HOME/.cache/rb-sys-dock" + mkdir -p "$rb_sys_dock_cache_dir" + echo "RB_SYS_DOCK_CACHE_DIR=$rb_sys_dock_cache_dir" >> $GITHUB_ENV + + - name: Setup caching + uses: actions/cache@v3 + with: + path: | + ${{ env.RB_SYS_DOCK_CACHE_DIR }} + ${{ inputs.working-directory }}/tmp/rb-sys-dock/${{ inputs.platform }}/target + key: rb-sys-dock-${{ inputs.cache-version }}-${{ inputs.platform }}-${{ hashFiles('**/Gemfile.lock', '**/Cargo.lock') }} + restore-keys: | + rb-sys-dock-${{ inputs.cache-version }}-${{ inputs.platform }}- + + - name: Build gem + shell: bash + working-directory: ${{ inputs.working-directory }} + run: | + : Compile gem + bundle exec rb-sys-dock --platform "${{ inputs.platform }}" --ruby-versions "${{ inputs.ruby-versions }}" --build + + - name: Set outputs + id: set-outputs + shell: bash + run: | + : Set output + echo "gem-path=$(find ${{ inputs.working-directory }}/pkg -name '*-${{ inputs.platform }}.gem')" >> $GITHUB_OUTPUT diff --git a/cross-gem/readme.md b/cross-gem/readme.md new file mode 100644 index 0000000..107557f --- /dev/null +++ b/cross-gem/readme.md @@ -0,0 +1,77 @@ +# ⚡️ `oxidize-rb/actions/cross-gem` + +A GitHub action to cross-compile a Ruby gem for multiple platforms using +`rb-sys-dock`. + +## Example usage + +```yaml +--- +name: CI + +on: + push: + tags: + - "v*" + +jobs: + ci-data: + runs-on: ubuntu-latest + outputs: + result: ${{ steps.fetch.outputs.result }} + steps: + - uses: oxidize-rb/actions/fetch-ci-data@v1 + id: fetch + with: + supported-ruby-platforms: | + exclude: [arm-linux] + stable-ruby-versions: | + exclude: [head] + cross-gem: + name: Compile native gem for ${{ matrix.platform }} + runs-on: ubuntu-latest + needs: ci-data + strategy: + matrix: + platform: ${{ fromJSON(needs.ci-data.outputs.result).supported-ruby-platforms }} + steps: + - uses: actions/checkout@v2 + + - uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.1" + + - uses: oxidize-rb/actions/cross-gem@v1 + id: cross-gem + with: + platform: ${{ matrix.ruby-platform }} + ruby-versions: ${{ join(fromJSON(needs.ci-data.outputs.result).stable-ruby-versions, ',') }} + + - uses: actions/upload-artifact@v2 + with: + name: cross-gem + path: ${{ steps.cross-gem.outputs.gem-path }} +``` + +## Inputs + + + +| Name | Description | Default | +| --------------------- | -------------------------------------------------------------------------------------------------------------------------------- | ------- | +| **cache-version** | Arbitrary string that will be added to the cache key of the bundler cache. Set or change it if you need to invalidate the cache. | `v0` | +| **platform** | The platform to cross-compile for (e.g. `x86_64-linux`) | | +| **ruby-versions** | The Ruby versions to cross-compile for (e.g. `2.7,3.0,3.1`) | | +| **working-directory** | The working directory to run the action in. | `.` | + + + +## Outputs + + + +| Name | Description | +| ------------ | ---------------------------------- | +| **gem-path** | The path to the cross-compiled gem | + + diff --git a/fetch-ci-data/readme.md b/fetch-ci-data/readme.md index b73e8ba..0999fd2 100644 --- a/fetch-ci-data/readme.md +++ b/fetch-ci-data/readme.md @@ -45,15 +45,6 @@ name: Test on: push -jobs: - - test: - name: Test on Ruby ${{ matrix.ruby }} - needs: ci-data - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: jobs: ci-data: runs-on: ubuntu-latest @@ -78,12 +69,12 @@ jobs: - uses: ruby/setup-ruby@v1 with: - ruby-version: '3.1' + ruby-version: "3.1" - uses: oxidize-rb/cross-gem-action@v7 with: platform: ${{ matrix.platform }} - ruby-versions: '3.1, 3.0, 2.7' # optional + ruby-versions: "3.1, 3.0, 2.7" # optional - uses: actions/download-artifact@v3 with: diff --git a/readme.md b/readme.md index 3525856..608175e 100644 --- a/readme.md +++ b/readme.md @@ -11,6 +11,13 @@ testing native Rust gems. [📝 Read the Docs](./setup-ruby-and-rust/readme.md) +### `oxidize-rb/actions/cross-gem@v1` + +A GitHub action to cross-compile a Ruby gem for multiple platforms using +`rb-sys-dock`. + +[📝 Read the Docs](./cross-gem/readme.md) + ### `oxidize-rb/actions/fetch-ci-data@v1` A GitHub Action to query useful CI data for usage in a matrix, etc. diff --git a/setup-ruby-and-rust/action.yml b/setup-ruby-and-rust/action.yml index 1735ddc..23b75bc 100644 --- a/setup-ruby-and-rust/action.yml +++ b/setup-ruby-and-rust/action.yml @@ -133,13 +133,7 @@ runs: echo "base-cache-key-level-2=$base_cache_level_2" >> $GITHUB_OUTPUT echo "base-cache-key-level-3=$base_cache_level_3" >> $GITHUB_OUTPUT - ext_cache_level_1="${base_cache_level_1}__${{ hashFiles('**/extconf.rb') }}" - ext_cache_level_2="${ext_cache_level_1}__${{ hashFiles('**/Gemfile') }}" - ext_cache_level_3="${ext_cache_level_2}__${{ hashFiles('**/Gemfile.lock') }}" - echo "ext-cache-key-level-1=$ext_cache_level_1" >> $GITHUB_OUTPUT - echo "ext-cache-key-level-2=$ext_cache_level_2" >> $GITHUB_OUTPUT - echo "ext-cache-key-level-3=$ext_cache_level_3" >> $GITHUB_OUTPUT - echo "cache-key=$ext_cache_level_3" >> $GITHUB_OUTPUT + echo "cache-key=$base_cache_level_3" >> $GITHUB_OUTPUT ruby --disable-gems $GITHUB_ACTION_PATH/helpers/cargo_registry_cache_keys.rb @@ -159,12 +153,6 @@ runs: echo "CARGO_INCREMENTAL=0" >> $GITHUB_ENV - echo "RB_SYS_DOCK_UID=$(id -u)" >> $GITHUB_ENV - echo "RB_SYS_DOCK_GID=$(id -g)" >> $GITHUB_ENV - rb_sys_dock_cache_dir="$HOME/.cache/rb-sys-dock" - mkdir -p "$rb_sys_dock_cache_dir" - echo "RB_SYS_DOCK_CACHE_DIR=$rb_sys_dock_cache_dir" >> $GITHUB_ENV - echo "RUBY_VERSION=$(rbconfig ruby_version)" >> $GITHUB_ENV if [ "${{ inputs.debug }}" = "true" ]; then @@ -207,19 +195,6 @@ runs: ${{ steps.set-outputs.outputs.base-cache-key-level-2 }} ${{ steps.set-outputs.outputs.base-cache-key-level-1 }} - - name: Setup ext cargo cache - uses: actions/cache@v3 - if: steps.set-outputs.outputs.cargo-cache == 'tarball' - with: - key: ${{ steps.set-outputs.outputs.ext-cache-key-level-3 }} - path: | - **/tmp/${{ steps.set-outputs.outputs.ruby-platform }}/ - ${{ inputs.cargo-cache-extra-path }} - ${{ env.RB_SYS_DOCK_CACHE_DIR }} - restore-keys: | - ${{ steps.set-outputs.outputs.ext-cache-key-level-2 }} - ${{ steps.set-outputs.outputs.ext-cache-key-level-1 }} - - name: Clean the cargo cache if: inputs.cargo-cache-clean == 'true' && steps.set-outputs.outputs.cargo-cache == 'tarball' uses: oxidize-rb/actions/post-run@v1 From 0ef9ecef14996f16fd3efeb5f145d9eb8d647c35 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Mon, 6 Feb 2023 19:17:54 -0500 Subject: [PATCH 2/3] Install rb_sys if needed --- cross-gem/action.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cross-gem/action.yml b/cross-gem/action.yml index 30ab666..118998c 100644 --- a/cross-gem/action.yml +++ b/cross-gem/action.yml @@ -50,7 +50,12 @@ runs: working-directory: ${{ inputs.working-directory }} run: | : Compile gem - bundle exec rb-sys-dock --platform "${{ inputs.platform }}" --ruby-versions "${{ inputs.ruby-versions }}" --build + if bundle info rb_sys > /dev/null; then + bundle exec rb-sys-dock --platform "${{ inputs.platform }}" --ruby-versions "${{ inputs.ruby-versions }}" --build + else + gem install rb_sys + rb-sys-dock --platform "${{ inputs.platform }}" --ruby-versions "${{ inputs.ruby-versions }}" --build + fi - name: Set outputs id: set-outputs From c0c689bdc3b1a25bbac6fa834ce9cb6f96de7430 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Mon, 6 Feb 2023 19:21:09 -0500 Subject: [PATCH 3/3] Log the platform --- .github/workflows/test-cross-gem.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-cross-gem.yml b/.github/workflows/test-cross-gem.yml index 29868cc..d2c25cd 100644 --- a/.github/workflows/test-cross-gem.yml +++ b/.github/workflows/test-cross-gem.yml @@ -9,7 +9,7 @@ env: SCCACHE_LOG: debug jobs: test: - name: Testing ${{ matrix.repo.slug }} + name: Testing ${{ matrix.repo.slug }} on ${{ matrix.ruby-platform }} strategy: fail-fast: false matrix: