|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +env: |
| 9 | + DOTNET_VERSION: '10.0.x' |
| 10 | + SOLUTION: MarkdownLd.Kb.slnx |
| 11 | + ARTIFACTS_DIR: ./artifacts |
| 12 | + |
| 13 | +jobs: |
| 14 | + build: |
| 15 | + name: Build, test, and pack |
| 16 | + runs-on: ubuntu-latest |
| 17 | + timeout-minutes: 20 |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + |
| 21 | + outputs: |
| 22 | + version: ${{ steps.version.outputs.version }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v5 |
| 27 | + with: |
| 28 | + submodules: true |
| 29 | + |
| 30 | + - name: Setup .NET |
| 31 | + uses: actions/setup-dotnet@v4 |
| 32 | + with: |
| 33 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 34 | + |
| 35 | + - name: Extract version from Directory.Build.props |
| 36 | + id: version |
| 37 | + shell: bash |
| 38 | + run: | |
| 39 | + set -euo pipefail |
| 40 | + VERSION="$(python3 - <<'PY' |
| 41 | + import xml.etree.ElementTree as ET |
| 42 | +
|
| 43 | + root = ET.parse("Directory.Build.props").getroot() |
| 44 | +
|
| 45 | + def read(name: str) -> str: |
| 46 | + for element in root.iter(): |
| 47 | + if element.tag.endswith(name) and element.text and element.text.strip(): |
| 48 | + return element.text.strip() |
| 49 | + return "" |
| 50 | +
|
| 51 | + print(read("PackageVersion") or read("Version")) |
| 52 | + PY |
| 53 | + )" |
| 54 | +
|
| 55 | + if [ -z "${VERSION}" ]; then |
| 56 | + echo "::error::Could not read <PackageVersion>/<Version> from Directory.Build.props" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +
|
| 60 | + echo "version=${VERSION}" >> "${GITHUB_OUTPUT}" |
| 61 | + echo "Version from Directory.Build.props: ${VERSION}" |
| 62 | +
|
| 63 | + - name: Restore dependencies |
| 64 | + run: dotnet restore ${{ env.SOLUTION }} |
| 65 | + |
| 66 | + - name: Verify formatting |
| 67 | + run: dotnet format ${{ env.SOLUTION }} --verify-no-changes --no-restore |
| 68 | + |
| 69 | + - name: Build |
| 70 | + run: dotnet build ${{ env.SOLUTION }} --configuration Release --no-restore |
| 71 | + |
| 72 | + - name: Test |
| 73 | + run: dotnet test --solution ${{ env.SOLUTION }} --configuration Release --no-build --verbosity normal |
| 74 | + |
| 75 | + - name: Pack NuGet packages |
| 76 | + run: dotnet pack ${{ env.SOLUTION }} --configuration Release --no-build -p:IncludeSymbols=false -p:SymbolPackageFormat=snupkg --output ${{ env.ARTIFACTS_DIR }} |
| 77 | + |
| 78 | + - name: Upload artifacts |
| 79 | + uses: actions/upload-artifact@v4 |
| 80 | + with: |
| 81 | + name: nuget-packages |
| 82 | + path: ${{ env.ARTIFACTS_DIR }}/*.nupkg |
| 83 | + retention-days: 5 |
| 84 | + |
| 85 | + publish-nuget: |
| 86 | + name: Publish to NuGet |
| 87 | + needs: build |
| 88 | + runs-on: ubuntu-latest |
| 89 | + if: github.ref == 'refs/heads/main' |
| 90 | + permissions: |
| 91 | + contents: read |
| 92 | + |
| 93 | + outputs: |
| 94 | + published: ${{ steps.publish.outputs.published }} |
| 95 | + version: ${{ needs.build.outputs.version }} |
| 96 | + |
| 97 | + steps: |
| 98 | + - name: Download artifacts |
| 99 | + uses: actions/download-artifact@v5 |
| 100 | + with: |
| 101 | + name: nuget-packages |
| 102 | + path: ${{ env.ARTIFACTS_DIR }} |
| 103 | + |
| 104 | + - name: Setup .NET |
| 105 | + uses: actions/setup-dotnet@v4 |
| 106 | + with: |
| 107 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 108 | + |
| 109 | + - name: Publish to NuGet |
| 110 | + id: publish |
| 111 | + shell: bash |
| 112 | + run: | |
| 113 | + set -euo pipefail |
| 114 | +
|
| 115 | + PUBLISHED=false |
| 116 | + shopt -s nullglob |
| 117 | +
|
| 118 | + for package in ${{ env.ARTIFACTS_DIR }}/*.nupkg; do |
| 119 | + echo "Publishing ${package}..." |
| 120 | + set +e |
| 121 | + RESULT="$(dotnet nuget push "${package}" \ |
| 122 | + --api-key "${{ secrets.NUGET_API_KEY }}" \ |
| 123 | + --source https://api.nuget.org/v3/index.json \ |
| 124 | + --skip-duplicate 2>&1)" |
| 125 | + EXIT_CODE=$? |
| 126 | + set -e |
| 127 | + echo "${RESULT}" |
| 128 | +
|
| 129 | + if [ ${EXIT_CODE} -eq 0 ]; then |
| 130 | + PUBLISHED=true |
| 131 | + elif echo "${RESULT}" | grep -qi "already exists"; then |
| 132 | + echo "Package already exists, skipping ${package}." |
| 133 | + else |
| 134 | + echo "::error::Failed to publish ${package}" |
| 135 | + exit ${EXIT_CODE} |
| 136 | + fi |
| 137 | + done |
| 138 | +
|
| 139 | + echo "published=${PUBLISHED}" >> "${GITHUB_OUTPUT}" |
| 140 | +
|
| 141 | + create-release: |
| 142 | + name: Create GitHub release and tag |
| 143 | + needs: publish-nuget |
| 144 | + runs-on: ubuntu-latest |
| 145 | + if: needs.publish-nuget.outputs.published == 'true' |
| 146 | + permissions: |
| 147 | + contents: write |
| 148 | + |
| 149 | + steps: |
| 150 | + - name: Checkout |
| 151 | + uses: actions/checkout@v5 |
| 152 | + with: |
| 153 | + fetch-depth: 0 |
| 154 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 155 | + |
| 156 | + - name: Download artifacts |
| 157 | + uses: actions/download-artifact@v5 |
| 158 | + with: |
| 159 | + name: nuget-packages |
| 160 | + path: ${{ env.ARTIFACTS_DIR }} |
| 161 | + |
| 162 | + - name: Create and push tag |
| 163 | + shell: bash |
| 164 | + run: | |
| 165 | + set -euo pipefail |
| 166 | + VERSION="${{ needs.publish-nuget.outputs.version }}" |
| 167 | + TAG="v${VERSION}" |
| 168 | +
|
| 169 | + git config user.name "github-actions[bot]" |
| 170 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 171 | +
|
| 172 | + if git rev-parse "${TAG}" >/dev/null 2>&1; then |
| 173 | + echo "Tag ${TAG} already exists." |
| 174 | + else |
| 175 | + git tag -a "${TAG}" -m "Release ${VERSION}" |
| 176 | + git push origin "${TAG}" |
| 177 | + fi |
| 178 | +
|
| 179 | + - name: Generate release notes |
| 180 | + shell: bash |
| 181 | + run: | |
| 182 | + set -euo pipefail |
| 183 | + VERSION="${{ needs.publish-nuget.outputs.version }}" |
| 184 | + TAG="v${VERSION}" |
| 185 | + PREVIOUS_TAG="$(git tag --sort=-version:refname | grep -v "^${TAG}$" | head -n1 || true)" |
| 186 | +
|
| 187 | + { |
| 188 | + echo "# Release ${VERSION}" |
| 189 | + echo |
| 190 | + echo "Released on $(date +'%Y-%m-%d')" |
| 191 | + echo |
| 192 | +
|
| 193 | + if [ -n "${PREVIOUS_TAG}" ]; then |
| 194 | + echo "## Changes since ${PREVIOUS_TAG}" |
| 195 | + echo |
| 196 | + git log --pretty=format:"- %s (%h)" "${PREVIOUS_TAG}..HEAD" || true |
| 197 | + else |
| 198 | + echo "## Initial release" |
| 199 | + echo |
| 200 | + git log --pretty=format:"- %s (%h)" --max-count=20 || true |
| 201 | + fi |
| 202 | +
|
| 203 | + echo |
| 204 | + echo "## NuGet packages" |
| 205 | + echo |
| 206 | + shopt -s nullglob |
| 207 | + for package in ${{ env.ARTIFACTS_DIR }}/*.nupkg; do |
| 208 | + file="$(basename "${package}")" |
| 209 | + base="${file%.nupkg}" |
| 210 | + package_id="${base%.${VERSION}}" |
| 211 | + echo "- [${package_id} ${VERSION}](https://www.nuget.org/packages/${package_id}/${VERSION})" |
| 212 | + done |
| 213 | + } > release_notes.md |
| 214 | +
|
| 215 | + - name: Create GitHub Release |
| 216 | + uses: softprops/action-gh-release@v2 |
| 217 | + with: |
| 218 | + tag_name: v${{ needs.publish-nuget.outputs.version }} |
| 219 | + name: v${{ needs.publish-nuget.outputs.version }} |
| 220 | + body_path: release_notes.md |
| 221 | + files: ${{ env.ARTIFACTS_DIR }}/*.nupkg |
| 222 | + draft: false |
| 223 | + prerelease: false |
| 224 | + env: |
| 225 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments