Skip to content

Commit f29ac18

Browse files
committed
setup: add auto configuration for publishing workflows
Make repository_setup.sh automatically discover whether we have a multi crate cargo workspace, or just a single crate repository, and then generate a publish github action for each of them that triggers when a tag named '$crate_name-v*' (or just 'v*' for single-crate repos) is published. Signed-off-by: Patrick Roy <[email protected]>
1 parent 563acd1 commit f29ac18

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

repository_setup.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,46 @@ if [ -f "$DEPENDABOT_FILE" ]; then
9999
else
100100
setup_dependabot_config
101101
fi
102+
103+
echo
104+
echo "Setting up auto-publish for crates.io upon creation of GitHub tags..."
105+
106+
if [ ! -f Cargo.toml ]; then
107+
echo "Cargo.toml not found. Please re-run this script after setting up the crates to configure auto-publishing!"
108+
else
109+
mkdir -p .github/workflows
110+
if grep -q '\[workspace\]' Cargo.toml; then
111+
members="$(cargo metadata --format-version 1 | jq '.workspace_members[]')"
112+
for member_path in $members; do
113+
member_path="${member_path#*//}"
114+
member_path="${member_path%#*}"
115+
116+
crate_name=$(basename $member_path)
117+
# get path relative to crate root
118+
member_path=$(realpath -m --relative-to . $member_path)
119+
120+
workflow_file=.github/workflows/publish-$crate_name.yml
121+
# Use printf when writing this value, so that whitespaces/newlines are respected
122+
workflow=$(sed "s/'v\*'/'$crate_name-v*'/" $RUST_VMM_CI/publish.yml | sed "s/cd ./cd $member_path/")
123+
124+
if [ -f $workflow_file ] && cmp --silent $workflow_file <(printf "$workflow"); then
125+
echo "Publish workflow for $crate_name already setup, skipping"
126+
continue
127+
fi
128+
129+
if confirm "Found workspace member '$crate_name' at '$member_path'. Setup auto-publish?"; then
130+
if [ -f $workflow_file ] && ! confirm "$workflow_file already exists. Overwrite?"; then
131+
continue
132+
fi
133+
134+
printf "$workflow" > $workflow_file
135+
echo "If not already done, go to https://crates.io/crates/$crate_name/settings and add $(basename $workflow_file) as a trusted publisher!"
136+
fi
137+
done
138+
else
139+
question="Single crate repository detected, setup running 'cargo publish' at repository root when tags matching 'v*' are published? (if a .github/workflows/publish.yml file already exists, it will be overwritten)"
140+
if confirm "$question"; then
141+
cp $RUST_VMM_CI/publish.yml .github/workflows/publish.yml
142+
fi
143+
fi
144+
fi

0 commit comments

Comments
 (0)