-
Notifications
You must be signed in to change notification settings - Fork 1
172 lines (150 loc) · 6 KB
/
Copy pathrelease.yml
File metadata and controls
172 lines (150 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
name: Release
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Version to release (e.g. 1.0.0 or 1.0.0-rc.2). The leading "v" is added automatically.'
required: true
type: string
permissions:
contents: write
env:
PACKAGE_PATH: .
CHANGELOG_PATH: CHANGELOG.md
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Determine version
id: version
run: |
set -euo pipefail
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
VERSION="${{ inputs.version }}"
else
VERSION="${GITHUB_REF_NAME}"
fi
VERSION="${VERSION#v}"
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::Version '$VERSION' is not a valid SemVer string."
exit 1
fi
TAG="v$VERSION"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
if [[ "$VERSION" == *-* ]]; then
echo "prerelease=true" >> "$GITHUB_OUTPUT"
else
echo "prerelease=false" >> "$GITHUB_OUTPUT"
fi
echo "Releasing $TAG"
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Check tag does not already exist
if: github.event_name == 'workflow_dispatch'
run: |
set -euo pipefail
TAG="${{ steps.version.outputs.tag }}"
if git rev-parse --verify --quiet "refs/tags/$TAG" >/dev/null; then
echo "::error::Tag $TAG already exists locally. Delete it or pick another version."
exit 1
fi
if git ls-remote --exit-code --tags origin "$TAG" >/dev/null 2>&1; then
echo "::error::Tag $TAG already exists on origin."
exit 1
fi
- name: Validate package.json version
run: |
set -euo pipefail
VERSION="${{ steps.version.outputs.version }}"
PKG_VERSION=$(jq -r .version "$PACKAGE_PATH/package.json")
if [[ "$PKG_VERSION" != "$VERSION" ]]; then
echo "::error file=$PACKAGE_PATH/package.json::package.json version ($PKG_VERSION) does not match release version ($VERSION). Bump it before tagging."
exit 1
fi
echo "package.json version matches: $PKG_VERSION"
- name: Extract CHANGELOG section
id: changelog
run: |
set -euo pipefail
VERSION="${{ steps.version.outputs.version }}"
NOTES=$(awk -v ver="$VERSION" '
BEGIN { capture = 0 }
/^## \[/ {
if (capture) { exit }
if ($0 ~ "^## \\[" ver "\\]([^0-9A-Za-z.-]|$)") { capture = 1; next }
}
capture { print }
' "$CHANGELOG_PATH")
if [[ -z "$(echo "$NOTES" | tr -d '[:space:]')" ]]; then
echo "::error file=$CHANGELOG_PATH::No CHANGELOG section found for version $VERSION. Add a '## [$VERSION] — YYYY-MM-DD' heading before releasing."
exit 1
fi
{
echo "notes<<__CHANGELOG_EOF__"
echo "$NOTES"
echo "__CHANGELOG_EOF__"
} >> "$GITHUB_OUTPUT"
echo "CHANGELOG section for $VERSION extracted ($(echo "$NOTES" | wc -l) lines)."
- name: Create and push release tag
if: github.event_name == 'workflow_dispatch'
run: |
set -euo pipefail
TAG="${{ steps.version.outputs.tag }}"
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
- name: Checkout release tag
run: |
set -euo pipefail
git fetch origin "refs/tags/${{ steps.version.outputs.tag }}:refs/tags/${{ steps.version.outputs.tag }}" || true
git checkout "${{ steps.version.outputs.tag }}"
- name: Publish UPM tag and branch
run: |
set -euo pipefail
VERSION="${{ steps.version.outputs.version }}"
TAG="${{ steps.version.outputs.tag }}"
PRERELEASE="${{ steps.version.outputs.prerelease }}"
UPM_TAG="upm/$VERSION"
if git ls-remote --exit-code --tags origin "$UPM_TAG" >/dev/null 2>&1; then
echo "::error::Tag $UPM_TAG already exists on origin. Refusing to overwrite a published UPM tag."
exit 1
fi
# Build a tree containing only the Unity package contents — strip
# repo-only paths so consumers don't pull workflows, CI configs, etc.
UPM_INDEX=$(mktemp)
trap 'rm -f "$UPM_INDEX"' EXIT
export GIT_INDEX_FILE="$UPM_INDEX"
git read-tree "$TAG"
git rm -r --cached --quiet --ignore-unmatch .github CLAUDE.md CLAUDE.md.meta .gitignore .gitignore.meta
TREE=$(git write-tree)
unset GIT_INDEX_FILE
PARENT=$(git rev-parse "$TAG^{commit}")
COMMIT=$(git commit-tree "$TREE" -p "$PARENT" -m "UPM release $VERSION")
# Move the appropriate upm branch forward:
# pre-releases → upm-preview, stable releases → upm.
if [[ "$PRERELEASE" == "true" ]]; then
UPM_BRANCH="upm-preview"
else
UPM_BRANCH="upm"
fi
git push --force origin "$COMMIT:refs/heads/$UPM_BRANCH"
# Immutable per-version tag for users who pin to a specific release.
git tag "$UPM_TAG" "$COMMIT"
git push origin "$UPM_TAG"
echo "Published $UPM_TAG and updated $UPM_BRANCH branch."
- name: Create GitHub release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.version.outputs.tag }}
name: ${{ steps.version.outputs.tag }}
body: ${{ steps.changelog.outputs.notes }}
prerelease: ${{ steps.version.outputs.prerelease == 'true' }}