Skip to content
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
186 changes: 25 additions & 161 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,189 +1,53 @@
name: CI

env:
DEBUG: napi:*
APP_NAME: cinnamon
MACOSX_DEPLOYMENT_TARGET: '10.13'

permissions:
contents: write
id-token: write

on:
push:
tags:
- '[0-9]+.*'
branches: [ "main", "master" ]
tags: [ "*.*.*" ]
pull_request:
branches: [ "main", "master" ]
workflow_dispatch:

jobs:
build:
name: Build - ${{ matrix.settings.target }}
runs-on: ${{ matrix.settings.host }}
strategy:
fail-fast: false
matrix:
settings:
# MacOS (Apple Silicon)
- host: macos-latest
target: aarch64-apple-darwin
build: |
npm run build -- --target aarch64-apple-darwin
strip -x *.node

# Windows (x64 MSVC)
- host: windows-latest
target: x86_64-pc-windows-msvc
build: npm run build -- --target x86_64-pc-windows-msvc
env:
CARGO_TERM_COLOR: always

# Linux (x64 GNU) - Uses Docker
- host: ubuntu-latest
target: x86_64-unknown-linux-gnu
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian
build: |-
rustup update stable &&
npm run build -- --target x86_64-unknown-linux-gnu &&
strip *.node
jobs:
check:
name: Test & Lint
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup node
uses: actions/setup-node@v4
if: ${{ !matrix.settings.docker }}
with:
node-version: 20
cache: npm

- name: Install Rust
- name: Install Rust Toolchain
uses: dtolnay/rust-toolchain@stable
if: ${{ !matrix.settings.docker }}
with:
toolchain: stable
targets: ${{ matrix.settings.target }}
components: rustfmt, clippy

- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ matrix.settings.target }}-cargo-${{ matrix.settings.host }}
- name: Cache Cargo dependencies
uses: Swatinem/rust-cache@v2

- name: Install dependencies
run: npm install
- name: Check Formatting
run: cargo fmt --all -- --check

- name: Build (Docker)
if: ${{ matrix.settings.docker }}
uses: addnab/docker-run-action@v3
with:
image: ${{ matrix.settings.docker }}
options: --user 0:0 -v ${{ github.workspace }}/.cargo-cache/git:/root/.cargo/git -v ${{ github.workspace }}/.cargo-cache/registry:/root/.cargo/registry -v ${{ github.workspace }}:/build -w /build
run: ${{ matrix.settings.build }}
- name: Run Clippy
run: cargo clippy --all-targets --all-features -- -D warnings

- name: Build (Host)
if: ${{ !matrix.settings.docker }}
run: ${{ matrix.settings.build }}
shell: bash

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: bindings-${{ matrix.settings.target }}
path: ${{ env.APP_NAME }}.*.node
if-no-files-found: error
- name: Run Tests
run: cargo test --verbose

publish:
name: Publish to NPM & Crates.io
runs-on: ubuntu-latest
needs: build
name: Publish to Crates.io
needs: check
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- name: Install Rust
- name: Install Rust Toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable

- name: Install dependencies
run: npm install

- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts

- name: Create npm dirs
run: npx napi create-npm-dirs

- name: Move artifacts to npm folders
run: npm run artifacts

- name: Publish to NPM
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN

# Helper function to check if package exists
# Returns "true" if version exists, "false" if not
function version_exists() {
local name=$1
local version=$2
# npm view returns the version string if found, or empty/error if not
if [ "$(npm view "${name}@${version}" version 2>/dev/null)" == "$version" ]; then
echo "true"
else
echo "false"
fi
}

# 1. Publish sub-packages
for package_dir in npm/*; do
if [ -d "$package_dir" ]; then
cd $package_dir

# Get name and version from package.json in the subdir
PKG_NAME=$(node -p "require('./package.json').name")
PKG_VERSION=$(node -p "require('./package.json').version")

if [ "$(version_exists $PKG_NAME $PKG_VERSION)" == "true" ]; then
echo "$PKG_NAME@$PKG_VERSION already exists on NPM. Skipping."
else
echo "Publishing $PKG_NAME@$PKG_VERSION..."
npm publish --access public --provenance
fi

cd ../..
fi
done

# 2. Publish root package
ROOT_NAME=$(node -p "require('./package.json').name")
ROOT_VERSION=$(node -p "require('./package.json').version")

if [ "$(version_exists $ROOT_NAME $ROOT_VERSION)" == "true" ]; then
echo "$ROOT_NAME@$ROOT_VERSION already exists on NPM. Skipping."
else
echo "Publishing root package $ROOT_NAME@$ROOT_VERSION..."
npm publish --access public --provenance
fi

- name: Publish to Crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
if [ -n "$CARGO_REGISTRY_TOKEN" ]; then
echo "Publishing to Crates.io..."
cargo publish --allow-dirty
else
echo "Skipping Crates.io (Token not found)"
fi
run: cargo publish
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ target/
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# NAPI-RS Build Artifacts
node_modules
*.node
index.js
index.d.ts

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
Expand Down
Loading