-
Notifications
You must be signed in to change notification settings - Fork 1
377 lines (320 loc) · 13.5 KB
/
Copy pathci-cd.yml
File metadata and controls
377 lines (320 loc) · 13.5 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
name: CI/CD Pipeline
on:
push:
branches: [ main, develop, 'release/*' ]
tags: [ 'v*' ]
pull_request:
branches: [ main, develop ]
workflow_dispatch:
inputs:
release_type:
description: 'Release type (patch / minor / major)'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
package_family:
description: 'Package family to bump'
required: true
default: 'core'
type: choice
options:
- core # PowerCSharpVersion — Core, Extensions, Helpers, Utilities, Compatibility, AspNetCore
- features # PowerCSharpFeaturesVersion — Features.Abstractions, Features, BuiltInFeatures
- cache # PowerCSharpFeatureCacheVersion — Feature.Cache.*, Feature.Cache.BitFaster, Feature.Cache.Disk
- sanitization # PowerCSharpFeatureSanitizationVersion — Feature.Sanitization.*, Feature.Sanitization
permissions:
contents: write
pull-requests: write
env:
DOTNET_VERSION: '8.0.x'
jobs:
build-and-test:
name: Build and Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4.2.2
- name: Setup .NET
uses: actions/setup-dotnet@v5.3.0
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Restore dependencies
run: dotnet restore PowerCSharp.sln
- name: Build
run: dotnet build PowerCSharp.sln --no-restore --configuration Release
- name: Test
run: dotnet test PowerCSharp.sln --no-build --configuration Release --verbosity normal --collect:"XPlat Code Coverage"
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v6.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: '**/coverage.cobertura.xml'
fail_ci_if_error: false
pack:
name: Pack NuGet Packages
runs-on: ubuntu-latest
needs: [build-and-test, update-version]
if: |
always() &&
needs.build-and-test.result == 'success' &&
(needs.update-version.result == 'success' || needs.update-version.result == 'skipped') &&
(
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' && startsWith(github.event.pull_request.head.ref, 'release/')) ||
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
)
steps:
- name: Checkout
uses: actions/checkout@v4.2.2
with:
ref: ${{ github.event_name == 'workflow_dispatch' && github.ref_name || github.sha }}
- name: Setup .NET
uses: actions/setup-dotnet@v5.3.0
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Restore dependencies
run: dotnet restore PowerCSharp.sln
- name: Build
run: dotnet build PowerCSharp.sln --no-restore --configuration Release
- name: Pack
run: dotnet pack PowerCSharp.sln --no-build --configuration Release --output ./packages
- name: Upload packages as artifacts
uses: actions/upload-artifact@v7.0.1
with:
name: nuget-packages
path: ./packages/*.nupkg
publish:
name: Publish to NuGet
runs-on: ubuntu-latest
needs: pack
if: |
always() &&
needs.pack.result == 'success' &&
(
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' && startsWith(github.event.pull_request.head.ref, 'release/')) ||
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
)
steps:
- name: Download packages
uses: actions/download-artifact@v8.0.1
with:
name: nuget-packages
path: ./packages
- name: Setup .NET
uses: actions/setup-dotnet@v5.3.0
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Publish to NuGet
run: dotnet nuget push ./packages/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
- name: Publish to GitHub Packages
run: dotnet nuget push ./packages/*.nupkg --api-key ${{ secrets.PACKAGES_PAT }} --source https://nuget.pkg.github.com/marioarce/index.json --skip-duplicate
env:
PACKAGES_PAT: ${{ secrets.PACKAGES_PAT }}
delete-feature-branch:
name: Delete Feature Branch
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
steps:
- name: Delete feature branch
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd
with:
script: |
console.log('=== Feature Branch Deletion Debug ===');
console.log('Event name:', context.eventName);
console.log('Event action:', context.payload.action);
console.log('Ref:', context.ref);
let branchName;
if (context.eventName === 'pull_request') {
console.log('Processing pull_request event');
console.log('PR base ref:', context.payload.pull_request.base.ref);
console.log('PR head ref:', context.payload.pull_request.head.ref);
console.log('PR merged:', context.payload.pull_request.merged);
if (context.payload.pull_request.base.ref === 'develop' && context.payload.pull_request.merged === true) {
branchName = context.payload.pull_request.head.ref;
console.log('Found branch name from PR:', branchName);
}
} else if (context.eventName === 'push') {
console.log('Processing push event');
console.log('Push ref:', context.ref);
console.log('Commit message:', context.payload.head_commit.message);
if (context.ref === 'refs/heads/develop') {
const commitMessage = context.payload.head_commit.message;
const commitTitle = commitMessage.split('\n')[0]; // Only use first line
console.log('Looking for merge in commit message');
console.log('Commit title:', JSON.stringify(commitTitle));
// Try multiple patterns for merge commit messages
const patterns = [
/Merge pull request #\d+ from .+?\/(.+)$/,
/Merge pull request #\d+ from (.+)$/,
/Merge branch '(.+?)' into develop/,
/Merge (.+?) into develop/,
/from (.+?)$/
];
console.log('Testing patterns against:', JSON.stringify(commitTitle));
for (let i = 0; i < patterns.length; i++) {
const pattern = patterns[i];
console.log(`Testing pattern ${i + 1}:`, pattern);
const match = commitTitle.match(pattern);
console.log(`Pattern ${i + 1} match result:`, match);
if (match && match[1]) {
branchName = match[1];
console.log('Found branch name with pattern', pattern, ':', branchName);
break;
}
}
}
}
console.log('Final branch name to delete:', branchName);
if (!branchName) {
console.log('Could not determine branch name to delete - skipping');
return;
}
// Only delete branches that start with "feature/"
if (!branchName.startsWith('feature/')) {
console.log(`Branch ${branchName} does not start with "feature/" - skipping deletion`);
return;
}
console.log(`Deleting feature branch: ${branchName}`);
try {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${branchName}`
});
console.log(`Successfully deleted feature branch: ${branchName}`);
} catch (error) {
console.error(`Failed to delete feature branch ${branchName}:`, error.message);
}
update-version:
name: Update Package Version
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
steps:
- name: Checkout
uses: actions/checkout@v4.2.2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup .NET
uses: actions/setup-dotnet@v5.3.0
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Update version
run: |
FAMILY="${{ github.event.inputs.package_family }}"
RELEASE_TYPE="${{ github.event.inputs.release_type }}"
echo "Bumping $FAMILY packages ($RELEASE_TYPE)"
# Select the correct XML element and tag prefix based on family
case "$FAMILY" in
"features")
VERSION_ELEMENT="PowerCSharpFeaturesVersion"
TAG_PREFIX="features-v"
;;
"cache")
VERSION_ELEMENT="PowerCSharpFeatureCacheVersion"
TAG_PREFIX="cache-v"
;;
"sanitization")
VERSION_ELEMENT="PowerCSharpFeatureSanitizationVersion"
TAG_PREFIX="sanitization-v"
;;
*)
VERSION_ELEMENT="PowerCSharpVersion"
TAG_PREFIX="v"
;;
esac
# Read current version
CURRENT_VERSION=$(grep "<$VERSION_ELEMENT>" Directory.Build.props | sed "s/.*<$VERSION_ELEMENT>\(.*\)<\/$VERSION_ELEMENT>.*/\1/")
echo "Current $VERSION_ELEMENT: $CURRENT_VERSION"
# Split and bump
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
case "$RELEASE_TYPE" in
"major")
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
;;
"minor")
MINOR=$((MINOR + 1))
PATCH=0
;;
"patch")
PATCH=$((PATCH + 1))
;;
esac
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "New $VERSION_ELEMENT: $NEW_VERSION"
# Update Directory.Build.props
sed -i "s/<$VERSION_ELEMENT>.*<\/$VERSION_ELEMENT>/<$VERSION_ELEMENT>$NEW_VERSION<\/$VERSION_ELEMENT>/" Directory.Build.props
# Commit and push
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add Directory.Build.props
git commit -m "chore: bump $VERSION_ELEMENT to $NEW_VERSION"
git push
# Export for the tag step
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
echo "TAG_PREFIX=$TAG_PREFIX" >> $GITHUB_ENV
- name: Create release tag
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd
with:
script: |
const tagPrefix = process.env.TAG_PREFIX;
const version = process.env.NEW_VERSION;
const tag = `${tagPrefix}${version}`;
console.log(`Creating tag: ${tag}`);
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${tag}`,
sha: context.sha
});
delete-release-branch:
name: Delete Release Branch
runs-on: ubuntu-latest
if: (github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' && startsWith(github.event.pull_request.head.ref, 'release/')) || (github.event_name == 'push' && github.ref == 'refs/heads/main' && startsWith(github.event.head_commit.message, 'Merge pull request'))
steps:
- name: Delete release branch
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd
with:
script: |
let branchName;
if (context.eventName === 'pull_request') {
branchName = context.payload.pull_request.head.ref;
} else if (context.eventName === 'push') {
// Extract branch name from merge commit message
const commitMessage = context.payload.head_commit.message;
const match = commitMessage.match(/Merge pull request #\d+ from (.+)$/);
if (match && match[1]) {
branchName = match[1];
}
}
if (!branchName) {
console.log('Could not determine branch name to delete');
return;
}
// Only delete if it's a release branch
if (!branchName.startsWith('release/')) {
console.log(`Branch ${branchName} is not a release branch, skipping deletion`);
return;
}
console.log(`Deleting release branch: ${branchName}`);
try {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${branchName}`
});
console.log(`Successfully deleted release branch: ${branchName}`);
} catch (error) {
console.error(`Failed to delete release branch ${branchName}:`, error.message);
}