Skip to content

Commit ac1f2c0

Browse files
authored
Merge pull request #6 from MikeGarde/build-process
Added build process and release automation tasks
2 parents b0a9556 + 0c97fa3 commit ac1f2c0

File tree

3 files changed

+249
-4
lines changed

3 files changed

+249
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# will have compiled files and executables
33
debug
44
target
5+
dist
56

67
# These are backup files generated by rustfmt
78
**/*.rs.bk

Taskfile.yaml

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ vars:
44
BINARY_NAME: commitbot
55

66
tasks:
7+
setup:
8+
desc: "Setup development environment"
9+
silent: true
10+
status:
11+
- rustup target list --installed | grep -q x86_64-apple-darwin
12+
- rustup target list --installed | grep -q aarch64-apple-darwin
13+
cmds:
14+
- rustup target add x86_64-apple-darwin
15+
- rustup target add aarch64-apple-darwin
16+
17+
test:
18+
desc: "Run all tests"
19+
cmds:
20+
- cargo clippy --all-targets --all-features -- -D warnings
21+
- cargo test --all-features
22+
723
run:simple:
824
desc: "Run commitbot in simple mode (no model, debug) using cargo run"
925
cmds:
@@ -32,14 +48,52 @@ tasks:
3248
- cargo install --path . --force
3349

3450
build:release:
35-
desc: "Build optimized release binary"
51+
desc: "Build optimized release binary for the host"
52+
silent: true
3653
cmds:
3754
- cargo build --release
55+
- du -h target/release/{{.BINARY_NAME}}
56+
57+
build:release:*:
58+
desc: "Build release binaries for all targets and package into dist/"
59+
vars:
60+
VERSION: '{{index .MATCH 0}}'
61+
silent: true
62+
cmds:
63+
- mkdir -p dist
64+
- rm -f dist/commitbot-*.tar.gz
65+
- |
66+
set -euo pipefail
67+
68+
echo "Building release binaries..."
69+
cargo build --release --target aarch64-apple-darwin
70+
cargo build --release --target x86_64-apple-darwin
71+
72+
du -h target/aarch64-apple-darwin/release/commitbot
73+
du -h target/x86_64-apple-darwin/release/commitbot
74+
75+
echo "Packaging into dist with version {{.VERSION}}..."
76+
77+
tar -C target/aarch64-apple-darwin/release \
78+
-czf dist/commitbot-{{.VERSION}}-aarch64-apple-darwin.tar.gz \
79+
commitbot
80+
81+
tar -C target/x86_64-apple-darwin/release \
82+
-czf dist/commitbot-{{.VERSION}}-x86_64-apple-darwin.tar.gz \
83+
commitbot
84+
85+
echo "Artifacts created:"
86+
ls -lh dist
3887
39-
path:binary:
40-
desc: "Show the path to the release binary"
88+
release:*:
89+
desc: "Create a release by building and publishing to crates.io (requires VERSION arg)"
90+
vars:
91+
VERSION: '{{index .MATCH 0}}'
92+
interactive: true
4193
cmds:
42-
- echo "target/release/{{.BINARY_NAME}}"
94+
- sh devops/release.sh {{.VERSION}}
95+
- publish:dry
96+
- publish
4397

4498
publish:dry:
4599
desc: "Dry-run crates.io publish to verify packaging"

devops/release.sh

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/bin/zsh
2+
set -euo pipefail
3+
4+
# ---------------------------------------------------------
5+
# Config
6+
# ---------------------------------------------------------
7+
DIST_DIR="dist"
8+
9+
STEP=${1:?usage: release.sh [major|minor|patch|X.Y.Z]}
10+
11+
# ---------------------------------------------------------
12+
# Pre-flight checks
13+
# ---------------------------------------------------------
14+
if ! command -v task >/dev/null 2>&1; then
15+
echo "Error: 'task' command not found. Install Taskfile runner first."
16+
exit 1
17+
fi
18+
19+
if ! command -v gh >/dev/null 2>&1; then
20+
echo "Error: 'gh' CLI not found. Install GitHub CLI (gh) first."
21+
exit 1
22+
fi
23+
24+
BRANCH=$(git rev-parse --abbrev-ref HEAD)
25+
if [ "$BRANCH" != "main" ]; then
26+
echo "Error: releases may only be created from 'main' (current: $BRANCH)."
27+
exit 1
28+
fi
29+
30+
# Ensure working tree is clean
31+
if ! git diff --quiet || ! git diff --cached --quiet; then
32+
echo "Error: working tree is not clean. Commit or stash changes before releasing."
33+
exit 1
34+
fi
35+
36+
# ---------------------------------------------------------
37+
# Read current version from Cargo.toml
38+
# ---------------------------------------------------------
39+
CURRENT_VERSION=$(
40+
grep -E '^version = "[0-9]+\.[0-9]+\.[0-9]+"' Cargo.toml \
41+
| head -n1 \
42+
| sed -E 's/version = "([^"]+)"/\1/'
43+
)
44+
45+
if [ -z "$CURRENT_VERSION" ]; then
46+
echo "Error: could not determine current version from Cargo.toml"
47+
exit 1
48+
fi
49+
50+
MAJOR=$(echo "$CURRENT_VERSION" | cut -d. -f1)
51+
MINOR=$(echo "$CURRENT_VERSION" | cut -d. -f2)
52+
PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3)
53+
LATEST=true
54+
55+
# ---------------------------------------------------------
56+
# Compute new version
57+
# ---------------------------------------------------------
58+
case "$STEP" in
59+
major)
60+
MAJOR=$((MAJOR+1))
61+
MINOR=0
62+
PATCH=0
63+
;;
64+
minor)
65+
MINOR=$((MINOR+1))
66+
PATCH=0
67+
;;
68+
patch)
69+
PATCH=$((PATCH+1))
70+
;;
71+
*)
72+
if [[ "$STEP" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
73+
MAJOR=$(echo "$STEP" | cut -d. -f1)
74+
MINOR=$(echo "$STEP" | cut -d. -f2)
75+
PATCH=$(echo "$STEP" | cut -d. -f3)
76+
LATEST=false
77+
else
78+
echo "Invalid step: $STEP"
79+
echo "Usage: release.sh [major|minor|patch|X.Y.Z]"
80+
exit 1
81+
fi
82+
;;
83+
esac
84+
85+
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
86+
87+
# Check that tag does not already exist
88+
if git rev-parse -q --verify "refs/tags/$NEW_VERSION" >/dev/null 2>&1; then
89+
echo "Error: git tag $NEW_VERSION already exists."
90+
exit 1
91+
fi
92+
93+
echo "Current version: $CURRENT_VERSION"
94+
echo "New version: $NEW_VERSION"
95+
echo "Branch: $BRANCH"
96+
echo
97+
98+
read -r -p "Proceed with release? (y/N): " CONFIRM
99+
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
100+
echo "Aborted."
101+
exit 1
102+
fi
103+
104+
# ---------------------------------------------------------
105+
# Bump Cargo.toml with rollback on failure
106+
# ---------------------------------------------------------
107+
CARGO_BAK="Cargo.toml.bak.$$"
108+
cp Cargo.toml "$CARGO_BAK"
109+
110+
cleanup_on_error() {
111+
rc=$?
112+
echo "Error during release (exit code $rc). Restoring Cargo.toml..."
113+
if [ -f "$CARGO_BAK" ]; then
114+
mv "$CARGO_BAK" Cargo.toml
115+
fi
116+
exit $rc
117+
}
118+
trap cleanup_on_error INT TERM ERR
119+
120+
echo "Updating Cargo.toml to version $NEW_VERSION..."
121+
perl -pi -e 's/^version = "[0-9]+\.[0-9]+\.[0-9]+"/version = "'"$NEW_VERSION"'"/' Cargo.toml
122+
123+
# ---------------------------------------------------------
124+
# Run tasks: setup, test, build
125+
# ---------------------------------------------------------
126+
echo "Running task setup..."
127+
task setup
128+
129+
echo "Running task test..."
130+
task test
131+
132+
echo "Running task build:release:all..."
133+
task "build:release:${NEW_VERSION}"
134+
135+
# If we got here, tasks succeeded – stop rollback trap
136+
trap - INT TERM ERR
137+
rm -f "$CARGO_BAK"
138+
139+
# ---------------------------------------------------------
140+
# Commit, tag, push
141+
# ---------------------------------------------------------
142+
if git diff --quiet -- Cargo.toml; then
143+
echo "Warning: Cargo.toml did not change; nothing to commit."
144+
else
145+
git commit Cargo.toml -m "$NEW_VERSION"
146+
fi
147+
148+
HASH=$(git rev-parse HEAD)
149+
150+
echo "Tagging $NEW_VERSION..."
151+
git tag "$NEW_VERSION" "$HASH"
152+
153+
echo "Pushing main and tag..."
154+
git push origin main
155+
git push origin "refs/tags/$NEW_VERSION"
156+
157+
# ---------------------------------------------------------
158+
# Create GitHub release from artifacts in DIST_DIR
159+
# ---------------------------------------------------------
160+
if [ ! -d "$DIST_DIR" ]; then
161+
echo "Error: build artifacts directory '$DIST_DIR' not found."
162+
echo "Check task build:release:all"
163+
exit 1
164+
fi
165+
166+
ASSETS=("$DIST_DIR"/*)
167+
if [ ${#ASSETS[@]} -eq 0 ]; then
168+
echo "Error: no build artifacts found in '$DIST_DIR'."
169+
exit 1
170+
fi
171+
172+
LATEST_FLAG=""
173+
if [ "$LATEST" = true ]; then
174+
LATEST_FLAG="--latest"
175+
fi
176+
177+
echo "Creating GitHub release $NEW_VERSION with assets:"
178+
for a in "${ASSETS[@]}"; do
179+
echo " - $a"
180+
done
181+
182+
gh release create "$NEW_VERSION" \
183+
--fail-on-no-commits \
184+
--generate-notes \
185+
$LATEST_FLAG \
186+
--target "$HASH" \
187+
"${ASSETS[@]}"
188+
189+
echo "Release $NEW_VERSION created successfully."
190+
exit 0

0 commit comments

Comments
 (0)