Skip to content

Commit 0faaf15

Browse files
committed
crates build ci
1 parent 196d96a commit 0faaf15

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Publish Rust Crates
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
- ci/release
9+
10+
jobs:
11+
publish:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Rust
18+
uses: dtolnay/rust-toolchain@stable
19+
20+
- name: Install cargo-binstall
21+
run: curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
22+
23+
- name: Install tq
24+
run: cargo binstall -y tomlq
25+
26+
- name: Cache Cargo registry and index
27+
uses: actions/cache@v3
28+
with:
29+
path: |
30+
~/.cargo/registry
31+
~/.cargo/git
32+
key: cargo-${{ runner.os }}-${{ hashFiles('**/Cargo.lock') }}
33+
restore-keys: cargo-${{ runner.os }}-
34+
35+
- name: Login to crates.io
36+
if: github.event_name == 'push'
37+
run: cargo login ${{ secrets.CRATES_API_TOKEN }}
38+
39+
- name: Write resolved versions to file
40+
run: |
41+
jq -r '.packages[] | select(.source == null) | "\(.name) \(.version)"' < <(cargo metadata --format-version=1) > resolved_versions.txt
42+
43+
44+
- name: Publish Crates
45+
run: |
46+
for manifest in crates/types/Cargo.toml crates/core/Cargo.toml crates/db/Cargo.toml crates/gql/Cargo.toml crates/subgraph/Cargo.toml crates/studio/Cargo.toml; do
47+
if [ -f "$manifest" ]; then
48+
echo "Checking $manifest for changes..."
49+
50+
CRATE_NAME=$(tq -f "$manifest" .package.name | sed 's/"//g') # remove quotes around package name
51+
VERSION=$(tq -f "$manifest" .package.version)
52+
53+
if cargo search -q "$CRATE_NAME" | grep "$VERSION"; then
54+
echo "$CRATE_NAME ($manifest) is already published with version $VERSION, skipping..."
55+
continue
56+
fi
57+
58+
# Resolve dependencies
59+
while IFS= read -r dep; do
60+
DEP_NAME=$(echo "$dep" | awk '{print $1}')
61+
RESOLVED_VERSION=$(echo "$dep" | awk '{print $2}')
62+
63+
# Check if resolved version exists on crates.io
64+
if cargo search -q "$DEP_NAME" --limit 1 | grep -q "$RESOLVED_VERSION"; then
65+
FINAL_VERSION="$RESOLVED_VERSION"
66+
else
67+
# Get the latest published version from crates.io
68+
FINAL_VERSION=$(cargo search -q "$DEP_NAME" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
69+
echo "Resolved version $RESOLVED_VERSION for $DEP_NAME is not published. Using latest published version $FINAL_VERSION instead."
70+
fi
71+
72+
# Replace workspace dependencies with the determined version
73+
sed -i "s|${DEP_NAME} = { workspace = true }|${DEP_NAME} = \"$FINAL_VERSION\"|" "$manifest"
74+
sed -i "s|${DEP_NAME} = { workspace = true, default-features = false }|${DEP_NAME} = \"$FINAL_VERSION\"|" "$manifest"
75+
sed -i "s|${DEP_NAME} = { path = \"../types\" }|${DEP_NAME} = \"$FINAL_VERSION\"|" "$manifest"
76+
77+
done < resolved_versions.txt
78+
79+
echo "Publishing $CRATE_NAME from $manifest..."
80+
if [ "${{ github.event_name }}" = "pull_request" ]; then
81+
cargo publish --package $CRATE_NAME --dry-run --allow-dirty
82+
else
83+
cargo publish --package $CRATE_NAME --allow-dirty
84+
fi
85+
else
86+
echo "Skipping $manifest, Cargo.toml not found."
87+
fi
88+
done
89+
env:
90+
CRATES_API_TOKEN: ${{ secrets.CRATES_API_TOKEN }}
91+

0 commit comments

Comments
 (0)