Skip to content
Closed
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
222 changes: 222 additions & 0 deletions .github/workflows/publish_crates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

name: Publish crates

on:
workflow_dispatch:
inputs:
tag:
description: "Tag to publish (manual run, e.g. v0.1.15, v0.1.15-rc0 or v0.1.14-post1)"
required: true
type: string
dry_run:
description: "Build and verify crates without publishing to crates.io"
required: true
type: boolean
default: true

permissions:
contents: read

concurrency:
group: publish-crates-${{ inputs.tag }}
cancel-in-progress: false

env:
CARGO_TERM_COLOR: always
RELEASE_TAG: ${{ inputs.tag }}

jobs:
build_crates:
name: ${{ matrix.os }} (${{ matrix.arch }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
# Keep the OS/architecture matrix in sync with publish_wheel.yml.
include:
- {os: ubuntu-latest, arch: x86_64}
- {os: ubuntu-24.04-arm, arch: aarch64}
- {os: windows-latest, arch: AMD64}
- {os: macos-14, arch: arm64}

steps:
- name: Check out release source
uses: actions/checkout@v5
with:
ref: refs/tags/${{ inputs.tag }}
submodules: recursive
fetch-depth: 0
fetch-tags: true

- name: Set up uv
uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
with:
python-version: "3.13"
activate-environment: true

# Workspace publishing and dry-runs require Cargo 1.90 or newer.
- name: Set up Rust
run: rustup toolchain install stable --profile minimal

- uses: ./.github/actions/detect-env-vars
id: env_vars

# tvm-ffi-sys locates libtvm_ffi through tvm-ffi-config on PATH.
- name: Build and install TVM FFI
env:
CMAKE_BUILD_PARALLEL_LEVEL: ${{ steps.env_vars.outputs.cpu_count }}
run: uv pip install --verbose .

# Apply the release version after the Python build so setuptools-scm sees a clean tree.
- name: Set crate versions from Git
shell: bash
run: &set_crate_versions |
uv run --no-project --with tomlkit python - <<'PY'
from __future__ import annotations

import os
import re
import subprocess
from pathlib import Path

import tomlkit

tag = subprocess.check_output(
["git", "describe", "--tags", "--exact-match", "--match", os.environ["RELEASE_TAG"]],
text=True,
).strip()
# Accept only the repository's release tag forms and map them onto SemVer:
# v0.1.15 -> 0.1.15
# v0.1.15-rc0 -> 0.1.15-rc.0 (likewise alpha/beta)
# v0.1.14-post1 -> 0.1.15-post.1 (PEP 440 post releases sort above their base
# release, so they become a prerelease of the
# next patch rather than of the same one)
match = re.fullmatch(
r"v(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"
r"(?:-(?P<kind>alpha|beta|rc|post)(?P<num>\d+))?",
tag,
)
if match is None:
raise SystemExit(
f"unsupported release tag {tag!r}: "
"expected vX.Y.Z, vX.Y.Z-(alpha|beta|rc)N or vX.Y.Z-postN"
)
major, minor, patch = (int(match[name]) for name in ("major", "minor", "patch"))
kind, num = match["kind"], match["num"]
if kind == "post":
version = f"{major}.{minor}.{patch + 1}-post.{num}"
elif kind:
version = f"{major}.{minor}.{patch}-{kind}.{num}"
else:
version = f"{major}.{minor}.{patch}"
crates = ("tvm-ffi", "tvm-ffi-sys", "tvm-ffi-macros")
for crate in crates:
path = Path("rust") / crate / "Cargo.toml"
manifest = tomlkit.parse(path.read_text(encoding="utf-8"))
manifest["package"]["version"] = version
for section in ("dependencies", "dev-dependencies", "build-dependencies"):
for name, dependency in manifest.get(section, {}).items():
if name in crates:
dependency["version"] = version
path.write_text(tomlkit.dumps(manifest), encoding="utf-8")
print(f"{crate}: {version}")
with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as env_file:
env_file.write(f"CRATE_VERSION={version}\n")
PY
# Cargo validates the derived SemVer and the updated dependency requirements.
cargo +stable metadata --manifest-path rust/Cargo.toml --no-deps --format-version 1 > /dev/null

- name: Build crates
working-directory: rust
run: cargo +stable build --release --workspace

- name: Test crates
working-directory: rust
run: cargo +stable test --release --workspace

- name: Package and verify crates
working-directory: rust
# Only the release-version edits above are uncommitted in this checkout.
run: cargo +stable package --workspace --registry crates-io --allow-dirty

- name: Upload crate artifacts
uses: actions/upload-artifact@v4
with:
name: crates-${{ matrix.os }}-${{ matrix.arch }}
path: rust/target/package/*.crate
if-no-files-found: error

- name: Dry-run crate publication
if: inputs.dry_run
working-directory: rust
run: cargo +stable publish --dry-run --workspace --registry crates-io --allow-dirty

upload_crates:
needs: [build_crates]
runs-on: ubuntu-latest
environment: crates-io
if: github.event_name == 'workflow_dispatch' && !inputs.dry_run
steps:
- name: Check out release source
uses: actions/checkout@v5
with:
ref: refs/tags/${{ inputs.tag }}
submodules: recursive
fetch-depth: 0
fetch-tags: true

- name: Set up uv
uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
with:
python-version: "3.13"
activate-environment: true

- name: Set up Rust
run: rustup toolchain install stable --profile minimal

- uses: ./.github/actions/detect-env-vars
id: env_vars

- name: Build and install TVM FFI
env:
CMAKE_BUILD_PARALLEL_LEVEL: ${{ steps.env_vars.outputs.cpu_count }}
run: uv pip install --verbose .

- name: Set crate versions from Git
shell: bash
run: *set_crate_versions

# Publish in dependency order; each publish waits for the registry index before returning.
# A version that is already on crates.io is skipped, so a run that stopped part-way can be
# repeated and resumes with the crates that are still missing.
# Configure a crates.io API token with publish access to all three crates.
- name: Publish crates to crates.io
working-directory: rust
shell: bash
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
for crate in tvm-ffi-macros tvm-ffi-sys tvm-ffi; do
if cargo +stable info --registry crates-io "${crate}@${CRATE_VERSION}" > /dev/null 2>&1; then
echo "${crate}@${CRATE_VERSION} is already on crates.io; skipping."
continue
fi
echo "Publishing ${crate}@${CRATE_VERSION}"
cargo +stable publish --package "${crate}" --registry crates-io --allow-dirty
done
1 change: 1 addition & 0 deletions rust/tvm-ffi-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
name = "tvm-ffi-macros"
description = "Procedural macro crate for tvm-ffi"

# Development placeholder; publish_crates.yml sets the release version from the Git tag.
version = "0.1.0-alpha.0"
edition = "2021"
license = "Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions rust/tvm-ffi-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
name = "tvm-ffi-sys"
description = "Low-level sys crate for tvm-ffi"

# Development placeholder; publish_crates.yml sets the release version from the Git tag.
version = "0.1.0-alpha.0"
edition = "2021"
license = "Apache-2.0"
Expand Down
2 changes: 2 additions & 0 deletions rust/tvm-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
name = "tvm-ffi"
description = "tvm-ffi rust support"

# Development placeholder; publish_crates.yml sets the release version from the Git tag.
version = "0.1.0-alpha.0"
edition = "2021"
license = "Apache-2.0"
Expand All @@ -30,6 +31,7 @@ crate-type = ["lib"]

[dependencies]
paste = "1.0"
# Release CI also updates the internal dependency versions, including dev-dependencies.
tvm-ffi-sys = { version = "0.1.0-alpha.0", path = "../tvm-ffi-sys" }
tvm-ffi-macros = { version = "0.1.0-alpha.0", path = "../tvm-ffi-macros" }

Expand Down
Loading