# Reusable workflow that builds a native Parcel plugin — Rust, Go, or Swift — for every # target listed in its package.json, and publishes the results to npm. # # See .github/scripts/native-plugin/README.md for usage. name: native-plugin on: workflow_call: inputs: paths: description: | Newline or comma separated plugin directories, for a monorepo publishing several at once. Building them in one run is what lets publishing wait for all of them, instead of pushing half a release when one fails to build. required: false default: '' type: string publish: description: 'Publish to npm once every target has built successfully' required: false default: false type: boolean version: description: | Version to publish, in place of the one in each package.json. For releasing plugins in lockstep with something else, such as Parcel itself. required: false default: '' type: string tag: description: 'npm dist-tag to publish under. Defaults to the prerelease identifier (e.g. alpha), or latest for a stable version' required: false default: '' type: string language: description: 'Toolchain that builds the plugin: rust, go, or swift. Detected from Cargo.toml / go.mod / Package.swift when empty.' required: false default: '' type: string rust-toolchain: description: 'Rust toolchain to build with' required: false default: 'stable' type: string go-version: description: 'Go version to build with' required: false default: 'stable' type: string go-args: description: 'Extra arguments passed to go build (e.g. -tags foo)' required: false default: '' type: string swift-version: description: 'Swift version for Linux builds: a tag of the official swift Docker image, used as swift:-amazonlinux2' required: false default: '6.2' type: string xcode-version: description: 'Xcode version for macOS Swift builds, as accepted by maxim-lobanov/setup-xcode' required: false default: 'latest-stable' type: string swift-product: description: 'Dynamic library product to build, when Package.swift declares more than one' required: false default: '' type: string swift-args: description: 'Extra arguments passed to swift build (e.g. -Xswiftc -DFOO)' required: false default: '' type: string cargo-args: description: 'Extra arguments passed to cargo build (e.g. --features foo)' required: false default: '' type: string rustflags: description: 'Extra RUSTFLAGS. Note that setting this shadows build.rustflags in .cargo/config.toml, as musl builds already do.' required: false default: '' type: string node-version: description: 'Node.js version used to run the packaging scripts and npm publish' required: false default: '24' type: string zig-version: description: 'Zig version used to cross compile Linux targets' required: false default: '0.14.0' type: string outputs: name: description: 'Name of the plugin package' value: ${{ jobs.matrix.outputs.name }} version: description: 'Version that was built (and published, if enabled)' value: ${{ jobs.matrix.outputs.version }} language: description: 'Toolchain the plugin was built with' value: ${{ jobs.matrix.outputs.language }} jobs: matrix: name: Read targets runs-on: ubuntu-latest permissions: contents: read outputs: matrix: ${{ steps.matrix.outputs.matrix }} plugins: ${{ steps.matrix.outputs.plugins }} name: ${{ steps.matrix.outputs.name }} version: ${{ steps.matrix.outputs.version }} language: ${{ steps.matrix.outputs.language }} steps: - uses: actions/checkout@v4 # job.workflow_repository/job.workflow_sha identify this reusable workflow # file, so the scripts are always the ones belonging to the ref the caller # pinned - including when a fork is being tested. - name: Check out packaging scripts uses: actions/checkout@v4 with: repository: ${{ job.workflow_repository }} ref: ${{ job.workflow_sha }} path: .parcel-native-plugin-scripts sparse-checkout: .github/scripts/native-plugin sparse-checkout-cone-mode: false filter: blob:none - name: Move packaging scripts out of the workspace shell: bash run: | set -euo pipefail mkdir -p "$RUNNER_TEMP/native-plugin" mv .parcel-native-plugin-scripts/.github/scripts/native-plugin/* "$RUNNER_TEMP/native-plugin/" rm -rf .parcel-native-plugin-scripts - uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} - name: Compute build matrix id: matrix shell: bash env: PLUGIN_DIRS: ${{ inputs.paths }} LANGUAGE: ${{ inputs.language }} PARCEL_PLUGIN_VERSION: ${{ inputs.version }} run: node "$RUNNER_TEMP/native-plugin/matrix.mjs" --dirs "$PLUGIN_DIRS" --language "$LANGUAGE" build: name: ${{ matrix.plugin }} (${{ matrix.target }}) needs: matrix runs-on: ${{ matrix.runner }} permissions: contents: read strategy: fail-fast: false matrix: ${{ fromJson(needs.matrix.outputs.matrix) }} steps: - uses: actions/checkout@v4 - name: Check out packaging scripts uses: actions/checkout@v4 with: repository: ${{ job.workflow_repository }} ref: ${{ job.workflow_sha }} path: .parcel-native-plugin-scripts sparse-checkout: .github/scripts/native-plugin sparse-checkout-cone-mode: false filter: blob:none - name: Move packaging scripts out of the workspace shell: bash run: | set -euo pipefail mkdir -p "$RUNNER_TEMP/native-plugin" mv .parcel-native-plugin-scripts/.github/scripts/native-plugin/* "$RUNNER_TEMP/native-plugin/" rm -rf .parcel-native-plugin-scripts - uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} - name: Install Rust if: ${{ matrix.language == 'rust' }} uses: dtolnay/rust-toolchain@master with: toolchain: ${{ inputs.rust-toolchain }} targets: ${{ matrix.target }} - if: ${{ matrix.language == 'rust' }} uses: Swatinem/rust-cache@v2 with: workspaces: ${{ matrix.dir }} shared-key: ${{ matrix.plugin }}-${{ matrix.target }} - name: Install Go if: ${{ matrix.language == 'go' }} uses: actions/setup-go@v5 with: go-version: ${{ inputs.go-version }} cache-dependency-path: ${{ matrix.dir }}/go.sum # Linux targets are cross compiled with Zig so that glibc builds link # against an old enough glibc to run on distros older than the runner. # Rust reaches it through cargo-zigbuild; cgo uses `zig cc` as its compiler. - name: Install Zig if: ${{ matrix.builder == 'zig' }} uses: goto-bus-stop/setup-zig@v1 with: version: ${{ inputs.zig-version }} - name: Install cargo-zigbuild if: ${{ matrix.builder == 'zig' && matrix.language == 'rust' }} uses: taiki-e/install-action@v2 env: GITHUB_TOKEN: ${{ github.token }} with: tool: cargo-zigbuild - name: Build (Rust) if: ${{ matrix.language == 'rust' }} shell: bash working-directory: ${{ matrix.dir }} env: TARGET: ${{ matrix.target }} ZIG_TARGET: ${{ matrix.zigTarget }} BUILDER: ${{ matrix.builder }} CARGO_ARGS: ${{ inputs.cargo-args }} EXTRA_RUSTFLAGS: ${{ inputs.rustflags }} ${{ matrix.rustflags }} EXT: ${{ matrix.ext }} run: | set -euo pipefail # Setting RUSTFLAGS at all shadows build.rustflags from .cargo/config.toml, # so only export it when a target actually needs extra flags. if [ -n "$(printf '%s' "$EXTRA_RUSTFLAGS" | tr -d '[:space:]')" ]; then export RUSTFLAGS="$EXTRA_RUSTFLAGS" echo "RUSTFLAGS=$RUSTFLAGS" fi # The JSON message stream says exactly which file cargo produced; # diagnostics still go to stderr and stay readable. if [ "$BUILDER" = 'zig' ]; then cargo zigbuild --release --target "$ZIG_TARGET" --message-format json-render-diagnostics $CARGO_ARGS > "$RUNNER_TEMP/cargo-messages.json" else cargo build --release --target "$TARGET" --message-format json-render-diagnostics $CARGO_ARGS > "$RUNNER_TEMP/cargo-messages.json" fi LIBRARY="$(node "$RUNNER_TEMP/native-plugin/cargo-library.mjs" --dir . --messages "$RUNNER_TEMP/cargo-messages.json" --ext "$EXT")" echo "LIBRARY=$LIBRARY" >> "$GITHUB_ENV" - name: Build (Go) if: ${{ matrix.language == 'go' }} shell: bash working-directory: ${{ matrix.dir }} env: GOOS: ${{ matrix.goos }} GOARCH: ${{ matrix.goarch }} # Set as a step env rather than interpolated into the script: it carries # spaces (`zig cc -target ...`) and would need quoting in every shell. TARGET_CC: ${{ matrix.cc }} GO_ARGS: ${{ inputs.go-args }} EXT: ${{ matrix.ext }} run: | set -euo pipefail # c-shared is only possible through cgo, so a missing or wrong compiler # has to fail here rather than silently produce a library with no entry # points. export CGO_ENABLED=1 if [ -n "$TARGET_CC" ]; then export CC="$TARGET_CC" fi echo "CC=${CC:-}" LIBRARY="$RUNNER_TEMP/plugin.$EXT" go build -buildmode=c-shared -o "$LIBRARY" $GO_ARGS . echo "LIBRARY=$LIBRARY" >> "$GITHUB_ENV" # The Swift SDK needs a newer toolchain than some runner images default to. - name: Select Xcode if: ${{ matrix.language == 'swift' && matrix.builder == 'native' }} uses: maxim-lobanov/setup-xcode@v1 with: xcode-version: ${{ inputs.xcode-version }} - name: Build (Swift, macOS) if: ${{ matrix.language == 'swift' && matrix.builder == 'native' }} shell: bash working-directory: ${{ matrix.dir }} env: ARCH: ${{ matrix.swiftArch }} SWIFT_PRODUCT: ${{ inputs.swift-product }} SWIFT_ARGS: ${{ inputs.swift-args }} run: | set -euo pipefail swift --version swift package dump-package > "$RUNNER_TEMP/swift-package.json" PRODUCT="$(node "$RUNNER_TEMP/native-plugin/swift-product.mjs" --manifest "$RUNNER_TEMP/swift-package.json" --product "$SWIFT_PRODUCT")" # The other architecture is a cross build; macros still build for the host. swift build -c release --product "$PRODUCT" --arch "$ARCH" $SWIFT_ARGS BIN="$(swift build -c release --product "$PRODUCT" --arch "$ARCH" $SWIFT_ARGS --show-bin-path)" # macOS ships the Swift runtime, so the library links the system's. LIBRARY="$BIN/lib$PRODUCT.dylib" lipo -info "$LIBRARY" echo "LIBRARY=$LIBRARY" >> "$GITHUB_ENV" # Built natively in the official Amazon Linux 2 image rather than cross # compiled: its glibc 2.26 is the floor Rust and Go builds target through # Zig, so the library runs on the same distributions. - name: Build (Swift, Linux) if: ${{ matrix.language == 'swift' && matrix.builder == 'docker' }} shell: bash working-directory: ${{ matrix.dir }} env: IMAGE: swift:${{ inputs.swift-version }}-amazonlinux2 PLATFORM: ${{ matrix.swiftPlatform }} SWIFT_PRODUCT: ${{ inputs.swift-product }} SWIFT_ARGS: ${{ inputs.swift-args }} run: | set -euo pipefail # The whole checkout is mounted at the same path, so path dependencies # elsewhere in the repository resolve, and paths swift prints are valid # out here too. swift() { docker run --rm --platform "$PLATFORM" \ -v "$GITHUB_WORKSPACE:$GITHUB_WORKSPACE" -w "$PWD" \ "$IMAGE" swift "$@" } swift --version swift package dump-package > "$RUNNER_TEMP/swift-package.json" PRODUCT="$(node "$RUNNER_TEMP/native-plugin/swift-product.mjs" --manifest "$RUNNER_TEMP/swift-package.json" --product "$SWIFT_PRODUCT")" # Linux has no system Swift runtime, so the plugin carries its own. swift build -c release --product "$PRODUCT" -Xswiftc -static-stdlib $SWIFT_ARGS BIN="$(swift build -c release --product "$PRODUCT" -Xswiftc -static-stdlib $SWIFT_ARGS --show-bin-path)" LIBRARY="$BIN/lib$PRODUCT.so" # A library that still needs the toolchain's shared runtime would load # on this image and nowhere else. docker run --rm --platform "$PLATFORM" -v "$GITHUB_WORKSPACE:$GITHUB_WORKSPACE" "$IMAGE" ldd "$LIBRARY" | tee "$RUNNER_TEMP/ldd.txt" if grep -E 'libswift|libFoundation|libdispatch|libBlocksRuntime|not found' "$RUNNER_TEMP/ldd.txt"; then echo "::error::lib$PRODUCT.so links the Swift runtime dynamically; it must be linked statically to load outside the build image." exit 1 fi echo "LIBRARY=$LIBRARY" >> "$GITHUB_ENV" - name: Pack ${{ matrix.package }} shell: bash env: PLUGIN_DIR: ${{ matrix.dir }} TARGET: ${{ matrix.target }} PARCEL_PLUGIN_VERSION: ${{ inputs.version }} run: | node "$RUNNER_TEMP/native-plugin/pack-artifact.mjs" \ --dir "$PLUGIN_DIR" \ --target "$TARGET" \ --library "$LIBRARY" \ --out "$RUNNER_TEMP/artifacts" # Namespaced by plugin: several plugins in one run would otherwise collide # on both the artifact name and the .tgz inside it. - uses: actions/upload-artifact@v4 with: name: native-artifact-${{ matrix.plugin }}-${{ matrix.target }} path: ${{ runner.temp }}/artifacts/${{ matrix.target }}.tgz if-no-files-found: error pack: name: Pack ${{ matrix.name }} needs: [matrix, build] runs-on: ubuntu-latest permissions: contents: read strategy: fail-fast: false matrix: ${{ fromJson(needs.matrix.outputs.plugins) }} steps: - uses: actions/checkout@v4 - name: Check out packaging scripts uses: actions/checkout@v4 with: repository: ${{ job.workflow_repository }} ref: ${{ job.workflow_sha }} path: .parcel-native-plugin-scripts sparse-checkout: .github/scripts/native-plugin sparse-checkout-cone-mode: false filter: blob:none - name: Move packaging scripts out of the workspace shell: bash run: | set -euo pipefail mkdir -p "$RUNNER_TEMP/native-plugin" mv .parcel-native-plugin-scripts/.github/scripts/native-plugin/* "$RUNNER_TEMP/native-plugin/" rm -rf .parcel-native-plugin-scripts - uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} # This plugin's artifacts only. Merging them is safe here because the names # inside are .tgz, unique within a plugin. - name: Download artifact packages uses: actions/download-artifact@v4 with: pattern: native-artifact-${{ matrix.plugin }}-* path: ${{ runner.temp }}/artifacts merge-multiple: true # Also verifies that every target declared in parcel.artifacts produced a # tarball, before anything is published. - name: Pack plugin package shell: bash env: PLUGIN_DIR: ${{ matrix.dir }} PARCEL_PLUGIN_VERSION: ${{ inputs.version }} run: | node "$RUNNER_TEMP/native-plugin/pack-main.mjs" \ --dir "$PLUGIN_DIR" \ --artifacts "$RUNNER_TEMP/artifacts" \ --out "$RUNNER_TEMP/main" - uses: actions/upload-artifact@v4 with: name: native-plugin-main-${{ matrix.plugin }} path: ${{ runner.temp }}/main/*.tgz if-no-files-found: error publish: name: Publish to npm if: ${{ inputs.publish }} needs: [matrix, pack] runs-on: ubuntu-latest permissions: contents: read id-token: write # npm trusted publishing steps: - name: Check out packaging scripts uses: actions/checkout@v4 with: repository: ${{ job.workflow_repository }} ref: ${{ job.workflow_sha }} path: .parcel-native-plugin-scripts sparse-checkout: .github/scripts/native-plugin sparse-checkout-cone-mode: false filter: blob:none - name: Move packaging scripts out of the workspace shell: bash run: | set -euo pipefail mkdir -p "$RUNNER_TEMP/native-plugin" mv .parcel-native-plugin-scripts/.github/scripts/native-plugin/* "$RUNNER_TEMP/native-plugin/" rm -rf .parcel-native-plugin-scripts - uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} registry-url: 'https://registry.npmjs.org' # Trusted publishing (and the provenance attestation it generates) # requires npm 11.5.1 or later. - name: Update npm shell: bash run: npm install -g npm@latest # Left unmerged: across plugins the tarballs inside share names, so each # artifact keeps its own subdirectory. - name: Download artifact packages uses: actions/download-artifact@v4 with: pattern: native-artifact-* path: ${{ runner.temp }}/artifacts - name: Download plugin packages uses: actions/download-artifact@v4 with: pattern: native-plugin-main-* path: ${{ runner.temp }}/main # Platform packages go first, across every plugin: a plugin package's # optionalDependencies point at exact versions of its own artifacts, so it # must never be resolvable before they are. Ordering all artifacts ahead of # all plugin packages satisfies that for each of them. - name: Publish artifact packages shell: bash env: TAG: ${{ inputs.tag }} run: node "$RUNNER_TEMP/native-plugin/publish.mjs" --tag "$TAG" "$RUNNER_TEMP"/artifacts/*/*.tgz - name: Publish plugin packages shell: bash env: TAG: ${{ inputs.tag }} run: node "$RUNNER_TEMP/native-plugin/publish.mjs" --tag "$TAG" "$RUNNER_TEMP"/main/*/*.tgz