-
Notifications
You must be signed in to change notification settings - Fork 1
413 lines (353 loc) · 17.7 KB
/
rust-release-reusable.yml
File metadata and controls
413 lines (353 loc) · 17.7 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
name: Reusable Rust Release Workflow
on:
workflow_call:
inputs:
repository:
description: 'Repository to release (e.g., GoPlasmatic/datalogic-rs)'
required: true
type: string
package-type:
description: 'Package type: library or application'
required: false
type: string
default: 'library'
skip-publish:
description: 'Skip publishing to crates.io (for applications or testing)'
required: false
type: boolean
default: false
dry-run:
description: 'Dry run (skip publishing)'
required: false
type: boolean
default: false
docker-enabled:
description: 'Enable Docker image build and push'
required: false
type: boolean
default: false
docker-image-name:
description: 'Docker image name (defaults to package name)'
required: false
type: string
secrets:
GH_PAT:
description: 'GitHub Personal Access Token for pushing tags'
required: true
CRATES_IO_TOKEN:
description: 'Token for publishing to crates.io'
required: false
ACR_URL:
description: 'Azure Container Registry URL (e.g., myregistry.azurecr.io)'
required: false
ACR_USERNAME:
description: 'Azure Container Registry username (service principal ID or username)'
required: false
ACR_PASSWORD:
description: 'Azure Container Registry password or token'
required: false
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
repository: ${{ inputs.repository }}
fetch-depth: 0
token: ${{ secrets.GH_PAT }}
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache cargo dependencies
uses: Swatinem/rust-cache@v2
with:
key: ${{ inputs.repository }}
cache-on-failure: true
- name: Install cargo-workspaces (if needed)
run: |
# Check if this is a workspace
PACKAGE_COUNT=$(cargo metadata --no-deps --format-version 1 | jq '.packages | length')
if [ "$PACKAGE_COUNT" -gt 1 ] && [ "${{ inputs.package-type }}" = "library" ]; then
echo "Installing cargo-workspaces for better dependency handling..."
cargo install cargo-workspaces || echo "cargo-workspaces installation failed, will use fallback method"
fi
- name: Check code formatting
run: cargo fmt -- --check
- name: Run clippy
run: cargo clippy -- -D warnings
- name: Run tests
run: cargo test --all-features
- name: Get package info
id: package_info
run: |
# Check if this is a workspace or single package
PACKAGE_COUNT=$(cargo metadata --no-deps --format-version 1 | jq '.packages | length')
if [ "$PACKAGE_COUNT" -gt 1 ]; then
# For workspace, find all publishable packages in dependency order
echo "Workspace with $PACKAGE_COUNT packages detected"
# Get all publishable packages
PUBLISHABLE_PACKAGES=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.publish != false) | .name' | tr '\n' ' ')
echo "Publishable packages: $PUBLISHABLE_PACKAGES"
# For now, we'll handle the main package (this will be improved for dependency ordering)
if [ "${{ inputs.package-type }}" = "application" ]; then
# Find binary package
PACKAGE_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.targets[]?.kind[]? == "bin") | .name' | head -n1)
else
# Find main library package (usually doesn't have -macros, -derive, etc. suffix)
PACKAGE_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.publish != false) | select(.name | test("-macros$|-derive$|-core$") | not) | .name' | head -n1)
fi
PACKAGE_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r ".packages[] | select(.name == \"$PACKAGE_NAME\") | .version")
# Store all publishable packages for later use
echo "publishable_packages=$PUBLISHABLE_PACKAGES" >> $GITHUB_OUTPUT
else
# Single package
PACKAGE_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name')
PACKAGE_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
echo "publishable_packages=$PACKAGE_NAME" >> $GITHUB_OUTPUT
fi
echo "name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
echo "version=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
echo "count=$PACKAGE_COUNT" >> $GITHUB_OUTPUT
echo "Main package: $PACKAGE_NAME v$PACKAGE_VERSION (workspace: $PACKAGE_COUNT packages)"
- name: Check if version exists on crates.io
if: inputs.package-type == 'library' && inputs.skip-publish != true
id: check_crates
run: |
CRATE_VERSION=$(curl -s https://crates.io/api/v1/crates/${{ steps.package_info.outputs.name }} | jq -r '.crate.max_version // "none"')
echo "Current crates.io version: $CRATE_VERSION"
echo "Local version: ${{ steps.package_info.outputs.version }}"
if [ "$CRATE_VERSION" = "${{ steps.package_info.outputs.version }}" ]; then
echo "Version ${{ steps.package_info.outputs.version }} already exists on crates.io"
echo "exists=true" >> $GITHUB_OUTPUT
exit 1
else
echo "Version ${{ steps.package_info.outputs.version }} is new"
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Configure git
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "actions@github.com"
- name: Create and push tag
id: tag
run: |
TAG="v${{ steps.package_info.outputs.version }}"
# Check if tag already exists
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Tag $TAG already exists"
echo "exists=true" >> $GITHUB_OUTPUT
else
git tag -a "$TAG" -m "Release $TAG - ${{ steps.package_info.outputs.name }}"
# Configure remote with PAT
git remote set-url origin https://x-access-token:${{ secrets.GH_PAT }}@github.com/${{ inputs.repository }}.git
git push origin "$TAG"
echo "Created and pushed tag $TAG"
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Build release (application)
if: inputs.package-type == 'application'
run: |
if [ "${{ steps.package_info.outputs.count }}" -gt 1 ]; then
# Workspace: build specific package
cargo build --release --package ${{ steps.package_info.outputs.name }}
else
# Single package
cargo build --release
fi
echo "Binary built at: target/release/${{ steps.package_info.outputs.name }}"
- name: Package release artifacts (application)
if: inputs.package-type == 'application' && steps.tag.outputs.exists != 'true'
run: |
mkdir -p release-artifacts
cp target/release/${{ steps.package_info.outputs.name }} release-artifacts/
cp README.md release-artifacts/ 2>/dev/null || echo "No README.md found"
cp LICENSE release-artifacts/ 2>/dev/null || echo "No LICENSE found"
cd release-artifacts
tar -czf ../${{ steps.package_info.outputs.name }}-v${{ steps.package_info.outputs.version }}-linux-x86_64.tar.gz *
cd ..
echo "Release artifacts packaged"
- name: Login to Azure Container Registry
if: inputs.docker-enabled == true && inputs.dry-run != true
run: |
echo "Logging in to Azure Container Registry: ${{ secrets.ACR_URL }}"
echo "${{ secrets.ACR_PASSWORD }}" | docker login "${{ secrets.ACR_URL }}" \
--username "${{ secrets.ACR_USERNAME }}" \
--password-stdin
- name: Create default Dockerfile if needed
if: inputs.docker-enabled == true
run: |
if [ ! -f "Dockerfile" ]; then
echo "Creating multi-stage Dockerfile for Rust application"
cat > Dockerfile << 'DOCKERFILE_END'
# Build stage
FROM rust:1.75-slim as builder
WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y \
pkg-config \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*
# Copy manifests
COPY Cargo.toml Cargo.lock ./
# Copy source code
COPY src ./src
# Build release binary
RUN cargo build --release
# Runtime stage
FROM debian:bookworm-slim
# Install runtime dependencies
RUN apt-get update && apt-get install -y \
ca-certificates \
libssl3 \
&& rm -rf /var/lib/apt/lists/*
# Copy the binary from builder
COPY --from=builder /app/target/release/${{ steps.package_info.outputs.name }} /usr/local/bin/${{ steps.package_info.outputs.name }}
# Create non-root user
RUN useradd -m -u 1000 appuser && \
chown appuser:appuser /usr/local/bin/${{ steps.package_info.outputs.name }}
USER appuser
ENTRYPOINT ["/usr/local/bin/${{ steps.package_info.outputs.name }}"]
DOCKERFILE_END
echo "Created default Dockerfile"
else
echo "Using existing Dockerfile"
fi
- name: Build Docker image
if: inputs.docker-enabled == true
run: |
IMAGE_NAME="${{ inputs.docker-image-name || steps.package_info.outputs.name }}"
IMAGE_TAG="${{ steps.package_info.outputs.version }}"
FULL_IMAGE="${{ secrets.ACR_URL }}/${IMAGE_NAME}:${IMAGE_TAG}"
LATEST_IMAGE="${{ secrets.ACR_URL }}/${IMAGE_NAME}:latest"
echo "Building Docker image: ${FULL_IMAGE}"
# Check if Dockerfile expects PACKAGE_URL build arg (for Reframe)
if grep -q "ARG PACKAGE_URL" Dockerfile 2>/dev/null; then
echo "Dockerfile requires PACKAGE_URL build argument"
# Default package URL for Reframe SWIFT CBPR package
PACKAGE_URL="${PACKAGE_URL:-https://github.com/GoPlasmatic/reframe-package-swift-cbpr/releases/download/v2.1.0/reframe-swift-cbpr-v2.1.0.zip}"
echo "Using PACKAGE_URL: ${PACKAGE_URL}"
docker build --build-arg PACKAGE_URL="${PACKAGE_URL}" -t "${FULL_IMAGE}" -t "${LATEST_IMAGE}" .
else
echo "Building without PACKAGE_URL argument"
docker build -t "${FULL_IMAGE}" -t "${LATEST_IMAGE}" .
fi
echo "docker_image=${FULL_IMAGE}" >> $GITHUB_OUTPUT
echo "docker_image_latest=${LATEST_IMAGE}" >> $GITHUB_OUTPUT
- name: Push Docker image to Azure Container Registry
if: inputs.docker-enabled == true && inputs.dry-run != true
run: |
IMAGE_NAME="${{ inputs.docker-image-name || steps.package_info.outputs.name }}"
IMAGE_TAG="${{ steps.package_info.outputs.version }}"
FULL_IMAGE="${{ secrets.ACR_URL }}/${IMAGE_NAME}:${IMAGE_TAG}"
LATEST_IMAGE="${{ secrets.ACR_URL }}/${IMAGE_NAME}:latest"
echo "Pushing Docker images to Azure Container Registry"
docker push "${FULL_IMAGE}"
docker push "${LATEST_IMAGE}"
echo "✅ Docker images pushed:"
echo " - ${FULL_IMAGE}"
echo " - ${LATEST_IMAGE}"
- name: Create GitHub Release
if: steps.tag.outputs.exists != 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.package_info.outputs.version }}
name: ${{ steps.package_info.outputs.name }} v${{ steps.package_info.outputs.version }}
body: |
## ${{ steps.package_info.outputs.name }} v${{ steps.package_info.outputs.version }}
### Installation
${{ inputs.package-type == 'library' && format('Add to your `Cargo.toml`:
```toml
[dependencies]
{0} = "{1}"
```', steps.package_info.outputs.name, steps.package_info.outputs.version) || format('Download and extract the binary:
```bash
tar -xzf {0}-v{1}-linux-x86_64.tar.gz
chmod +x {0}
./{0} --version
```', steps.package_info.outputs.name, steps.package_info.outputs.version) }}
### Documentation
- [Repository](https://github.com/${{ inputs.repository }})
${{ inputs.package-type == 'library' && format('- [API Documentation](https://docs.rs/{0})', steps.package_info.outputs.name) || '' }}
draft: false
prerelease: false
token: ${{ secrets.GH_PAT }}
files: ${{ inputs.package-type == 'application' && format('{0}-v{1}-linux-x86_64.tar.gz', steps.package_info.outputs.name, steps.package_info.outputs.version) || '' }}
- name: Publish to crates.io (library)
if: inputs.package-type == 'library' && inputs.skip-publish != true && inputs.dry-run != true && steps.check_crates.outputs.exists != 'true'
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
run: |
if [ "${{ steps.package_info.outputs.count }}" -gt 1 ]; then
echo "Publishing workspace packages..."
# First, try to publish all workspace packages in dependency order
# This uses cargo-workspaces if available, otherwise falls back to manual publish
if command -v cargo-workspaces &> /dev/null; then
echo "Using cargo-workspaces for publishing"
cargo workspaces publish --from-git --no-verify --yes
else
echo "Publishing packages individually"
# Get packages in dependency order using cargo metadata
PACKAGES_JSON=$(cargo metadata --format-version 1)
# Function to publish a package and its dependencies
publish_with_deps() {
local pkg=$1
local version=$(echo "$PACKAGES_JSON" | jq -r ".packages[] | select(.name == \"$pkg\") | .version")
# Check if already published
CRATE_VERSION=$(curl -s https://crates.io/api/v1/crates/$pkg | jq -r '.crate.max_version // "none"')
if [ "$CRATE_VERSION" = "$version" ]; then
echo "$pkg v$version already published"
return 0
fi
# Try to publish
echo "Publishing $pkg v$version..."
if cargo publish --package $pkg --no-verify; then
echo "✅ Published $pkg v$version"
# Wait for crates.io to index
sleep 10
else
echo "⚠️ Failed to publish $pkg v$version (might have dependencies)"
return 1
fi
}
# Try to publish packages, starting with those that might be dependencies
for pkg in ${{ steps.package_info.outputs.publishable_packages }}; do
# Skip if it's the main package (we'll do it last)
if [ "$pkg" != "${{ steps.package_info.outputs.name }}" ]; then
publish_with_deps "$pkg" || true
fi
done
# Finally publish the main package
publish_with_deps "${{ steps.package_info.outputs.name }}"
fi
else
# Single package
cargo publish --no-verify
echo "✅ Published ${{ steps.package_info.outputs.name }} v${{ steps.package_info.outputs.version }} to crates.io"
fi
- name: Release summary
run: |
if [ "${{ inputs.dry-run }}" = "true" ]; then
echo "🔍 DRY RUN: Skipping release"
echo "Would have released ${{ steps.package_info.outputs.name }} v${{ steps.package_info.outputs.version }}"
else
echo "✅ Released ${{ steps.package_info.outputs.name }} v${{ steps.package_info.outputs.version }}"
if [ "${{ inputs.package-type }}" = "application" ]; then
echo "📦 Binary package: ${{ steps.package_info.outputs.name }}-v${{ steps.package_info.outputs.version }}-linux-x86_64.tar.gz"
elif [ "${{ inputs.skip-publish }}" = "true" ]; then
echo "📦 Git tag and GitHub release created (no crates.io publish)"
else
echo "📦 Published to crates.io"
fi
echo "🏷️ Git tag: v${{ steps.package_info.outputs.version }}"
if [ "${{ inputs.docker-enabled }}" = "true" ]; then
IMAGE_NAME="${{ inputs.docker-image-name || steps.package_info.outputs.name }}"
echo "🐳 Docker images pushed to Azure Container Registry:"
echo " - ${{ secrets.ACR_URL }}/${IMAGE_NAME}:${{ steps.package_info.outputs.version }}"
echo " - ${{ secrets.ACR_URL }}/${IMAGE_NAME}:latest"
fi
fi