|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Cut a release for the ddev-pressable add-on. |
| 5 | +# |
| 6 | +# DDEV add-ons are versioned by git tags: `ddev add-on get pressable/ddev-pressable` |
| 7 | +# installs the latest GitHub release, which .github/workflows/release.yml publishes |
| 8 | +# automatically when a `vX.Y.Z` tag is pushed. This helper computes the next |
| 9 | +# version from the latest tag, then creates and pushes the annotated tag. |
| 10 | +# |
| 11 | +# Usage (run from the repo root): |
| 12 | +# .github/scripts/release.rb # patch bump (default): 1.2.3 -> 1.2.4 |
| 13 | +# .github/scripts/release.rb patch # 1.2.3 -> 1.2.4 |
| 14 | +# .github/scripts/release.rb minor # 1.2.3 -> 1.3.0 |
| 15 | +# .github/scripts/release.rb major # 1.2.3 -> 2.0.0 |
| 16 | +# .github/scripts/release.rb 1.5.0 # explicit version |
| 17 | +# .github/scripts/release.rb --dry-run minor |
| 18 | + |
| 19 | +require "open3" |
| 20 | + |
| 21 | +DEFAULT_BUMP = "patch" |
| 22 | +REMOTE = "origin" |
| 23 | +MAIN_BRANCH = "main" |
| 24 | + |
| 25 | +# Run a command, aborting with its output on failure. |
| 26 | +def run(*cmd) |
| 27 | + out, status = Open3.capture2e(*cmd) |
| 28 | + abort "command failed: #{cmd.join(' ')}\n#{out}" unless status.success? |
| 29 | + out.strip |
| 30 | +end |
| 31 | + |
| 32 | +# Run a command, returning its (stripped) output and ignoring a non-zero exit. |
| 33 | +def capture(*cmd) |
| 34 | + out, = Open3.capture2e(*cmd) |
| 35 | + out.strip |
| 36 | +end |
| 37 | + |
| 38 | +dry_run = !ARGV.delete("--dry-run").nil? |
| 39 | +arg = ARGV.shift || DEFAULT_BUMP |
| 40 | + |
| 41 | +branch = run("git", "rev-parse", "--abbrev-ref", "HEAD") |
| 42 | +abort "Refusing to release from '#{branch}'; switch to '#{MAIN_BRANCH}' first." unless branch == MAIN_BRANCH |
| 43 | + |
| 44 | +abort "Working tree is dirty; commit or stash changes first." unless capture("git", "status", "--porcelain").empty? |
| 45 | + |
| 46 | +run("git", "fetch", "--tags", REMOTE) |
| 47 | +local = run("git", "rev-parse", "@") |
| 48 | +# `--verify --quiet` prints nothing and exits non-zero when there is no upstream |
| 49 | +# (e.g. CI's `git checkout -B main`), so the sync check is skipped rather than |
| 50 | +# tripping on the error text. Without it the check aborts whenever no upstream |
| 51 | +# tracking is configured. |
| 52 | +upstream = capture("git", "rev-parse", "--verify", "--quiet", "@{u}") |
| 53 | +abort "Local #{MAIN_BRANCH} is out of sync with #{REMOTE}; pull/push first." unless upstream.empty? || local == upstream |
| 54 | + |
| 55 | +# Pick the highest STRICT semver tag, ignoring any malformed `v*` tags |
| 56 | +# (e.g. `v1`, `vfoo`) that would otherwise crash the major.minor.patch split. |
| 57 | +latest = capture("git", "tag", "--list", "v*", "--sort=-v:refname") |
| 58 | + .lines.map(&:strip) |
| 59 | + .find { |t| t.match?(/\Av\d+\.\d+\.\d+\z/) } |
| 60 | +current = latest ? latest.sub(/\Av/, "") : "0.0.0" |
| 61 | +major, minor, patch = current.split(".").map(&:to_i) |
| 62 | + |
| 63 | +next_version = |
| 64 | + case arg |
| 65 | + when "major" then "#{major + 1}.0.0" |
| 66 | + when "minor" then "#{major}.#{minor + 1}.0" |
| 67 | + when "patch" then "#{major}.#{minor}.#{patch + 1}" |
| 68 | + when /\Av?\d+\.\d+\.\d+\z/ then arg.sub(/\Av/, "") |
| 69 | + else abort "Unknown argument '#{arg}'. Use major | minor | patch, or an explicit X.Y.Z." |
| 70 | + end |
| 71 | + |
| 72 | +tag = "v#{next_version}" |
| 73 | +abort "Tag #{tag} already exists." unless capture("git", "tag", "--list", tag).empty? |
| 74 | + |
| 75 | +puts "Current version: v#{current}" |
| 76 | +puts "Next version: #{tag}" |
| 77 | + |
| 78 | +if dry_run |
| 79 | + puts "[dry-run] Would create and push annotated tag #{tag} to #{REMOTE}." |
| 80 | + exit 0 |
| 81 | +end |
| 82 | + |
| 83 | +run("git", "tag", "-a", tag, "-m", "Release #{tag}") |
| 84 | +run("git", "push", REMOTE, tag) |
| 85 | + |
| 86 | +puts "Pushed #{tag}. The release workflow will publish the GitHub release." |
| 87 | +slug = capture("git", "remote", "get-url", REMOTE)[%r{github\.com[:/](.+?)(?:\.git)?\z}, 1] |
| 88 | +puts "Watch: https://github.com/#{slug}/actions" if slug |
0 commit comments