Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
name: test
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: bash -n release/*.sh
|
||||
- run: bash test/release.test.sh
|
||||
@@ -0,0 +1,58 @@
|
||||
# ci-shared
|
||||
|
||||
Shared Gitea Actions steps for the extant2000 repos. Public, holds no secrets.
|
||||
|
||||
## release (auto-release)
|
||||
|
||||
Computes the next semver from Conventional Commits since the last `vX.Y.Z`
|
||||
tag, then creates an annotated tag and a Gitea release on the released commit.
|
||||
|
||||
- `type!:` or a `BREAKING CHANGE:` footer = major, `feat:` = minor, any other commit = patch.
|
||||
- Merge commits and commits with `[skip release]` in the subject do not count.
|
||||
No counted commit since the last tag = no release.
|
||||
- First release (no tag yet) = the version the repo declares: `initial-version` input,
|
||||
`VERSION`, `package.json`, a WordPress plugin `Version:` header, `MARKETING_VERSION`,
|
||||
Gradle `versionName`, else `0.1.0`.
|
||||
- Release notes group commits under Features, Fixes and Other, each linked.
|
||||
- Optional: `image` pushes `IMAGE:vX.Y.Z` next to `IMAGE:<sha8>`; `npm-token`
|
||||
publishes the package as `X.Y.Z` (skipped when that version is already in the registry).
|
||||
`package.json` in git is not rewritten.
|
||||
- A rerun on a released commit creates nothing new, but still outputs the version,
|
||||
so a failed image push or npm publish can be retried.
|
||||
|
||||
Deploy repo, after the deploy job:
|
||||
|
||||
```yaml
|
||||
release:
|
||||
needs: [deploy]
|
||||
if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/main'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with: { fetch-depth: 0 }
|
||||
- uses: https://git.extant2000.com/extant2000/ci-shared/release@v1
|
||||
with:
|
||||
token: ${{ secrets.RELEASE_TOKEN }}
|
||||
image: git.extant2000.com/extant2000/<image>
|
||||
registry-token: ${{ secrets.REGISTRY_TOKEN }}
|
||||
```
|
||||
|
||||
Library, after the tests:
|
||||
|
||||
```yaml
|
||||
- uses: https://git.extant2000.com/extant2000/ci-shared/release@v1
|
||||
with:
|
||||
token: ${{ secrets.RELEASE_TOKEN }}
|
||||
npm-token: ${{ secrets.NPM_TOKEN }}
|
||||
```
|
||||
|
||||
`RELEASE_TOKEN` is an org secret (extant2000git, write:repository). Outputs:
|
||||
`released`, `version`, `tag`, `previous`.
|
||||
|
||||
## Versioning
|
||||
|
||||
Workflows pin `@v1`. Compatible changes move the `v1` tag; breaking changes get `v2`.
|
||||
|
||||
## Tests
|
||||
|
||||
`bash test/release.test.sh` (runs in the `test` workflow).
|
||||
@@ -0,0 +1,75 @@
|
||||
name: auto-release
|
||||
description: >
|
||||
Next semver from Conventional Commits since the last vX.Y.Z tag. Creates an
|
||||
annotated tag and a Gitea release on the released commit, and optionally
|
||||
pushes IMAGE:vX.Y.Z and publishes the npm package as X.Y.Z.
|
||||
inputs:
|
||||
token:
|
||||
description: Gitea token that can create tags and releases in this repo.
|
||||
required: true
|
||||
sha:
|
||||
description: Commit to release.
|
||||
default: ${{ github.sha }}
|
||||
initial-version:
|
||||
description: First-release version when the repo has no release tag (default detected, else 0.1.0).
|
||||
default: ''
|
||||
version-file:
|
||||
description: File to read the first-release version from.
|
||||
default: ''
|
||||
image:
|
||||
description: Image to tag, e.g. git.extant2000.com/extant2000/app. Empty = no image.
|
||||
default: ''
|
||||
image-source-tag:
|
||||
description: Already pushed tag to copy (default the first 8 characters of sha).
|
||||
default: ''
|
||||
registry-user:
|
||||
default: extant2000git
|
||||
registry-token:
|
||||
default: ''
|
||||
npm-token:
|
||||
description: Set to publish the package in the working directory as X.Y.Z.
|
||||
default: ''
|
||||
npm-registry:
|
||||
default: https://git.extant2000.com/api/packages/extant2000-pkg/npm/
|
||||
dry-run:
|
||||
default: 'false'
|
||||
outputs:
|
||||
released:
|
||||
description: "'true' when this run created the release."
|
||||
value: ${{ steps.release.outputs.released }}
|
||||
version:
|
||||
description: X.Y.Z of the release on this commit (empty when there is none).
|
||||
value: ${{ steps.release.outputs.version }}
|
||||
tag:
|
||||
value: ${{ steps.release.outputs.tag }}
|
||||
previous:
|
||||
value: ${{ steps.release.outputs.previous }}
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- id: release
|
||||
shell: bash
|
||||
env:
|
||||
RELEASE_TOKEN: ${{ inputs.token }}
|
||||
SHA: ${{ inputs.sha }}
|
||||
INITIAL_VERSION: ${{ inputs.initial-version }}
|
||||
VERSION_FILE: ${{ inputs.version-file }}
|
||||
DRY_RUN: ${{ inputs.dry-run == 'true' && '1' || '0' }}
|
||||
run: bash "${{ github.action_path }}/release.sh"
|
||||
- if: inputs.image != '' && steps.release.outputs.version != '' && inputs.dry-run != 'true'
|
||||
shell: bash
|
||||
env:
|
||||
IMAGE: ${{ inputs.image }}
|
||||
SOURCE_TAG: ${{ inputs.image-source-tag }}
|
||||
SHA: ${{ inputs.sha }}
|
||||
VERSION_TAG: ${{ steps.release.outputs.tag }}
|
||||
REGISTRY_USER: ${{ inputs.registry-user }}
|
||||
REGISTRY_TOKEN: ${{ inputs.registry-token }}
|
||||
run: SOURCE_TAG=${SOURCE_TAG:-${SHA::8}} bash "${{ github.action_path }}/image-tag.sh"
|
||||
- if: inputs.npm-token != '' && steps.release.outputs.version != '' && inputs.dry-run != 'true'
|
||||
shell: bash
|
||||
env:
|
||||
VERSION: ${{ steps.release.outputs.version }}
|
||||
NPM_TOKEN: ${{ inputs.npm-token }}
|
||||
NPM_REGISTRY: ${{ inputs.npm-registry }}
|
||||
run: bash "${{ github.action_path }}/npm-publish.sh"
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
# Pushes IMAGE:VERSION_TAG next to the already pushed IMAGE:SOURCE_TAG.
|
||||
# Environment: IMAGE, SOURCE_TAG, VERSION_TAG, REGISTRY_USER, REGISTRY_TOKEN (never printed).
|
||||
set -euo pipefail
|
||||
: "${IMAGE:?}" "${SOURCE_TAG:?}" "${VERSION_TAG:?}" "${REGISTRY_USER:?}" "${REGISTRY_TOKEN:?}"
|
||||
if ! command -v docker >/dev/null 2>&1; then
|
||||
curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-27.5.1.tgz | tar xz -C /tmp
|
||||
install /tmp/docker/docker /usr/local/bin/docker
|
||||
fi
|
||||
REGISTRY=${IMAGE%%/*}
|
||||
printf '%s' "$REGISTRY_TOKEN" | docker login "$REGISTRY" -u "$REGISTRY_USER" --password-stdin >/dev/null
|
||||
trap 'docker logout "$REGISTRY" >/dev/null 2>&1 || true' EXIT
|
||||
docker pull -q "$IMAGE:$SOURCE_TAG"
|
||||
docker tag "$IMAGE:$SOURCE_TAG" "$IMAGE:$VERSION_TAG"
|
||||
for i in 1 2 3; do docker push -q "$IMAGE:$VERSION_TAG" && break; [ "$i" = 3 ] && exit 1; sleep 10; done
|
||||
echo "pushed $IMAGE:$VERSION_TAG = $IMAGE:$SOURCE_TAG"
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
# Publishes the package in the working directory as VERSION, unless that
|
||||
# version is already in the registry. package.json in git is not changed.
|
||||
# Environment: VERSION (X.Y.Z), NPM_TOKEN (never printed), NPM_REGISTRY.
|
||||
set -euo pipefail
|
||||
: "${VERSION:?}" "${NPM_TOKEN:?}"
|
||||
REG=${NPM_REGISTRY:-https://git.extant2000.com/api/packages/extant2000-pkg/npm/}
|
||||
AUTH="//${REG#https://}:_authToken=\${NPM_TOKEN}"
|
||||
grep -qF "$AUTH" .npmrc 2>/dev/null || printf '%s\n' "$AUTH" >> .npmrc
|
||||
NAME=$(node -p 'require("./package.json").name')
|
||||
if [ "$(npm view "$NAME@$VERSION" version --registry "$REG" 2>/dev/null || true)" = "$VERSION" ]; then
|
||||
echo "$NAME@$VERSION is already published"; exit 0
|
||||
fi
|
||||
npm version "$VERSION" --no-git-tag-version --allow-same-version >/dev/null
|
||||
npm publish --registry "$REG"
|
||||
echo "published $NAME@$VERSION to $REG"
|
||||
Executable
+223
@@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env bash
|
||||
# Computes the next semver from Conventional Commits and, unless DRY_RUN=1,
|
||||
# creates an annotated tag vX.Y.Z on the released commit plus a Gitea release.
|
||||
#
|
||||
# Rules
|
||||
# - Last release = highest vX.Y.Z (or bare X.Y.Z) tag reachable from SHA.
|
||||
# - Range = last..SHA, merge commits and "[skip release]" commits ignored.
|
||||
# - "type!:" header or a "BREAKING CHANGE:" / "BREAKING-CHANGE:" footer = major,
|
||||
# "feat:" = minor, any other commit = patch. Empty range = no release.
|
||||
# - No release tag yet: the first release is the repo's current version
|
||||
# (INITIAL_VERSION, then VERSION file, package.json, WordPress plugin
|
||||
# header, MARKETING_VERSION, Gradle versionName), else 0.1.0.
|
||||
# - SHA already carries a release tag: no new release; outputs that version.
|
||||
#
|
||||
# Environment
|
||||
# SHA commit to release (default GITHUB_SHA, then HEAD)
|
||||
# RELEASE_TOKEN Gitea token with write access to the repo (never printed)
|
||||
# GITHUB_SERVER_URL, GITHUB_REPOSITORY set by Gitea Actions
|
||||
# INITIAL_VERSION optional first-release override (X.Y.Z)
|
||||
# VERSION_FILE optional file to read the first-release version from
|
||||
# DRY_RUN=1 compute and print only
|
||||
#
|
||||
# Outputs (stdout lines and $GITHUB_OUTPUT): released, version, tag, previous, bump
|
||||
set -euo pipefail
|
||||
|
||||
SEMVER_RE='^v?[0-9]+\.[0-9]+\.[0-9]+$'
|
||||
|
||||
log() { printf '%s\n' "$*" >&2; }
|
||||
|
||||
# Highest semver among stdin lines, printed without a leading v.
|
||||
max_semver() {
|
||||
grep -E "$SEMVER_RE" | sed 's/^v//' | awk -F. '{ printf "%010d %010d %010d %s\n", $1, $2, $3, $0 }' |
|
||||
sort | tail -n 1 | awk '{ print $4 }'
|
||||
}
|
||||
|
||||
# Classify one commit message (full text on stdin): major, minor or patch.
|
||||
classify() {
|
||||
local msg header
|
||||
msg=$(cat)
|
||||
header=$(printf '%s\n' "$msg" | head -n 1)
|
||||
if printf '%s\n' "$header" | grep -Eq '^[A-Za-z]+(\([^)]*\))?!:'; then echo major; return; fi
|
||||
if printf '%s\n' "$msg" | tail -n +2 | grep -Eq '^BREAKING[ -]CHANGE:'; then echo major; return; fi
|
||||
if printf '%s\n' "$header" | grep -Eiq '^feat(\([^)]*\))?:'; then echo minor; return; fi
|
||||
echo patch
|
||||
}
|
||||
|
||||
# bump X.Y.Z kind -> X'.Y'.Z'
|
||||
bump() {
|
||||
local v=$1 kind=$2 ma mi pa
|
||||
IFS=. read -r ma mi pa <<<"$v"
|
||||
case $kind in
|
||||
major) echo "$((ma + 1)).0.0" ;;
|
||||
minor) echo "$ma.$((mi + 1)).0" ;;
|
||||
patch) echo "$ma.$mi.$((pa + 1))" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Release-relevant commits in a range, oldest first (merges and skips removed).
|
||||
range_commits() {
|
||||
local range=$1 sha
|
||||
for sha in $(git rev-list --reverse --no-merges "$range"); do
|
||||
if git log -1 --format=%B "$sha" | head -n 1 | grep -qiF '[skip release]'; then continue; fi
|
||||
echo "$sha"
|
||||
done
|
||||
}
|
||||
|
||||
# Version the repo declares today, for the first release.
|
||||
current_version() {
|
||||
local v="" f
|
||||
if [ -n "${INITIAL_VERSION:-}" ]; then v=$INITIAL_VERSION
|
||||
elif [ -n "${VERSION_FILE:-}" ] && [ -f "$VERSION_FILE" ]; then
|
||||
v=$(grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' "$VERSION_FILE" | head -n 1 || true)
|
||||
elif [ -f VERSION ]; then
|
||||
v=$(grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' VERSION | head -n 1 || true)
|
||||
elif [ -f package.json ]; then
|
||||
v=$(sed -n 's/^ "version": *"\([0-9]*\.[0-9]*\.[0-9]*\)".*/\1/p' package.json | head -n 1)
|
||||
fi
|
||||
if [ -z "$v" ]; then
|
||||
for f in *.php */*.php; do
|
||||
[ -f "$f" ] || continue
|
||||
v=$(head -n 40 "$f" | sed -n 's/^[ *#]*Version: *\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p' | head -n 1)
|
||||
[ -n "$v" ] && break
|
||||
done
|
||||
fi
|
||||
if [ -z "$v" ]; then
|
||||
v=$(git ls-files '*.yml' '*.yaml' '*.xcconfig' '*.pbxproj' 2>/dev/null | head -n 200 |
|
||||
xargs grep -hEo 'MARKETING_VERSION *[:=] *"?[0-9]+\.[0-9]+\.[0-9]+' 2>/dev/null |
|
||||
grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | max_semver || true)
|
||||
fi
|
||||
if [ -z "$v" ]; then
|
||||
v=$(git ls-files '*build.gradle' '*build.gradle.kts' 2>/dev/null |
|
||||
xargs grep -hEo 'versionName *=? *"[0-9]+\.[0-9]+\.[0-9]+"' 2>/dev/null |
|
||||
grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | max_semver || true)
|
||||
fi
|
||||
[ -n "$v" ] || v=0.1.0
|
||||
printf '%s\n' "$v" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' || { log "bad initial version: $v"; return 1; }
|
||||
echo "$v"
|
||||
}
|
||||
|
||||
# Sets RELEASED VERSION TAG PREVIOUS BUMP COMMITS for $SHA.
|
||||
compute() {
|
||||
local sha=$1 here last kinds c k
|
||||
RELEASED=false VERSION="" TAG="" PREVIOUS="" BUMP="" COMMITS=""
|
||||
here=$(git tag --points-at "$sha" | max_semver || true)
|
||||
if [ -n "$here" ]; then
|
||||
VERSION=$here; TAG="v$here"; PREVIOUS=$here; BUMP=none
|
||||
return
|
||||
fi
|
||||
last=$(git tag --merged "$sha" | max_semver || true)
|
||||
if [ -z "$last" ]; then
|
||||
VERSION=$(current_version); TAG="v$VERSION"; BUMP=initial; RELEASED=true
|
||||
COMMITS=$(range_commits "$sha")
|
||||
return
|
||||
fi
|
||||
PREVIOUS=$last
|
||||
local lasttag
|
||||
lasttag=$(git tag --merged "$sha" | grep -E "^v?${last//./\\.}\$" | head -n 1)
|
||||
COMMITS=$(range_commits "$lasttag..$sha")
|
||||
if [ -z "$COMMITS" ]; then BUMP=none; return; fi
|
||||
BUMP=patch
|
||||
for c in $COMMITS; do
|
||||
k=$(git log -1 --format=%B "$c" | classify)
|
||||
if [ "$k" = major ]; then BUMP=major; break; fi
|
||||
[ "$k" = minor ] && BUMP=minor
|
||||
done
|
||||
VERSION=$(bump "$last" "$BUMP"); TAG="v$VERSION"; RELEASED=true
|
||||
}
|
||||
|
||||
# Markdown release notes grouped by type.
|
||||
notes() {
|
||||
local base=$1 repo=$2 c subj short kind feats="" fixes="" other="" line list total
|
||||
total=$(printf '%s\n' $COMMITS | grep -c . || true)
|
||||
list=$(printf '%s\n' $COMMITS | tail -n "${NOTES_MAX:-100}")
|
||||
for c in $list; do
|
||||
subj=$(git log -1 --format=%s "$c")
|
||||
short=${c:0:8}
|
||||
kind=$(git log -1 --format=%B "$c" | classify)
|
||||
line="- ${subj} ([${short}](${base}/${repo}/commit/${c}))"
|
||||
[ "$kind" = major ] && line="$line **breaking**"
|
||||
if printf '%s\n' "$subj" | grep -Eiq '^feat(\([^)]*\))?!?:'; then feats="$feats$line"$'\n'
|
||||
elif printf '%s\n' "$subj" | grep -Eiq '^fix(\([^)]*\))?!?:'; then fixes="$fixes$line"$'\n'
|
||||
else other="$other$line"$'\n'; fi
|
||||
done
|
||||
printf 'Commit [%s](%s/%s/commit/%s).' "${SHA:0:12}" "$base" "$repo" "$SHA"
|
||||
if [ -n "$PREVIOUS" ]; then
|
||||
printf ' Changes since [v%s](%s/%s/compare/v%s...%s).' "$PREVIOUS" "$base" "$repo" "$PREVIOUS" "$TAG"
|
||||
else
|
||||
printf ' First release.'
|
||||
fi
|
||||
printf '\n'
|
||||
[ -n "$feats" ] && printf '\n### Features\n\n%s' "$feats"
|
||||
[ -n "$fixes" ] && printf '\n### Fixes\n\n%s' "$fixes"
|
||||
[ -n "$other" ] && printf '\n### Other\n\n%s' "$other"
|
||||
if [ "$total" -gt "${NOTES_MAX:-100}" ]; then
|
||||
printf '\n%s earlier commits are not listed.\n' "$((total - ${NOTES_MAX:-100}))"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
json() { # json key value [key value ...] -> one JSON object of strings/bools
|
||||
if command -v node >/dev/null 2>&1; then
|
||||
node -e 'const a=process.argv.slice(1),o={};for(let i=0;i<a.length;i+=2){o[a[i]]=a[i+1]==="__false__"?false:a[i+1]}process.stdout.write(JSON.stringify(o))' "$@"
|
||||
else
|
||||
python3 -c 'import json,sys;a=sys.argv[1:];print(json.dumps({a[i]:(False if a[i+1]=="__false__" else a[i+1]) for i in range(0,len(a),2)}))' "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
api() { # api METHOD PATH JSON -> prints HTTP code, body to $API_BODY file
|
||||
curl -sS -o "$API_BODY" -w '%{http_code}' -X "$1" \
|
||||
-H "Authorization: token $RELEASE_TOKEN" -H 'Content-Type: application/json' \
|
||||
"$BASE/api/v1/repos/$REPO$2" --data-binary "$3"
|
||||
}
|
||||
|
||||
emit() {
|
||||
local k
|
||||
for k in released version tag previous bump; do
|
||||
local v
|
||||
case $k in released) v=$RELEASED ;; version) v=$VERSION ;; tag) v=$TAG ;; previous) v=$PREVIOUS ;; bump) v=$BUMP ;; esac
|
||||
echo "$k=$v"
|
||||
[ -n "${GITHUB_OUTPUT:-}" ] && echo "$k=$v" >>"$GITHUB_OUTPUT"
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
main() {
|
||||
SHA=${SHA:-${GITHUB_SHA:-$(git rev-parse HEAD)}}
|
||||
SHA=$(git rev-parse "$SHA^{commit}")
|
||||
BASE=${GITHUB_SERVER_URL:-https://git.extant2000.com}
|
||||
REPO=${GITHUB_REPOSITORY:-}
|
||||
if [ "${SKIP_FETCH:-0}" != 1 ]; then
|
||||
if [ "$(git rev-parse --is-shallow-repository)" = true ]; then git fetch -q --unshallow --tags --force origin
|
||||
else git fetch -q --tags --force origin; fi
|
||||
fi
|
||||
compute "$SHA"
|
||||
if [ "$RELEASED" != true ]; then
|
||||
log "no release: ${TAG:+$SHA is already $TAG}${TAG:-no commits since v$PREVIOUS}"
|
||||
emit; return 0
|
||||
fi
|
||||
log "release $TAG ($BUMP${PREVIOUS:+ from v$PREVIOUS}) at $SHA"
|
||||
local body
|
||||
body=$(notes "$BASE" "$REPO")
|
||||
if [ "${DRY_RUN:-0}" = 1 ]; then printf '%s\n' "$body" >&2; emit; return 0; fi
|
||||
[ -n "${RELEASE_TOKEN:-}" ] || { log "RELEASE_TOKEN is empty"; return 1; }
|
||||
[ -n "$REPO" ] || { log "GITHUB_REPOSITORY is empty"; return 1; }
|
||||
API_BODY=$(mktemp); trap 'rm -f "$API_BODY"' EXIT
|
||||
local code
|
||||
code=$(api POST /tags "$(json tag_name "$TAG" target "$SHA" message "Release $TAG")")
|
||||
if [ "$code" != 201 ]; then
|
||||
git fetch -q --tags --force origin || true
|
||||
if [ "$(git rev-parse -q --verify "refs/tags/$TAG^{commit}" || true)" = "$SHA" ]; then
|
||||
log "tag $TAG already on $SHA (concurrent run); not releasing twice"
|
||||
RELEASED=false; emit; return 0
|
||||
fi
|
||||
log "creating tag $TAG failed: HTTP $code $(head -c 300 "$API_BODY")"; return 1
|
||||
fi
|
||||
code=$(api POST /releases "$(json tag_name "$TAG" target_commitish "$SHA" name "$TAG" body "$body" draft __false__ prerelease __false__)")
|
||||
[ "$code" = 201 ] || { log "creating release $TAG failed: HTTP $code $(head -c 300 "$API_BODY")"; return 1; }
|
||||
log "released $BASE/$REPO/releases/tag/$TAG"
|
||||
emit
|
||||
}
|
||||
|
||||
if [ "${BASH_SOURCE[0]}" = "$0" ]; then main "$@"; fi
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for release/release.sh version calculation. Each case builds a
|
||||
# throwaway git repo and runs the script with DRY_RUN=1 (no network).
|
||||
set -uo pipefail
|
||||
HERE=$(cd "$(dirname "$0")" && pwd)
|
||||
SCRIPT="$HERE/../release/release.sh"
|
||||
PASS=0 FAIL=0
|
||||
export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@example.com GIT_COMMITTER_NAME=t GIT_COMMITTER_EMAIL=t@example.com
|
||||
|
||||
repo() { D=$(mktemp -d); cd "$D" && git init -q -b main && git commit -q --allow-empty -m "chore: init"; }
|
||||
c() { git commit -q --allow-empty -m "$1"; }
|
||||
run() { ( unset GITHUB_OUTPUT; DRY_RUN=1 SKIP_FETCH=1 GITHUB_REPOSITORY=o/r "$@" bash "$SCRIPT" 2>"$D/.err" ); }
|
||||
expect() { # name expected-released expected-version [env...]
|
||||
local name=$1 rel=$2 ver=$3 out; shift 3
|
||||
out=$(run env "$@")
|
||||
local got_rel got_ver
|
||||
got_rel=$(printf '%s\n' "$out" | sed -n 's/^released=//p'); got_ver=$(printf '%s\n' "$out" | sed -n 's/^version=//p')
|
||||
if [ "$got_rel" = "$rel" ] && [ "$got_ver" = "$ver" ]; then PASS=$((PASS + 1)); echo "ok $name"
|
||||
else FAIL=$((FAIL + 1)); echo "FAIL $name: want released=$rel version=$ver, got released=$got_rel version=$got_ver"; cat "$D/.err"; fi
|
||||
}
|
||||
expect_err() { # name pattern: stderr of the last run contains pattern
|
||||
if grep -qF -- "$2" "$D/.err"; then PASS=$((PASS + 1)); echo "ok $1"
|
||||
else FAIL=$((FAIL + 1)); echo "FAIL $1: stderr lacks '$2'"; cat "$D/.err"; fi
|
||||
}
|
||||
|
||||
# First release
|
||||
repo; printf '{\n "name": "x",\n "version": "1.4.2"\n}\n' > package.json; git add .; c "feat: x"
|
||||
expect "first release = package.json version" true 1.4.2
|
||||
repo; c "fix: y"
|
||||
expect "first release with no manifest = 0.1.0" true 0.1.0
|
||||
repo; printf '<?php\n/**\n * Plugin Name: P\n * Version: 2.3.4\n */\n' > p.php; git add .; c "feat: p"
|
||||
expect "first release = WordPress plugin header" true 2.3.4
|
||||
repo; printf 'settings:\n MARKETING_VERSION: "1.7.0"\n' > project.yml; git add .; c "fix: ios"
|
||||
expect "first release = MARKETING_VERSION" true 1.7.0
|
||||
repo; printf 'android {\n defaultConfig {\n versionName = "3.1.4"\n }\n}\n' > build.gradle.kts; git add .; c "fix: a"
|
||||
expect "first release = Gradle versionName" true 3.1.4
|
||||
repo; printf '{\n "version": "1.4.2"\n}\n' > package.json; git add .; c "x"
|
||||
expect "INITIAL_VERSION overrides detection" true 5.0.0 INITIAL_VERSION=5.0.0
|
||||
repo; printf '9.9.9\n' > VERSION; printf '{\n "version": "1.4.2"\n}\n' > package.json; git add .; c "x"
|
||||
expect "VERSION file wins over package.json" true 9.9.9
|
||||
repo; git tag 0.3.0; c "fix: bare tag"
|
||||
expect "bare X.Y.Z tag counts as the last release" true 0.3.1
|
||||
|
||||
# Bumps after a tag
|
||||
repo; git tag -a v1.2.3 -m r; c "fix: a"
|
||||
expect "fix = patch" true 1.2.4
|
||||
repo; git tag v1.2.3; c "docs: a"; c "chore(deps): b"; c "Update README"
|
||||
expect "docs/chore/non-conventional = patch" true 1.2.4
|
||||
repo; git tag v1.2.3; c "fix: a"; c "feat(ui): b"; c "chore: c"
|
||||
expect "feat = minor" true 1.3.0
|
||||
repo; git tag v1.2.3; c "Feat: capital"
|
||||
expect "type is case-insensitive" true 1.3.0
|
||||
repo; git tag v1.2.3; c "feature: not a type"
|
||||
expect "feature: is not feat = patch" true 1.2.4
|
||||
repo; git tag v1.2.3; c "feat(api)!: drop v1"
|
||||
expect "type! = major" true 2.0.0
|
||||
repo; git tag v1.2.3; c "fix!: drop"
|
||||
expect "fix! = major" true 2.0.0
|
||||
repo; git tag v1.2.3; c "$(printf 'refactor: x\n\nBREAKING CHANGE: config renamed')"
|
||||
expect "BREAKING CHANGE footer = major" true 2.0.0
|
||||
repo; git tag v1.2.3; c "$(printf 'fix: x\n\nThis is not a BREAKING CHANGE: mid-line')"
|
||||
expect "BREAKING CHANGE mid-line is not a footer" true 1.2.4
|
||||
repo; git tag v0.9.0; git tag v0.10.0; c "fix: a"
|
||||
expect "highest tag wins (0.10.0 > 0.9.0)" true 0.10.1
|
||||
|
||||
# No release
|
||||
repo; c "feat: a"; git tag v1.2.3
|
||||
expect "no commits since tag = no new release, reports the tag" false 1.2.3
|
||||
repo; git tag v1.2.3; c "chore: bump [skip release]"
|
||||
expect "only [skip release] commits = no release" false ""
|
||||
repo; git tag v1.2.3; git checkout -q -b f; c "feat: on branch"; git checkout -q main; git merge -q --no-ff f -m "Merge branch f"
|
||||
expect "merge commit ignored, branch feat counts" true 1.3.0
|
||||
repo; c "fix: a"; T=$(git rev-parse HEAD); git tag v2.0.0; c "fix: b"
|
||||
expect "SHA override releases an older commit" false 2.0.0 SHA="$T"
|
||||
|
||||
# Notes
|
||||
repo; git tag v1.0.0; c "feat: add x"; c "fix(db): y"; c "ci: z"; c "feat!: w"
|
||||
run env >/dev/null
|
||||
expect_err "notes: Features section" "### Features"
|
||||
expect_err "notes: Fixes section" "### Fixes"
|
||||
expect_err "notes: Other section" "### Other"
|
||||
expect_err "notes: commit link" "(https://git.extant2000.com/o/r/commit/"
|
||||
expect_err "notes: breaking marked" "**breaking**"
|
||||
expect_err "notes: compare link" "compare/v1.0.0...v2.0.0"
|
||||
|
||||
echo "$PASS passed, $FAIL failed"
|
||||
[ "$FAIL" = 0 ]
|
||||
Reference in New Issue
Block a user