-
Notifications
You must be signed in to change notification settings - Fork 1
217 lines (208 loc) · 7.87 KB
/
Copy pathrelease.yml
File metadata and controls
217 lines (208 loc) · 7.87 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
name: Build and Release
on:
workflow_dispatch:
inputs:
version_bump:
description: Version increment
required: true
default: patch
type: choice
options:
- major
- minor
- patch
push:
branches: [main]
permissions:
contents: write
packages: write
pull-requests: write
id-token: write
concurrency:
group: release
cancel-in-progress: false
env:
BUN_CACHE_PATH: ~/.bun/install/cache
BUN_VERSION: 1.3.14
CI: true
NEXT_TELEMETRY_DISABLED: '1'
jobs:
create-version-pr:
if: ${{ github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with: { fetch-depth: 0 }
- name: Prepare version branch
id: version
shell: bash
run: |
set -euo pipefail
current_version="$(node -p "require('./package.json').version")"
IFS='.' read -r major minor patch <<< "$current_version"
case "${{ inputs.version_bump }}" in
major)
version="$((major + 1)).0.0"
;;
minor)
version="${major}.$((minor + 1)).0"
;;
patch)
version="${major}.${minor}.$((patch + 1))"
;;
*)
echo 'Unsupported version increment.' >&2
exit 1
;;
esac
branch="release/v${version}"
node -e "const fs=require('fs'); const p='package.json'; const x=JSON.parse(fs.readFileSync(p,'utf8')); x.version=process.argv[1]; fs.writeFileSync(p,JSON.stringify(x,null,2)+'\\n')" "$version"
git switch -c "$branch"
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git add package.json
git commit -m "chore: prepare release v${version}"
git push --set-upstream origin "$branch"
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "branch=$branch" >> "$GITHUB_OUTPUT"
- name: Open version pull request
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ steps.version.outputs.version }}
BRANCH: ${{ steps.version.outputs.branch }}
run: |
existing="$(gh pr list --head "$BRANCH" --base main --state open --json number --jq '.[0].number')"
if [[ -n "$existing" ]]; then exit 0; fi
gh pr create --base main --head "$BRANCH" --label release --title "chore: prepare release v${VERSION}" --body "Automated version bump for release v${VERSION}. Merge this PR to trigger the release workflow."
detect-release:
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.detect.outputs.should_release }}
version: ${{ steps.detect.outputs.version }}
release_tag: ${{ steps.detect.outputs.release_tag }}
image: ghcr.io/${{ steps.owner.outputs.owner }}/codebuddy2api
steps:
- uses: actions/checkout@v7
with: { fetch-depth: 0 }
- id: detect
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
version="$(node -p "require('./package.json').version")"
release_pr="$(gh api --paginate "/repos/${GITHUB_REPOSITORY}/commits/${GITHUB_SHA}/pulls" --jq '.[] | select(.base.ref == "main" and (.title | startswith("chore: prepare release v")) and (.head.ref | startswith("release/v"))) | .number' | head -n 1 || true)"
if [[ -z "$release_pr" ]]; then
echo 'should_release=false' >> "$GITHUB_OUTPUT"
exit 0
fi
[[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || exit 1
echo 'should_release=true' >> "$GITHUB_OUTPUT"
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "release_tag=v${version}" >> "$GITHUB_OUTPUT"
- id: owner
run: echo "owner=$(printf '%s' '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"
verify:
needs: detect-release
if: ${{ needs.detect-release.outputs.should_release == 'true' }}
name: Verify Bun / TypeScript / Coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: oven-sh/setup-bun@v2
with:
bun-version: ${{ env.BUN_VERSION }}
- uses: actions/cache@v6
with:
path: ${{ env.BUN_CACHE_PATH }}
key: ${{ runner.os }}-bun-${{ env.BUN_VERSION }}-${{ hashFiles('bun.lock') }}
restore-keys: |
${{ runner.os }}-bun-${{ env.BUN_VERSION }}-
${{ runner.os }}-bun-
- run: bun install --frozen-lockfile
- run: bun run lint
- run: bun run format:check
- run: bun run typecheck
- run: bun run test:coverage
publish-image:
needs: [detect-release, verify]
if: ${{ needs.detect-release.outputs.should_release == 'true' }}
name: Publish ${{ matrix.arch }} image
runs-on: ${{ matrix.runs_on }}
strategy:
fail-fast: false
matrix:
include:
- { arch: amd64, platform: linux/amd64, runs_on: ubuntu-24.04 }
- { arch: arm64, platform: linux/arm64, runs_on: ubuntu-24.04-arm }
steps:
- uses: actions/checkout@v7
- uses: docker/setup-buildx-action@v4
- uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- id: tags
env:
IMAGE: ${{ needs.detect-release.outputs.image }}
VERSION: ${{ needs.detect-release.outputs.version }}
run: |
echo "tags<<EOF" >> "$GITHUB_OUTPUT"
printf '%s\n' "${IMAGE}:${VERSION}-${{ matrix.arch }}" "${IMAGE}:latest-${{ matrix.arch }}" >> "$GITHUB_OUTPUT"
echo EOF >> "$GITHUB_OUTPUT"
- uses: docker/build-push-action@v7
with:
context: .
file: Dockerfile
platforms: ${{ matrix.platform }}
push: true
tags: ${{ steps.tags.outputs.tags }}
merge-image-manifests:
needs: [detect-release, publish-image]
if: ${{ needs.detect-release.outputs.should_release == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: docker/setup-buildx-action@v4
- uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- env:
IMAGE: ${{ needs.detect-release.outputs.image }}
VERSION: ${{ needs.detect-release.outputs.version }}
run: docker buildx imagetools create -t "${IMAGE}:${VERSION}" -t "${IMAGE}:latest" "${IMAGE}:${VERSION}-amd64" "${IMAGE}:${VERSION}-arm64"
release:
needs: [detect-release, merge-image-manifests]
if: ${{ needs.detect-release.outputs.should_release == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with: { fetch-depth: 0 }
- env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ needs.detect-release.outputs.release_tag }}
IMAGE: ${{ needs.detect-release.outputs.image }}:${{ needs.detect-release.outputs.version }}
run: |
set -euo pipefail
previous_tag="$(git tag --sort=-v:refname | grep '^v' | head -n 1 || true)"
if [[ -n "$previous_tag" ]]; then
commits="$(git log --pretty=format:'- %h %s' "${previous_tag}..${GITHUB_SHA}")"
else
commits="$(git log --pretty=format:'- %h %s' "${GITHUB_SHA}")"
fi
notes_file="$(mktemp)"
{
echo "Docker image: \`$IMAGE\`"
echo
echo '## Commits'
echo
if [[ -n "$commits" ]]; then
printf '%s\n' "$commits"
else
echo '- No commits found.'
fi
} > "$notes_file"
gh release create "$TAG" --target "$GITHUB_SHA" --title "$TAG" --latest --notes-file "$notes_file"