|
| 1 | +name: Publish to crates.io |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | +jobs: |
| 5 | + checks: |
| 6 | + runs-on: ubuntu-latest |
| 7 | + if: false |
| 8 | + steps: |
| 9 | + - uses: actions/checkout@v6 |
| 10 | + with: |
| 11 | + fetch-depth: 0 |
| 12 | + - name: Check HEAD has a tag |
| 13 | + id: get_tag |
| 14 | + run: | |
| 15 | + TAG=$(git tag --points-at HEAD | head -n1) |
| 16 | + if [ -z "$TAG" ]; then |
| 17 | + echo "Error: HEAD commit has no tag. Please tag this commit before publishing." |
| 18 | + exit 1 |
| 19 | + fi |
| 20 | + echo "Found tag: $TAG" |
| 21 | + echo "tag=$TAG" >> "$GITHUB_OUTPUT" |
| 22 | + - name: Check tag matches Cargo.toml version |
| 23 | + run: | |
| 24 | + TAG="${{ steps.get_tag.outputs.tag }}" |
| 25 | + # Strip leading 'v' if present (v0.4.1 -> 0.4.1) |
| 26 | + VERSION="${TAG#v}" |
| 27 | + CARGO_VERSION=$(grep -m1 '^version' Cargo.toml | sed 's/.*"\(.*\)".*/\1/') |
| 28 | + if [ "$VERSION" != "$CARGO_VERSION" ]; then |
| 29 | + echo "Error: Tag version ($VERSION) does not match Cargo.toml version ($CARGO_VERSION)" |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | + echo "Tag matches Cargo.toml version: $VERSION" |
| 33 | + - name: Check version not already on crates.io |
| 34 | + run: | |
| 35 | + TAG="${{ steps.get_tag.outputs.tag }}" |
| 36 | + # Strip leading 'v' if present (v0.4.1 -> 0.4.1) |
| 37 | + VERSION="${TAG#v}" |
| 38 | + echo "Checking if $VERSION exists on crates.io..." |
| 39 | +
|
| 40 | + CRATE_NAME=$(grep -m1 '^name' Cargo.toml | sed 's/.*"\(.*\)".*/\1/') |
| 41 | + HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://crates.io/api/v1/crates/$CRATE_NAME/$VERSION") |
| 42 | + if [ "$HTTP_STATUS" = "200" ]; then |
| 43 | + echo "Error: Version $VERSION already exists on crates.io" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + echo "Version $VERSION not yet published, proceeding." |
| 47 | + ci: |
| 48 | + needs: checks |
| 49 | + uses: ./.github/workflows/ci.yml |
| 50 | + publish: |
| 51 | + needs: ci |
| 52 | + runs-on: ubuntu-latest |
| 53 | + steps: |
| 54 | + - uses: actions/checkout@v6 |
| 55 | + # TODO: check which packages are needed, if any |
| 56 | + - name: Install packages needed for build |
| 57 | + run: sudo apt-get update && sudo apt-get install -y build-essential |
| 58 | + - name: Install Rust |
| 59 | + uses: actions-rust-lang/setup-rust-toolchain@v1 |
| 60 | + - name: Publish to crates.io |
| 61 | + run: cargo publish |
| 62 | + env: |
| 63 | + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
0 commit comments