-
Notifications
You must be signed in to change notification settings - Fork 0
170 lines (147 loc) · 5.48 KB
/
Copy pathrelease.yml
File metadata and controls
170 lines (147 loc) · 5.48 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
name: Release
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
concurrency:
group: filecontext-release
cancel-in-progress: false
env:
DOTNET_VERSION: '10.0.x'
jobs:
build:
name: Validate and pack
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
timeout-minutes: 20
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Setup .NET
uses: actions/setup-dotnet@v6
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Read package version
id: version
shell: bash
run: |
set -euo pipefail
VERSION="$(dotnet msbuild src/ManagedCode.FileContext/ManagedCode.FileContext.csproj -nologo -getProperty:PackageVersion | tr -d '\r')"
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::Invalid release version: $VERSION"
exit 1
fi
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Restore
run: dotnet restore ManagedCode.FileContext.slnx
- name: Verify formatting
run: dotnet format ManagedCode.FileContext.slnx --verify-no-changes --no-restore
- name: Build
run: dotnet build ManagedCode.FileContext.slnx --configuration Release --no-restore
- name: Test with coverage
run: dotnet test tests/ManagedCode.FileContext.Tests/ManagedCode.FileContext.Tests.csproj --configuration Release --no-build /p:CollectCoverage=true
- name: Pack
run: dotnet pack src/ManagedCode.FileContext/ManagedCode.FileContext.csproj --configuration Release --no-build --output artifacts -p:IncludeSymbols=false
- name: Upload validated package
uses: actions/upload-artifact@v7
with:
name: release-package
path: artifacts/ManagedCode.FileContext.${{ steps.version.outputs.version }}.nupkg
if-no-files-found: error
retention-days: 7
publish:
name: Publish NuGet and GitHub release
needs: build
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: write
env:
VERSION: ${{ needs.build.outputs.version }}
RELEASE_TAG: v${{ needs.build.outputs.version }}
GH_TOKEN: ${{ github.token }}
steps:
- name: Checkout validated commit
uses: actions/checkout@v7
with:
ref: ${{ github.sha }}
fetch-depth: 0
- name: Check existing release
id: release
shell: bash
run: |
set -euo pipefail
gh api --paginate --slurp "repos/$GITHUB_REPOSITORY/releases" > "$RUNNER_TEMP/releases.json"
if jq -e --arg tag "$RELEASE_TAG" 'any(.[][]; .tag_name == $tag and .draft == false)' "$RUNNER_TEMP/releases.json" > /dev/null; then
echo "exists=true" >> "$GITHUB_OUTPUT"
echo "$RELEASE_TAG is already released; skipping publication." >> "$GITHUB_STEP_SUMMARY"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Validate existing tag
if: steps.release.outputs.exists == 'false'
shell: bash
run: |
set -euo pipefail
if git rev-parse --verify --quiet "refs/tags/$RELEASE_TAG" > /dev/null; then
TAG_COMMIT="$(git rev-parse "refs/tags/$RELEASE_TAG^{commit}")"
if [ "$TAG_COMMIT" != "$GITHUB_SHA" ]; then
echo "::error::$RELEASE_TAG points to $TAG_COMMIT, not the validated commit $GITHUB_SHA."
exit 1
fi
fi
- name: Download validated package
if: steps.release.outputs.exists == 'false'
uses: actions/download-artifact@v8
with:
name: release-package
path: artifacts
- name: Setup .NET
if: steps.release.outputs.exists == 'false'
uses: actions/setup-dotnet@v6
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Publish to NuGet
if: steps.release.outputs.exists == 'false'
shell: bash
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: |
set -euo pipefail
if [ -z "$NUGET_API_KEY" ]; then
echo "::error::NUGET_API_KEY is not configured."
exit 1
fi
dotnet nuget push "artifacts/ManagedCode.FileContext.$VERSION.nupkg" \
--api-key "$NUGET_API_KEY" \
--source https://api.nuget.org/v3/index.json \
--skip-duplicate
- name: Create version tag
if: steps.release.outputs.exists == 'false'
shell: bash
run: |
set -euo pipefail
if ! git rev-parse --verify --quiet "refs/tags/$RELEASE_TAG" > /dev/null; then
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git tag -a "$RELEASE_TAG" "$GITHUB_SHA" -m "Release $VERSION"
git push origin "refs/tags/$RELEASE_TAG"
fi
- name: Create GitHub release
if: steps.release.outputs.exists == 'false'
shell: bash
run: |
set -euo pipefail
FLAGS=()
if [[ "$VERSION" == *-* ]]; then
FLAGS+=(--prerelease)
fi
gh release create "$RELEASE_TAG" "artifacts/ManagedCode.FileContext.$VERSION.nupkg" \
--title "$RELEASE_TAG" \
--generate-notes \
--verify-tag \
"${FLAGS[@]}"