forked from lqdev/markdown-ld-kb
-
Notifications
You must be signed in to change notification settings - Fork 0
228 lines (189 loc) · 6.24 KB
/
release.yml
File metadata and controls
228 lines (189 loc) · 6.24 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
name: Release
on:
push:
branches: [ main ]
workflow_dispatch:
env:
DOTNET_VERSION: '10.0.x'
SOLUTION: MarkdownLd.Kb.slnx
ARTIFACTS_DIR: ./artifacts
jobs:
build:
name: Build, test, and pack
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v6
with:
submodules: true
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Extract version from Directory.Build.props
id: version
shell: bash
run: |
set -euo pipefail
VERSION="$(python3 - <<'PY'
import xml.etree.ElementTree as ET
root = ET.parse("Directory.Build.props").getroot()
def read(name: str) -> str:
for element in root.iter():
if element.tag.endswith(name) and element.text and element.text.strip():
return element.text.strip()
return ""
print(read("PackageVersion") or read("Version"))
PY
)"
if [ -z "${VERSION}" ]; then
echo "::error::Could not read <PackageVersion>/<Version> from Directory.Build.props"
exit 1
fi
echo "version=${VERSION}" >> "${GITHUB_OUTPUT}"
echo "Version from Directory.Build.props: ${VERSION}"
- name: Restore dependencies
run: dotnet restore ${{ env.SOLUTION }}
- name: Verify formatting
run: dotnet format ${{ env.SOLUTION }} --verify-no-changes --no-restore
- name: Build
run: dotnet build ${{ env.SOLUTION }} --configuration Release --no-restore
- name: Test
run: dotnet test --solution ${{ env.SOLUTION }} --configuration Release --verbosity normal
- name: Pack NuGet packages
run: dotnet pack ${{ env.SOLUTION }} --configuration Release -p:IncludeSymbols=false -p:SymbolPackageFormat=snupkg --output ${{ env.ARTIFACTS_DIR }}
- name: Upload artifacts
uses: actions/upload-artifact@v7
with:
name: nuget-packages
path: ${{ env.ARTIFACTS_DIR }}/*.nupkg
retention-days: 5
publish-nuget:
name: Publish to NuGet
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
permissions:
contents: read
outputs:
published: ${{ steps.publish.outputs.published }}
version: ${{ needs.build.outputs.version }}
steps:
- name: Download artifacts
uses: actions/download-artifact@v8
with:
name: nuget-packages
path: ${{ env.ARTIFACTS_DIR }}
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Publish to NuGet
id: publish
shell: bash
run: |
set -euo pipefail
PUBLISHED=false
shopt -s nullglob
for package in ${{ env.ARTIFACTS_DIR }}/*.nupkg; do
echo "Publishing ${package}..."
set +e
RESULT="$(dotnet nuget push "${package}" \
--api-key "${{ secrets.NUGET_API_KEY }}" \
--source https://api.nuget.org/v3/index.json \
--skip-duplicate 2>&1)"
EXIT_CODE=$?
set -e
echo "${RESULT}"
if [ ${EXIT_CODE} -eq 0 ]; then
PUBLISHED=true
elif echo "${RESULT}" | grep -qi "already exists"; then
echo "Package already exists, skipping ${package}."
else
echo "::error::Failed to publish ${package}"
exit ${EXIT_CODE}
fi
done
echo "published=${PUBLISHED}" >> "${GITHUB_OUTPUT}"
create-release:
name: Create GitHub release and tag
needs: publish-nuget
runs-on: ubuntu-latest
if: needs.publish-nuget.outputs.published == 'true'
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Download artifacts
uses: actions/download-artifact@v8
with:
name: nuget-packages
path: ${{ env.ARTIFACTS_DIR }}
- name: Create and push tag
shell: bash
run: |
set -euo pipefail
VERSION="${{ needs.publish-nuget.outputs.version }}"
TAG="v${VERSION}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if git rev-parse "${TAG}" >/dev/null 2>&1; then
echo "Tag ${TAG} already exists."
else
git tag -a "${TAG}" -m "Release ${VERSION}"
git push origin "${TAG}"
fi
- name: Generate release notes
shell: bash
run: |
set -euo pipefail
VERSION="${{ needs.publish-nuget.outputs.version }}"
TAG="v${VERSION}"
PREVIOUS_TAG="$(git tag --sort=-version:refname | grep -v -x -- "${TAG}" | head -n1 || true)"
{
echo "# Release ${VERSION}"
echo
echo "Released on $(date +'%Y-%m-%d')"
echo
if [ -n "${PREVIOUS_TAG}" ]; then
echo "## Changes since ${PREVIOUS_TAG}"
echo
git log --pretty=format:"- %s (%h)" "${PREVIOUS_TAG}..HEAD" || true
else
echo "## Initial release"
echo
git log --pretty=format:"- %s (%h)" --max-count=20 || true
fi
echo
echo "## NuGet packages"
echo
shopt -s nullglob
for package in ${{ env.ARTIFACTS_DIR }}/*.nupkg; do
file="$(basename "${package}")"
base="${file%.nupkg}"
version_suffix=".${VERSION}"
package_id="${base%"${version_suffix}"}"
echo "- [${package_id} ${VERSION}](https://www.nuget.org/packages/${package_id}/${VERSION})"
done
} > release_notes.md
- name: Create GitHub Release
shell: bash
run: |
set -euo pipefail
VERSION="${{ needs.publish-nuget.outputs.version }}"
TAG="v${VERSION}"
gh release create "${TAG}" \
--title "${TAG}" \
--notes-file release_notes.md \
${{ env.ARTIFACTS_DIR }}/*.nupkg
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}