Skip to content

Commit 1b32c97

Browse files
authored
Migrate this workspace to using trusted publishing (#1350)
* Migrate this workspace to using trusted publishing Similar to bytecodealliance/wasm-tools#2281 but for this repository. The main benefit is removal of a long-lived token, and the main consequence is that crates will need to be published to crates.io with a placeholder before being merged in here. * Prints-to-panics * Add necessary CI configuration keys
1 parent b74ed2c commit 1b32c97

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

.github/workflows/publish.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ on:
99

1010
permissions:
1111
contents: write
12+
id-token: write
1213

1314
jobs:
1415
create_tag:
1516
name: Publish artifacts of build
1617
runs-on: ubuntu-latest
18+
environment: release
1719
if: |
1820
github.repository_owner == 'bytecodealliance'
1921
&& github.event_name == 'push'
@@ -79,10 +81,14 @@ jobs:
7981
files: "dist/*"
8082
tag_name: v${{ steps.tag.outputs.version }}
8183

84+
- uses: rust-lang/crates-io-auth-action@v1
85+
id: auth
86+
if: steps.tag.outputs.push_tag == 'yes'
87+
8288
- run: |
8389
rm -rf dist main.log
8490
rustc ci/publish.rs
8591
./publish publish
8692
env:
87-
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
93+
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
8894
if: steps.tag.outputs.push_tag == 'yes'

ci/publish.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -334,42 +334,6 @@ fn publish(krate: &Crate) -> bool {
334334
return false;
335335
}
336336

337-
// After we've published then make sure that the `wasmtime-publish` group is
338-
// added to this crate for future publications. If it's already present
339-
// though we can skip the `cargo owner` modification.
340-
match curl(&format!(
341-
"https://crates.io/api/v1/crates/{}/owners",
342-
krate.name
343-
)) {
344-
Some(output) => {
345-
if output.contains("wasmtime-publish") {
346-
println!(
347-
"wasmtime-publish already listed as an owner of {}",
348-
krate.name
349-
);
350-
return true;
351-
}
352-
}
353-
None => return false,
354-
}
355-
356-
// Note that the status is ignored here. This fails most of the time because
357-
// the owner is already set and present, so we only want to add this to
358-
// crates which haven't previously been published.
359-
let status = Command::new("cargo")
360-
.arg("owner")
361-
.arg("-a")
362-
.arg("github:bytecodealliance:wasmtime-publish")
363-
.arg(&krate.name)
364-
.status()
365-
.expect("failed to run cargo");
366-
if !status.success() {
367-
panic!(
368-
"FAIL: failed to add wasmtime-publish as owner `{}`: {}",
369-
krate.name, status
370-
);
371-
}
372-
373337
true
374338
}
375339

@@ -410,6 +374,7 @@ fn verify(crates: &[Crate]) {
410374
if !krate.publish {
411375
continue;
412376
}
377+
verify_crates_io(krate);
413378
verify_and_vendor(&krate);
414379
}
415380

@@ -441,6 +406,46 @@ fn verify(crates: &[Crate]) {
441406
)
442407
.unwrap();
443408
}
409+
410+
fn verify_crates_io(krate: &Crate) {
411+
let name = &krate.name;
412+
let Some(owners) = curl(&format!("https://crates.io/api/v1/crates/{name}/owners")) else {
413+
panic!(
414+
"
415+
failed to get owners for {name}
416+
417+
If this crate does not exist on crates.io yet please ping wasm-tools maintainers
418+
to add the crate on crates.io as a small shim. When doing so please remind them
419+
that the trusted publishing workflow must be configured as well.
420+
",
421+
name = name,
422+
);
423+
};
424+
425+
// This is the id of the `wasmtime-publish` user on crates.io
426+
if !owners.contains("\"id\":73222,") {
427+
panic!(
428+
"
429+
crate {name} is not owned by wasmtime-publish, please run:
430+
431+
cargo owner -a wasmtime-publish {name}
432+
",
433+
name = name,
434+
);
435+
}
436+
437+
if owners.split("\"id\"").count() != 2 {
438+
panic!(
439+
"
440+
crate {name} is not exclusively owned by wasmtime-publish
441+
442+
Please contact wasm-tools maintainers to ensure that `wasmtime-publish` is the
443+
only listed owner of the crate.
444+
",
445+
name = name,
446+
);
447+
}
448+
}
444449
}
445450

446451
fn cmd_output(cmd: &mut Command) -> Output {

0 commit comments

Comments
 (0)