Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oxidize-rb/actions/cross-gem #14

Merged
merged 3 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .github/workflows/test-cross-gem.yml
Original file line number Diff line number Diff line change
@@ -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 }} on ${{ matrix.ruby-platform }}
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
65 changes: 65 additions & 0 deletions cross-gem/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
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
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
shell: bash
run: |
: Set output
echo "gem-path=$(find ${{ inputs.working-directory }}/pkg -name '*-${{ inputs.platform }}.gem')" >> $GITHUB_OUTPUT
77 changes: 77 additions & 0 deletions cross-gem/readme.md
Original file line number Diff line number Diff line change
@@ -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

<!-- 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. | `.` |

<!-- /inputs -->

## Outputs

<!-- outputs -->

| Name | Description |
| ------------ | ---------------------------------- |
| **gem-path** | The path to the cross-compiled gem |

<!-- /outputs -->
13 changes: 2 additions & 11 deletions fetch-ci-data/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 1 addition & 26 deletions setup-ruby-and-rust/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down