From 9360c8464dff3bf6278aceba68aac26d20c85f06 Mon Sep 17 00:00:00 2001 From: Abimael Martell <1450169+abimaelmartell@users.noreply.github.com> Date: Fri, 5 Jun 2026 11:21:55 -0700 Subject: [PATCH] ci: add crates trusted publishing (#103) --- .github/workflows/publish-crate.yml | 87 +++++++++++++++++++++++++++++ docs/publishing.md | 21 +++++++ 2 files changed, 108 insertions(+) create mode 100644 .github/workflows/publish-crate.yml create mode 100644 docs/publishing.md diff --git a/.github/workflows/publish-crate.yml b/.github/workflows/publish-crate.yml new file mode 100644 index 0000000..168a3ab --- /dev/null +++ b/.github/workflows/publish-crate.yml @@ -0,0 +1,87 @@ +name: Publish Rust crate + +on: + push: + branches: [main] + paths: ['Cargo.toml'] + +permissions: + contents: read + +env: + CARGO_TERM_COLOR: always + +jobs: + check-version: + name: Check version change + runs-on: ubuntu-latest + outputs: + changed: ${{ steps.check.outputs.changed }} + published: ${{ steps.check.outputs.published }} + version: ${{ steps.check.outputs.version }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 2 + + - name: Check if version changed + id: check + run: | + NEW_VERSION=$(python3 -c 'import pathlib, tomllib; print(tomllib.loads(pathlib.Path("Cargo.toml").read_text())["package"]["version"])') + OLD_VERSION=$(git show HEAD~1:Cargo.toml | python3 -c 'import sys, tomllib; print(tomllib.loads(sys.stdin.read())["package"]["version"])') + echo "old=$OLD_VERSION new=$NEW_VERSION" + echo "version=$NEW_VERSION" >> "$GITHUB_OUTPUT" + + if [ "$NEW_VERSION" = "$OLD_VERSION" ]; then + echo "changed=false" >> "$GITHUB_OUTPUT" + echo "published=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "changed=true" >> "$GITHUB_OUTPUT" + + HTTP_STATUS=$(curl --silent --show-error --output /tmp/crate-version.json --write-out "%{http_code}" \ + -H "User-Agent: firecrawl/pdf-inspector publish workflow (https://github.com/firecrawl/pdf-inspector)" \ + "https://crates.io/api/v1/crates/pdf-inspector/$NEW_VERSION") + + case "$HTTP_STATUS" in + 200) + echo "published=true" >> "$GITHUB_OUTPUT" + echo "pdf-inspector v$NEW_VERSION is already published" + ;; + 404) + echo "published=false" >> "$GITHUB_OUTPUT" + ;; + *) + cat /tmp/crate-version.json + echo "Unexpected crates.io response: $HTTP_STATUS" >&2 + exit 1 + ;; + esac + + publish: + name: Publish to crates.io + needs: check-version + if: needs.check-version.outputs.changed == 'true' && needs.check-version.outputs.published == 'false' + runs-on: ubuntu-latest + environment: crates-io + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Verify package + run: cargo publish --dry-run + + - name: Authenticate with crates.io + id: auth + uses: rust-lang/crates-io-auth-action@v1 + + - name: Publish crate + run: cargo publish + env: + CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }} diff --git a/docs/publishing.md b/docs/publishing.md new file mode 100644 index 0000000..53bf876 --- /dev/null +++ b/docs/publishing.md @@ -0,0 +1,21 @@ +# Publishing + +The Rust crate is published to [crates.io](https://crates.io/crates/pdf-inspector) with trusted publishing from GitHub Actions. The first release was published manually; future releases publish from `.github/workflows/publish-crate.yml` when a `Cargo.toml` version change lands on `main`. + +## crates.io Trusted Publisher + +Configure the trusted publisher for the `pdf-inspector` crate with: + +- Repository: `firecrawl/pdf-inspector` +- Workflow: `publish-crate.yml` +- Environment: `crates-io` + +The workflow uses `rust-lang/crates-io-auth-action@v1` to exchange GitHub's OIDC token for a short-lived crates.io token, then passes it to `cargo publish`. + +## Release Steps + +1. Update `version` in `Cargo.toml`. +2. Merge the version bump to `main`. +3. The publish workflow compares the new `Cargo.toml` version with `HEAD~1`, runs `cargo publish --dry-run`, then publishes if that version is not already on crates.io. + +If `Cargo.toml` changes without a package version bump, the workflow exits without publishing.