-
-
Notifications
You must be signed in to change notification settings - Fork 2
225 lines (193 loc) · 8.17 KB
/
release.yml
File metadata and controls
225 lines (193 loc) · 8.17 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
218
219
220
221
222
223
name: Build & Publish Docker
on:
repository_dispatch:
types: [release]
workflow_dispatch:
inputs:
version:
description: 'App version (e.g. 2.1.0 or 2.1.0-rc.1)'
required: true
dry_run:
description: 'Dry run (draft release, no commit, no latest tag)'
type: boolean
default: false
pe_version:
description: 'PE library version (defaults to app version if not set)'
required: false
permissions:
contents: write
packages: write
env:
DOTNET_VERSION: '10.0.x'
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Determine parameters
id: params
run: |
if [ "${{ github.event_name }}" = "repository_dispatch" ]; then
VERSION="${{ github.event.client_payload.version }}"
DRY_RUN="${{ github.event.client_payload.dry_run }}"
NUGET_SOURCE="${{ github.event.client_payload.nuget_source }}"
else
VERSION="${{ inputs.version }}"
DRY_RUN="${{ inputs.dry_run }}"
NUGET_SOURCE="github"
fi
# Default to github if not specified (backward compatible)
if [ -z "${NUGET_SOURCE}" ]; then
NUGET_SOURCE="github"
fi
if [ "${{ github.event_name }}" = "repository_dispatch" ]; then
PE_VERSION="${{ github.event.client_payload.pe_version }}"
else
PE_VERSION="${{ inputs.pe_version }}"
fi
# Default pe_version to version if not set (initial release: both identical)
if [ -z "${PE_VERSION}" ]; then
PE_VERSION="${VERSION}"
fi
if [[ "$VERSION" == *-* ]]; then
IS_PRERELEASE="true"
else
IS_PRERELEASE="false"
fi
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "is_prerelease=${IS_PRERELEASE}" >> $GITHUB_OUTPUT
echo "dry_run=${DRY_RUN}" >> $GITHUB_OUTPUT
echo "nuget_source=${NUGET_SOURCE}" >> $GITHUB_OUTPUT
echo "pe_version=${PE_VERSION}" >> $GITHUB_OUTPUT
IMAGE_TAG=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
- uses: actions/checkout@v4
with:
token: ${{ secrets.PAT_DISPATCH }}
fetch-depth: 0
# ── Version Guard ────────────────────────────────
- name: Check if version already exists
if: steps.params.outputs.dry_run != 'true'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.PAT_DISPATCH }}
script: |
const version = '${{ steps.params.outputs.version }}';
const tag = `v${version}`;
const owner = context.repo.owner;
const repo = context.repo.repo;
const errors = [];
try {
await github.rest.git.getRef({ owner, repo, ref: `tags/${tag}` });
errors.push(`Git tag '${tag}' already exists`);
} catch (e) {
if (e.status !== 404) throw e;
console.log(`✅ Git tag '${tag}' is available`);
}
try {
await github.rest.repos.getReleaseByTag({ owner, repo, tag });
errors.push(`GitHub Release '${tag}' already exists`);
} catch (e) {
if (e.status !== 404) throw e;
console.log(`✅ GitHub Release '${tag}' is available`);
}
try {
const packageName = repo.toLowerCase();
const versions = await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
package_type: 'container',
package_name: packageName,
org: owner
});
if (versions.data.some(v => v.metadata?.container?.tags?.includes(version))) {
errors.push(`Docker image tag '${version}' already exists on ghcr.io`);
} else {
console.log(`✅ Docker image tag '${version}' is available`);
}
} catch (e) {
if (e.status === 404) {
console.log(`✅ No container package found yet (first publish)`);
} else {
console.warn(`⚠️ Could not check container registry: ${e.message}`);
}
}
if (errors.length > 0) {
core.setFailed(`❌ Version guard failed:\n${errors.map(e => ` - ${e}`).join('\n')}`);
} else {
console.log('\n✅ All version checks passed');
}
# ── Update Directory.Build.props ─────────────────
- name: Read current version
id: current
run: |
CURRENT=$(grep -oP '(?<=<Version>)[^<]+' Directory.Build.props)
echo "version=${CURRENT}" >> $GITHUB_OUTPUT
echo "📋 Current version in Directory.Build.props: ${CURRENT}"
- name: Update Directory.Build.props
run: |
VERSION="${{ steps.params.outputs.version }}"
CURRENT="${{ steps.current.outputs.version }}"
if [ "${VERSION}" = "${CURRENT}" ]; then
echo "ℹ️ Version already set to ${VERSION}, no change needed"
else
sed -i "s|<Version>${CURRENT}</Version>|<Version>${VERSION}</Version>|" Directory.Build.props
echo "✅ Updated Directory.Build.props: ${CURRENT} → ${VERSION}"
fi
PE_VERSION="${{ steps.params.outputs.pe_version }}"
sed -i "s|<PayrollEngineVersion>[^<]*</PayrollEngineVersion>|<PayrollEngineVersion>${PE_VERSION}</PayrollEngineVersion>|" Directory.Build.props
echo "✅ PayrollEngineVersion → ${PE_VERSION}"
grep '<Version>\|<PayrollEngineVersion>' Directory.Build.props
- name: Commit version bump
if: steps.params.outputs.dry_run != 'true'
run: |
VERSION="${{ steps.params.outputs.version }}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add Directory.Build.props
if git diff --cached --quiet; then
echo "ℹ️ No changes to commit (version was already correct)"
else
git commit -m "release: v${VERSION}"
git push
echo "✅ Version bump committed and pushed"
fi
# ── Build & Push Docker ──────────────────────────
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Determine Docker tags
id: tags
run: |
IMAGE="${{ env.REGISTRY }}/${{ steps.params.outputs.image_tag }}"
VERSION="${{ steps.params.outputs.version }}"
IS_PRERELEASE="${{ steps.params.outputs.is_prerelease }}"
DRY_RUN="${{ steps.params.outputs.dry_run }}"
TAGS="${IMAGE}:${VERSION}"
if [ "${IS_PRERELEASE}" = "false" ] && [ "${DRY_RUN}" != "true" ]; then
TAGS="${TAGS},${IMAGE}:latest"
fi
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
build-args: |
GITHUB_TOKEN=${{ secrets.PAT_DISPATCH }}
NUGET_SOURCE=${{ steps.params.outputs.nuget_source }}
tags: ${{ steps.tags.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.params.outputs.version }}
name: v${{ steps.params.outputs.version }}
prerelease: ${{ steps.params.outputs.is_prerelease }}
draft: ${{ steps.params.outputs.dry_run == 'true' }}
generate_release_notes: true