@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

1 name: Publish to crates.io
2
3 on:
4 push:
5 tags:
6 - "v*.*.*"
7 workflow_dispatch:
8 inputs:
9 tag:
10 description: "Release tag (e.g. v1.3.0)"
11 required: true
12 dry_run:
13 description: "Package and verify without uploading or dispatching follow-up workflows"
14 type: boolean
15 default: false
16
17 concurrency:
18 # A re-pushed or re-dispatched tag fires this a second time. Queue the
19 # duplicate behind the original instead of cancelling it: cancelling a run
20 # mid-publish can leave a crate half-uploaded, whereas a queued duplicate
21 # just hits the "already published" check and exits clean.
22 group: ${{ github.workflow }}-${{ github.event.inputs.tag || github.ref_name }}
23 cancel-in-progress: false
24
25 # Publishing uses crates.io Trusted Publishing: the job trades its GitHub OIDC
26 # token for a short-lived crates.io token, so no long-lived API token is stored
27 # in the repository. Each crate (sigit-si-api and sigit-si-cli) needs a Trusted
28 # Publisher entry in its crates.io settings with repository `getsigit/si`,
29 # workflow `release-crates.yml`, and no environment. A missing entry shows up
30 # as a 403 from the auth step.
31 permissions:
32 contents: read
33
34 env:
35 CARGO_TERM_COLOR: always
36
37 jobs:
38 publish:
39 name: Publish to crates.io
40 runs-on: ubuntu-latest
41 permissions:
42 contents: read
43 id-token: write
44 outputs:
45 tag: ${{ steps.version.outputs.tag }}
46 published: ${{ steps.exists.outputs.published }}
47 steps:
48 - name: Checkout
49 uses: actions/checkout@v6
50 with:
51 ref: ${{ github.event.inputs.tag || github.ref }}
52
53 - name: Set release version
54 id: version
55 shell: bash
56 run: |
57 TAG="${{ github.event.inputs.tag || github.ref }}"
58 TAG="${TAG#refs/tags/}"
59 echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
60 echo "version=${TAG#v}" >> "$GITHUB_OUTPUT"
61
62 - name: Read Rust toolchain
63 shell: bash
64 run: |
65 rust_toolchain="$(sed -n 's/^channel = "\(.*\)"/\1/p' rust-toolchain.toml | head -n 1)"
66 if [ -z "$rust_toolchain" ]; then
67 echo "Failed to read Rust toolchain from rust-toolchain.toml" >&2
68 exit 1
69 fi
70 echo "RUST_TOOLCHAIN=${rust_toolchain}" >> "$GITHUB_ENV"
71
72 - name: Install Rust toolchain
73 uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9
74 with:
75 toolchain: ${{ env.RUST_TOOLCHAIN }}
76
77 - name: Setup Rust cache
78 uses: Swatinem/rust-cache@v2
79 with:
80 key: crates-publish
81
82 - name: Check crate versions match the tag
83 shell: bash
84 env:
85 VERSION: ${{ steps.version.outputs.version }}
86 run: |
87 mismatched=$(cargo metadata --no-deps --format-version 1 | \
88 jq -r --arg v "$VERSION" \
89 '.packages[] | select(.manifest_path | contains("/crates/")) | select(.version != $v) | "\(.name) \(.version)"')
90 if [ -n "$mismatched" ]; then
91 while read -r name version; do
92 echo "::error::${name} is at ${version}, but the tag is v${VERSION}"
93 done <<< "$mismatched"
94 exit 1
95 fi
96
97 - name: Test
98 run: cargo test --workspace --locked
99
100 # The CLI depends on the API crate, so the API goes first. Each crate sits
101 # behind its own "already published" check: a re-run publishes only what
102 # is missing.
103 - name: Check which crates already exist on crates.io
104 id: exists
105 shell: bash
106 env:
107 VERSION: ${{ steps.version.outputs.version }}
108 run: |
109 check() {
110 local crate="$1" output="$2" status
111 # crates.io answers 403 to requests without a descriptive
112 # User-Agent, so curl's default one makes this look like a 404 and
113 # the publish below runs into "crate already exists on the index".
114 # Branch on the status code rather than on curl's exit status, so an
115 # answer we do not understand stops the job instead of quietly
116 # degrading into that same failure.
117 # Retry first, so a timeout or a 5xx costs a few seconds instead of
118 # the whole release.
119 status=$(curl -sS -o /dev/null -w '%{http_code}' \
120 --retry 3 --retry-delay 2 --retry-all-errors \
121 -H "User-Agent: ${GITHUB_REPOSITORY} release workflow (https://github.com/${GITHUB_REPOSITORY})" \
122 "https://crates.io/api/v1/crates/${crate}/${VERSION}")
123
124 case "$status" in
125 200)
126 echo "${crate} ${VERSION} already exists on crates.io, skipping publish"
127 echo "${output}=true" >> "$GITHUB_OUTPUT"
128 ;;
129 404)
130 echo "${output}=false" >> "$GITHUB_OUTPUT"
131 ;;
132 *)
133 echo "Unexpected ${status} from crates.io while checking whether ${crate} ${VERSION} is published" >&2
134 exit 1
135 ;;
136 esac
137 }
138
139 check sigit-si-api api_exists
140 check sigit-si-cli cli_exists
141
142 - name: Get a crates.io token
143 id: auth
144 if: ${{ !inputs.dry_run && (steps.exists.outputs.api_exists != 'true' || steps.exists.outputs.cli_exists != 'true') }}
145 uses: rust-lang/crates-io-auth-action@v1
146
147 - name: Publish sigit-si-api to crates.io
148 if: steps.exists.outputs.api_exists != 'true'
149 env:
150 CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
151 DRY_RUN: ${{ inputs.dry_run }}
152 run: |
153 args=(--locked -p sigit-si-api)
154 if [ "$DRY_RUN" = "true" ]; then args+=(--dry-run); fi
155 cargo publish "${args[@]}"
156
157 # cargo publish waits for the API crate to show up in the index before it
158 # returns, so the CLI can resolve it straight away.
159 - name: Publish sigit-si-cli to crates.io
160 if: steps.exists.outputs.cli_exists != 'true'
161 env:
162 CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
163 DRY_RUN: ${{ inputs.dry_run }}
164 run: |
165 args=(--locked -p sigit-si-cli)
166 if [ "$DRY_RUN" = "true" ]; then args+=(--dry-run); fi
167 cargo publish "${args[@]}"
168
169 - name: Nothing to publish
170 if: steps.exists.outputs.api_exists == 'true' && steps.exists.outputs.cli_exists == 'true'
171 run: echo "Both crates are already on crates.io, nothing to publish"
172
173 # Runs even when nothing new was published: the MCP workflow skips duplicate
174 # versions itself, and waits for crates.io indexing and the mcp-name marker.
175 dispatch-mcp-registry:
176 name: Dispatch MCP Registry release
177 needs: publish
178 if: ${{ !inputs.dry_run }}
179 runs-on: ubuntu-latest
180 permissions:
181 actions: write
182 steps:
183 - name: Trigger MCP Registry release
184 uses: actions/github-script@v7
185 with:
186 script: |
187 await github.rest.actions.createWorkflowDispatch({
188 owner: context.repo.owner,
189 repo: context.repo.repo,
190 workflow_id: 'release-mcp-registry.yml',
191 ref: '${{ needs.publish.outputs.tag }}'
192 })