|
| 1 | +name: Publish Crates |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + crate_list: |
| 6 | + description: Space-separated crate package names to publish in order |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + dry_run: |
| 10 | + description: Run cargo publish --dry-run only |
| 11 | + required: false |
| 12 | + default: "true" |
| 13 | + type: choice |
| 14 | + options: ["true", "false"] |
| 15 | + |
| 16 | +env: |
| 17 | + CARGO_TERM_COLOR: always |
| 18 | + |
| 19 | +jobs: |
| 20 | + publish: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + - uses: dtolnay/rust-toolchain@stable |
| 27 | + - uses: Swatinem/rust-cache@v2 |
| 28 | + - name: Publish crates in dependency order |
| 29 | + env: |
| 30 | + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
| 31 | + CRATE_LIST: ${{ inputs.crate_list }} |
| 32 | + DRY_RUN: ${{ inputs.dry_run }} |
| 33 | + run: | |
| 34 | + set -euo pipefail |
| 35 | + for crate in $CRATE_LIST; do |
| 36 | + manifest="crates/$crate/Cargo.toml" |
| 37 | + if [ ! -f "$manifest" ]; then |
| 38 | + echo "Missing manifest for $crate at $manifest" >&2 |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + version=$(grep -m1 '^version' "$manifest" | sed 's/.*"\(.*\)".*/\1/') |
| 42 | + # Check crates.io with auth to avoid rate limits |
| 43 | + if curl -fsS -H "Authorization: ${CARGO_REGISTRY_TOKEN}" "https://crates.io/api/v1/crates/$crate/$version" >/dev/null 2>&1; then |
| 44 | + echo "$crate $version already exists on crates.io; skipping" |
| 45 | + continue |
| 46 | + fi |
| 47 | + if [ "$DRY_RUN" = "true" ]; then |
| 48 | + cargo publish --manifest-path "$manifest" --dry-run |
| 49 | + else |
| 50 | + set +e |
| 51 | + output=$(cargo publish --manifest-path "$manifest" 2>&1) |
| 52 | + exit_code=$? |
| 53 | + set -e |
| 54 | + if [ "$exit_code" -ne 0 ]; then |
| 55 | + if echo "$output" | grep -qi "already exists"; then |
| 56 | + echo "$crate $version already exists on crates.io; skipping" |
| 57 | + continue |
| 58 | + fi |
| 59 | + echo "$output" >&2 |
| 60 | + echo "ERROR: Failed to publish $crate" >&2 |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + fi |
| 64 | + done |
0 commit comments