|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------------ |
| 4 | +# DO RELEASE |
| 5 | +# ------------------------------------------------------------------------------ |
| 6 | +# This script automates the release process: |
| 7 | +# 1. Builds the application for macOS, Windows, and Linux |
| 8 | +# 2. Uploads the build artifacts to AWS S3 |
| 9 | +# 3. Invalidates the CloudFront cache for the release files |
| 10 | +# 4. Tags the latest commit on main with the version number |
| 11 | +# 5. Creates a GitHub release for the version number |
| 12 | +# 6. Updates the download links in the index.html file |
| 13 | +# 7. Invalidates the CloudFront cache for the index.html file |
| 14 | +# ------------------------------------------------------------------------------ |
| 15 | +# Prerequisites: |
| 16 | +# 1. jq: Command-line JSON processor |
| 17 | +# 2. AWS CLI: For interacting with AWS S3 |
| 18 | +# 3. GitHub CLI (gh): For tagging commits and creating releases on GitHub |
| 19 | +# |
| 20 | +# Install these tools on macOS using Homebrew: |
| 21 | +# - jq: brew install jq |
| 22 | +# - AWS CLI: brew install awscli |
| 23 | +# - GitHub CLI: brew install gh |
| 24 | +# |
| 25 | +# Additional Setup: |
| 26 | +# - Configure AWS CLI with `aws configure` and set up a profile named |
| 27 | +# 'worthwhileadventures'. |
| 28 | +# - Authenticate GitHub CLI with `gh auth login`. |
| 29 | +# ------------------------------------------------------------------------------ |
| 30 | + |
| 31 | +# Enable strict mode |
| 32 | +set -euo pipefail |
| 33 | + |
| 34 | +# Extract version from package.json |
| 35 | +APP_VERSION=$(jq -r '.version' package.json) |
| 36 | +echo "Detected application version: ${APP_VERSION}" |
| 37 | + |
| 38 | +# S3 bucket and profile |
| 39 | +S3_BUCKET="s3://scourhead.com" |
| 40 | +S3_RELEASE_FOLDER="$S3_BUCKET/releases" |
| 41 | +AWS_PROFILE="worthwhileadventures" |
| 42 | +BASE_URL="https://scourhead.com/releases" |
| 43 | +CLOUDFRONT_DISTRIBUTION_ID="E146D77OPFI37E" |
| 44 | + |
| 45 | +# Function to print errors and exit |
| 46 | +error_exit() { |
| 47 | + echo "Error: $1" >&2 |
| 48 | + exit 1 |
| 49 | +} |
| 50 | + |
| 51 | +# Check if a Git tag exists |
| 52 | +check_and_create_tag() { |
| 53 | + local tag="v${APP_VERSION}" |
| 54 | + if ! git rev-parse "$tag" > /dev/null 2>&1; then |
| 55 | + echo "Creating Git tag: $tag" |
| 56 | + git tag -a "$tag" -m "Release ${APP_VERSION}" || error_exit "Failed to create tag $tag" |
| 57 | + git push origin "$tag" || error_exit "Failed to push tag $tag" |
| 58 | + else |
| 59 | + echo "Git tag $tag already exists. Skipping." |
| 60 | + fi |
| 61 | +} |
| 62 | + |
| 63 | +# Create a GitHub release |
| 64 | +create_github_release() { |
| 65 | + local tag="v${APP_VERSION}" |
| 66 | + local release_notes=$(cat <<EOF |
| 67 | +# Scourhead v${APP_VERSION} |
| 68 | +
|
| 69 | +Download links for all architectures: |
| 70 | +
|
| 71 | +- [macOS arm64](https://scourhead.com/releases/${APP_VERSION}/Scourhead-${APP_VERSION}-arm64.dmg) |
| 72 | +- [Windows x64](https://scourhead.com/releases/${APP_VERSION}/Scourhead-Setup-x64.exe) |
| 73 | +- [Windows arm64](https://scourhead.com/releases/${APP_VERSION}/Scourhead-Setup-arm64.exe) |
| 74 | +- [Linux x64](https://scourhead.com/releases/${APP_VERSION}/scourhead_${APP_VERSION}_amd64.deb) |
| 75 | +- [Linux arm64](https://scourhead.com/releases/${APP_VERSION}/scourhead_${APP_VERSION}_arm64.deb) |
| 76 | +EOF |
| 77 | +) |
| 78 | + |
| 79 | + if ! gh release view "$tag" > /dev/null 2>&1; then |
| 80 | + echo "Creating GitHub release for $tag" |
| 81 | + gh release create "$tag" \ |
| 82 | + --title "v${APP_VERSION}" \ |
| 83 | + --notes "$release_notes" || error_exit "Failed to create GitHub release" |
| 84 | + else |
| 85 | + echo "GitHub release $tag already exists. Skipping." |
| 86 | + fi |
| 87 | +} |
| 88 | + |
| 89 | +cleanup() { |
| 90 | + echo "Cleaning up release directory..." |
| 91 | + rm -rf release && npm run clean || error_exit "Failed to clean up release directory" |
| 92 | +} |
| 93 | + |
| 94 | +build_upload_check() { |
| 95 | + local build_command=$1 |
| 96 | + local file_path=$2 |
| 97 | + local s3_path=$3 |
| 98 | + |
| 99 | + echo "Running build command: $build_command" |
| 100 | + eval "$build_command" || error_exit "Build command failed: $build_command" |
| 101 | + |
| 102 | + echo "Uploading $file_path to $s3_path" |
| 103 | + aws s3 cp "$file_path" "$s3_path" --profile "$AWS_PROFILE" || error_exit "Failed to upload $file_path to S3" |
| 104 | + |
| 105 | + echo "Verifying upload: $s3_path" |
| 106 | + aws s3 ls "$s3_path" --profile "$AWS_PROFILE" > /dev/null || error_exit "Uploaded file not found in S3: $s3_path" |
| 107 | +} |
| 108 | + |
| 109 | +invalidate_cloudfront_cache() { |
| 110 | + echo "Invalidating CloudFront cache for /releases/${APP_VERSION}/*" |
| 111 | + INVALIDATION_ID=$(aws cloudfront create-invalidation \ |
| 112 | + --distribution-id "$CLOUDFRONT_DISTRIBUTION_ID" \ |
| 113 | + --paths "/releases/${APP_VERSION}/*" \ |
| 114 | + --profile "$AWS_PROFILE" \ |
| 115 | + --query 'Invalidation.Id' \ |
| 116 | + --output text) || error_exit "Failed to create CloudFront invalidation" |
| 117 | + |
| 118 | + echo "CloudFront invalidation created: $INVALIDATION_ID" |
| 119 | +} |
| 120 | + |
| 121 | +update_index_html() { |
| 122 | + echo "Downloading index.html from S3..." |
| 123 | + aws s3 cp "$S3_BUCKET/index.html" index.html --profile "$AWS_PROFILE" || error_exit "Failed to download index.html" |
| 124 | + |
| 125 | + echo "Updating download links in index.html..." |
| 126 | + sed -i '' -e "s#https://scourhead.com/releases/.*/Scourhead-[0-9.]*-arm64.dmg#https://scourhead.com/releases/${APP_VERSION}/Scourhead-${APP_VERSION}-arm64.dmg#g" index.html |
| 127 | + sed -i '' -e "s#https://scourhead.com/releases/.*/Scourhead-Setup-x64.exe#https://scourhead.com/releases/${APP_VERSION}/Scourhead-Setup-x64.exe#g" index.html |
| 128 | + sed -i '' -e "s#https://scourhead.com/releases/.*/Scourhead-Setup-arm64.exe#https://scourhead.com/releases/${APP_VERSION}/Scourhead-Setup-arm64.exe#g" index.html |
| 129 | + sed -i '' -e "s#https://scourhead.com/releases/.*/scourhead_[0-9.]*_amd64.deb#https://scourhead.com/releases/${APP_VERSION}/scourhead_${APP_VERSION}_amd64.deb#g" index.html |
| 130 | + sed -i '' -e "s#https://scourhead.com/releases/.*/scourhead_[0-9.]*_arm64.deb#https://scourhead.com/releases/${APP_VERSION}/scourhead_${APP_VERSION}_arm64.deb#g" index.html |
| 131 | + |
| 132 | + echo "Uploading updated index.html to S3..." |
| 133 | + aws s3 cp index.html "$S3_BUCKET/index.html" --profile "$AWS_PROFILE" || error_exit "Failed to upload updated index.html" |
| 134 | + |
| 135 | + echo "Invalidating CloudFront cache for /index.html..." |
| 136 | + INVALIDATION_ID=$(aws cloudfront create-invalidation \ |
| 137 | + --distribution-id "$CLOUDFRONT_DISTRIBUTION_ID" \ |
| 138 | + --paths "/index.html" \ |
| 139 | + --profile "$AWS_PROFILE" \ |
| 140 | + --query 'Invalidation.Id' \ |
| 141 | + --output text) || error_exit "Failed to create CloudFront invalidation for /index.html" |
| 142 | + |
| 143 | + echo "CloudFront invalidation created for /index.html: $INVALIDATION_ID" |
| 144 | + |
| 145 | + rm index.html |
| 146 | +} |
| 147 | + |
| 148 | +cleanup |
| 149 | +build_upload_check "npm run build-mac" \ |
| 150 | + "release/Scourhead-${APP_VERSION}-arm64.dmg" \ |
| 151 | + "$S3_RELEASE_FOLDER/${APP_VERSION}/Scourhead-${APP_VERSION}-arm64.dmg" |
| 152 | + |
| 153 | +cleanup |
| 154 | +build_upload_check "npm run build-win-x64" \ |
| 155 | + "release/Scourhead Setup ${APP_VERSION}.exe" \ |
| 156 | + "$S3_RELEASE_FOLDER/${APP_VERSION}/Scourhead-Setup-x64.exe" |
| 157 | + |
| 158 | +cleanup |
| 159 | +build_upload_check "npm run build-win-arm64" \ |
| 160 | + "release/Scourhead Setup ${APP_VERSION}.exe" \ |
| 161 | + "$S3_RELEASE_FOLDER/${APP_VERSION}/Scourhead-Setup-arm64.exe" |
| 162 | + |
| 163 | +cleanup |
| 164 | +build_upload_check "npm run build-linux-x64" \ |
| 165 | + "release/scourhead_${APP_VERSION}_amd64.deb" \ |
| 166 | + "$S3_RELEASE_FOLDER/${APP_VERSION}/scourhead_${APP_VERSION}_amd64.deb" |
| 167 | + |
| 168 | +cleanup |
| 169 | +build_upload_check "npm run build-linux-arm64" \ |
| 170 | + "release/scourhead_${APP_VERSION}_arm64.deb" \ |
| 171 | + "$S3_RELEASE_FOLDER/${APP_VERSION}/scourhead_${APP_VERSION}_arm64.deb" |
| 172 | + |
| 173 | +cleanup |
| 174 | +invalidate_cloudfront_cache |
| 175 | +check_and_create_tag |
| 176 | +create_github_release |
| 177 | +update_index_html |
| 178 | + |
| 179 | +echo "All tasks completed successfully!" |
0 commit comments