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 }}