-
Notifications
You must be signed in to change notification settings - Fork 0
147 lines (118 loc) · 4.86 KB
/
Copy pathbuildwithartefacts.yml
File metadata and controls
147 lines (118 loc) · 4.86 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
name: Build and Publish NuGet Package
on:
pull_request:
workflow_dispatch:
inputs:
deployRelease:
description: 'Deploy release (publish NuGet package) (y/n)'
required: true
default: 'n'
push:
branches:
- main
- prerelease
paths:
- 'Source/**/PackageVersion.txt'
env:
NuGetDirectory: ${{ github.workspace }}/nuget
jobs:
validate-versions:
if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deployRelease == 'y')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Validate version suffixes match branch
shell: pwsh
run: |
$branch = "${{ github.ref }}"
$isMain = $branch -eq "refs/heads/main"
$isPrerelease = $branch -eq "refs/heads/prerelease"
if (-not $isMain -and -not $isPrerelease) {
Write-Host "Skipping validation - not on main or prerelease branch"
exit 0
}
# Get list of changed PackageVersion.txt files in this push
$changedFiles = git diff --name-only ${{ github.event.before }} ${{ github.sha }} | Where-Object { $_ -like "*/PackageVersion.txt" }
if ($changedFiles.Count -eq 0) {
Write-Host "No PackageVersion.txt files changed in this push"
exit 0
}
$errors = @()
foreach ($file in $changedFiles) {
if (-not (Test-Path $file)) {
Write-Host "Skipping deleted file: $file"
continue
}
$version = (Get-Content $file).Trim()
$hasPrereleaseSuffix = $version -match '-'
if ($isMain -and $hasPrereleaseSuffix) {
$errors += "ERROR: $file contains prerelease version '$version' but branch is 'main'"
}
elseif ($isPrerelease -and -not $hasPrereleaseSuffix) {
$errors += "ERROR: $file contains stable version '$version' but branch is 'prerelease'"
}
else {
Write-Host "✓ $file : $version (valid for $branch)"
}
}
if ($errors.Count -gt 0) {
Write-Host "`nValidation failed:"
$errors | ForEach-Object { Write-Host $_ }
Write-Host "`nBranch/version rules:"
Write-Host " - 'main' branch must have stable versions (e.g., 1.5.0)"
Write-Host " - 'prerelease' branch must have prerelease versions (e.g., 1.5.0-beta.1)"
exit 1
}
Write-Host "`n✓ All changed versions valid for branch $branch"
build-package:
runs-on: ubuntu-latest
needs: [validate-versions]
if: always() && (needs.validate-versions.result == 'success' || needs.validate-versions.result == 'skipped')
defaults:
run:
working-directory: ./Source
shell: pwsh
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.x
- name: Restore dependencies
run: dotnet restore Mal.SourceGeneratedDI.slnx
- name: Build solution
run: dotnet build Mal.SourceGeneratedDI.slnx --configuration Release --no-restore
- name: Pack generator package
run: dotnet pack Mal.SourceGeneratedDI/Mal.SourceGeneratedDI.csproj --configuration Release --no-build --output ${{ env.NuGetDirectory }}
- name: Pack abstractions package
run: dotnet pack Mal.SourceGeneratedDI.Abstractions/Mal.SourceGeneratedDI.Abstractions.csproj --configuration Release --no-build --output ${{ env.NuGetDirectory }}
- name: Upload NuGet package artifact
uses: actions/upload-artifact@v4
with:
name: nuget
if-no-files-found: error
retention-days: 7
path: ${{ env.NuGetDirectory }}/*.nupkg
deploy:
if: (github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/prerelease')) || (github.event_name == 'workflow_dispatch' && github.event.inputs.deployRelease == 'y')
runs-on: ubuntu-latest
environment: ${{ github.ref == 'refs/heads/main' && 'Deploy' || 'Prerelease' }}
needs: build-package
defaults:
run:
shell: pwsh
steps:
- name: Download NuGet artifact
uses: actions/download-artifact@v4
with:
name: nuget
path: ./nuget
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.x
- name: Publish NuGet packages
run: |
foreach($file in (Get-ChildItem "./nuget" -Recurse -Include *.nupkg)) {
dotnet nuget push $file --api-key "${{ secrets.NUGET_KEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate
}