-
Notifications
You must be signed in to change notification settings - Fork 245
186 lines (165 loc) · 6.76 KB
/
rust-publish.yml
File metadata and controls
186 lines (165 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: Publish Rust Crates
on:
workflow_dispatch:
inputs:
publish-kora-lib:
description: 'Publish kora-lib to crates.io'
required: true
default: true
type: boolean
publish-kora-cli:
description: 'Publish kora-cli to crates.io'
required: true
default: true
type: boolean
create-github-release:
description: 'Create GitHub release'
required: true
default: true
type: boolean
permissions:
contents: write
concurrency:
group: release-rust
cancel-in-progress: false
jobs:
publish:
name: Publish Kora crates to crates.io
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Guard allowed source branch for release publish
run: |
case "${{ github.ref }}" in
refs/heads/main|refs/heads/hotfix/*) ;;
*)
echo "::error::Publish Rust Crates must be run from main or hotfix/*. Current ref: ${{ github.ref }}"
exit 1
;;
esac
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Build check
run: cargo build --workspace
- name: Get current version
id: version
run: |
VERSION=$(grep "^version" Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "📦 Kora v$VERSION"
- name: Check if prerelease
id: prerelease
run: |
if [[ "${{ steps.version.outputs.version }}" == *"-"* ]]; then
echo "is_prerelease=true" >> $GITHUB_OUTPUT
echo "📦 Pre-release version detected"
else
echo "is_prerelease=false" >> $GITHUB_OUTPUT
fi
- name: Create and push git tags
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
VERSION="${{ steps.version.outputs.version }}"
for tag in "v$VERSION" "kora-lib-v$VERSION" "kora-cli-v$VERSION"; do
if git rev-parse "$tag" >/dev/null 2>&1; then
echo "⏭️ Tag $tag already exists, skipping"
else
git tag "$tag"
echo "✅ Created tag $tag"
fi
done
git push origin --tags 2>/dev/null || echo "⏭️ Tags already pushed"
- name: Authenticate crates.io trusted publisher
if: ${{ github.event.inputs.publish-kora-lib == 'true' || github.event.inputs.publish-kora-cli == 'true' }}
id: crates_io_auth
uses: rust-lang/crates-io-auth-action@bbd81622f20ce9e2dd9622e3218b975523e45bbe # v1.0.4
- name: Publish kora-lib to crates.io
if: ${{ github.event.inputs.publish-kora-lib == 'true' }}
env:
CARGO_REGISTRY_TOKEN: ${{ steps.crates_io_auth.outputs.token }}
run: |
echo "📦 Publishing kora-lib@${{ steps.version.outputs.version }} to crates.io..."
cargo publish -p kora-lib --locked
- name: Wait for kora-lib to be available on crates.io
if: ${{ github.event.inputs.publish-kora-lib == 'true' }}
run: |
echo "Waiting 30 seconds for crates.io to index kora-lib..."
sleep 30
- name: Publish kora-cli to crates.io
if: ${{ github.event.inputs.publish-kora-cli == 'true' }}
env:
CARGO_REGISTRY_TOKEN: ${{ steps.crates_io_auth.outputs.token }}
run: |
echo "📦 Publishing kora-cli@${{ steps.version.outputs.version }} to crates.io..."
cargo publish -p kora-cli --locked
- name: Create GitHub Release
if: ${{ github.event.inputs.create-github-release == 'true' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const tagName = `v${{ steps.version.outputs.version }}`;
const releaseName = `Kora v${{ steps.version.outputs.version }}`;
const isPrerelease = '${{ steps.prerelease.outputs.is_prerelease }}' === 'true';
// Check if release already exists
try {
await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: tagName,
});
console.log(`⏭️ Release ${tagName} already exists, skipping`);
return;
} catch (e) {
if (e.status !== 404) throw e;
}
const version = '${{ steps.version.outputs.version }}';
const body = [
`Release of Kora v${version}`,
'',
'**Crates:**',
`- [kora-lib v${version}](https://crates.io/crates/kora-lib/${version})`,
`- [kora-cli v${version}](https://crates.io/crates/kora-cli/${version})`,
].join('\n');
await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: tagName,
name: releaseName,
body,
draft: false,
prerelease: isPrerelease,
});
console.log(`✅ Created release: ${releaseName}`);
- name: Publish summary
run: |
echo "## 🎉 Kora Crates Published" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Version**: \`${{ steps.version.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**crates.io:**" >> $GITHUB_STEP_SUMMARY
if [ "${{ github.event.inputs.publish-kora-lib }}" == "true" ]; then
echo "- ✅ kora-lib: https://crates.io/crates/kora-lib/${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
else
echo "- ⏭️ kora-lib: skipped" >> $GITHUB_STEP_SUMMARY
fi
if [ "${{ github.event.inputs.publish-kora-cli }}" == "true" ]; then
echo "- ✅ kora-cli: https://crates.io/crates/kora-cli/${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
else
echo "- ⏭️ kora-cli: skipped" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
if [ "${{ github.event.inputs.create-github-release }}" == "true" ]; then
echo "✅ GitHub release created: https://github.com/${{ github.repository }}/releases/tag/v${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
else
echo "⏭️ Skipped GitHub release creation" >> $GITHUB_STEP_SUMMARY
fi