|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# see RELEASE.md !! |
4 | | -# --- |
| 3 | +# Creates and pushes a version tag to trigger the release workflow. |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./release.sh # auto-bump patch (v1.2.3 -> v1.2.4) |
| 7 | +# ./release.sh v1.3.0 # use specific version |
| 8 | +# ./release.sh minor # bump minor (v1.2.3 -> v1.3.0) |
| 9 | +# ./release.sh major # bump major (v1.2.3 -> v2.0.0) |
5 | 10 |
|
6 | | -# exit on error |
7 | 11 | set -e |
8 | | -# print every command being run |
9 | | -set -x |
10 | 12 |
|
11 | | -# curl + jq package.json to get version number from repo |
12 | | -# https://raw.githubusercontent.com/letsdiscodev/cli/main/package.json |
13 | | -# store version number in variable |
14 | | -version_from_repo=$(curl -s https://raw.githubusercontent.com/letsdiscodev/cli/main/package.json | jq -r '.version') |
15 | | -version_from_package=$(cat package.json | jq -r '.version') |
16 | | -# if the versions are the same, fail/exit/quit. |
17 | | -if [ "$version_from_repo" = "$version_from_package" ]; then |
18 | | - echo "The version in package.json is the same as the version in the repo. Please update the version in package.json before running this script" |
19 | | - exit 1 |
| 13 | +# Get the latest tag |
| 14 | +latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 15 | +latest_version="${latest_tag#v}" |
| 16 | + |
| 17 | +# Parse version components |
| 18 | +IFS='.' read -r major minor patch <<< "$latest_version" |
| 19 | + |
| 20 | +# Determine the new version |
| 21 | +if [ -n "$1" ]; then |
| 22 | + case "$1" in |
| 23 | + major) |
| 24 | + new_version="$((major + 1)).0.0" |
| 25 | + ;; |
| 26 | + minor) |
| 27 | + new_version="${major}.$((minor + 1)).0" |
| 28 | + ;; |
| 29 | + patch) |
| 30 | + new_version="${major}.${minor}.$((patch + 1))" |
| 31 | + ;; |
| 32 | + v*) |
| 33 | + new_version="${1#v}" |
| 34 | + ;; |
| 35 | + *) |
| 36 | + new_version="$1" |
| 37 | + ;; |
| 38 | + esac |
| 39 | +else |
| 40 | + # Default: check package.json, use it if higher, otherwise bump patch |
| 41 | + package_version=$(node -p "require('./package.json').version") |
| 42 | + |
| 43 | + # Compare versions (simple string compare works for semver) |
| 44 | + if [ "$(printf '%s\n' "$latest_version" "$package_version" | sort -V | tail -n1)" = "$package_version" ] && [ "$package_version" != "$latest_version" ]; then |
| 45 | + new_version="$package_version" |
| 46 | + echo "Using version from package.json: $new_version" |
| 47 | + else |
| 48 | + new_version="${major}.${minor}.$((patch + 1))" |
| 49 | + echo "Auto-bumping patch: $latest_version -> $new_version" |
| 50 | + fi |
20 | 51 | fi |
21 | 52 |
|
22 | | -# fail if git status shows changes not staged for commit |
23 | | -if [[ `git status --porcelain` ]]; then |
24 | | - echo "There are changes not staged for commit. Please commit or stash them before running this script" |
25 | | - exit 1 |
26 | | -fi |
| 53 | +new_tag="v$new_version" |
27 | 54 |
|
28 | | -if [ -z "${AWS_ACCESS_KEY_ID}" ]; then |
29 | | - # fail |
30 | | - echo "AWS_ACCESS_KEY_ID is not set Please set it before running this script" |
| 55 | +# Check if tag already exists |
| 56 | +if git rev-parse "$new_tag" >/dev/null 2>&1; then |
| 57 | + echo "Error: Tag $new_tag already exists" |
31 | 58 | exit 1 |
32 | 59 | fi |
33 | 60 |
|
34 | | -if [ -z "${AWS_SECRET_ACCESS_KEY}" ]; then |
35 | | - # fail |
36 | | - echo "AWS_SECRET_ACCESS_KEY is not set Please set it before running this script" |
37 | | - exit 1 |
38 | | -fi |
| 61 | +# Confirm with user |
| 62 | +echo "" |
| 63 | +echo "This will create and push tag: $new_tag" |
| 64 | +echo "GitHub Actions will then build and release." |
| 65 | +read -p "Continue? [y/N] " -n 1 -r |
| 66 | +echo "" |
39 | 67 |
|
40 | | -if [ -z "${AWS_CLOUDFRONT_DISTRIBUTION_ID}" ]; then |
41 | | - # fail |
42 | | - echo "AWS_CLOUDFRONT_DISTRIBUTION_ID is not set Please set it before running this script" |
| 68 | +if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 69 | + echo "Aborted." |
43 | 70 | exit 1 |
44 | 71 | fi |
45 | 72 |
|
46 | | -# get the latest release pushed to the repo |
47 | | -# watch out as it has a 'v' at the beginning |
48 | | -# latest_release=$(gh release list --limit 1 --json tagName --jq '.[0].tagName') |
49 | | - |
50 | | -# get the version by cat'ing package.json into jq and extracting the version |
51 | | -version=$(cat package.json | jq -r '.version') |
52 | | - |
53 | | -# check if the latest release is the same as the version in package.json |
54 | | -# don't use right now as this script is not running as part of the github release workflow |
55 | | -# if [ "$latest_release" = "v$version" ]; then |
56 | | -# echo "The latest release is the same as the version in package.json. Please update the version in package.json before running this script" |
57 | | -# exit 1 |
58 | | -# fi |
59 | | - |
60 | | - |
61 | | -# --- |
| 73 | +# Create and push the tag |
| 74 | +git tag "$new_tag" |
| 75 | +git push origin "$new_tag" |
62 | 76 |
|
63 | | -rm -rf dist |
64 | | -rm -rf tmp |
65 | | - |
66 | | -npm ci |
67 | | - |
68 | | -npm run build |
69 | | - |
70 | | -oclif pack tarballs --no-xz |
71 | | - |
72 | | -oclif upload tarballs --no-xz |
73 | | - |
74 | | -# promote tarballs |
75 | | - |
76 | | -# get the hash by calling python |
77 | | -hash=$(export PACKAGE_VERSION=$version && python3 -c "from pathlib import Path; import os; print(str(list(Path('./dist').glob('disco-v' + os.environ['PACKAGE_VERSION'] + '*'))[0]).split('-')[2])") |
78 | | - |
79 | | -oclif promote --sha $hash --version $version --no-xz |
80 | | - |
81 | | -aws cloudfront create-invalidation --distribution-id $AWS_CLOUDFRONT_DISTRIBUTION_ID --paths "/*" |
82 | | - |
83 | | -echo "done" |
84 | | -echo "" |
85 | | -echo "***** * ** * * DONT FORGET TO GIT PUSH * * * ** * ******* *****" |
86 | 77 | echo "" |
| 78 | +echo "Tag $new_tag pushed. GitHub Actions will handle the release." |
| 79 | +echo "Watch progress at: https://github.com/letsdiscodev/cli/actions" |
0 commit comments