|
| 1 | +name: CD |
| 2 | + |
| 3 | +# Builds, tests and packs the NuGet package on a version tag (v1.2.3) or manual run. |
| 4 | +# Publishing to NuGet uses Trusted Publishing (OIDC) and runs only on a v*.*.* tag push; a manual |
| 5 | +# run packs and uploads the artifact as a dry run without publishing (see the step comments below). |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + tags: [ 'v*.*.*' ] |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + version: |
| 13 | + description: 'Package version, e.g. 1.2.3. Defaults to a dev version when omitted.' |
| 14 | + required: false |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | +jobs: |
| 20 | + pack-publish: |
| 21 | + name: Pack & Publish |
| 22 | + # Windows is required: the library multi-targets net40/net45 and the demo targets net48, which |
| 23 | + # need the .NET Framework reference assemblies / targeting packs available on the Windows runners. |
| 24 | + runs-on: windows-latest |
| 25 | + timeout-minutes: 15 |
| 26 | + permissions: |
| 27 | + id-token: write # Required for NuGet Trusted Publishing; without it NuGet/login fails with 403. |
| 28 | + contents: write # Required to create the GitHub Release; a job-level block drops inherited defaults. |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v6 |
| 31 | + |
| 32 | + - name: Setup .NET |
| 33 | + uses: actions/setup-dotnet@v5 |
| 34 | + with: |
| 35 | + # Install the exact SDK pinned in global.json; Dependabot's dotnet-sdk updater keeps it current. |
| 36 | + global-json-file: global.json |
| 37 | + # Cache the restored NuGet packages, keyed on the committed lock files. |
| 38 | + cache: true |
| 39 | + cache-dependency-path: '**/packages.lock.json' |
| 40 | + |
| 41 | + - name: Resolve version |
| 42 | + id: version |
| 43 | + shell: bash |
| 44 | + # The manual input is read through an env var, not interpolated into the script, so a crafted |
| 45 | + # value cannot inject shell. It is then validated as SemVer before anything downstream uses it. |
| 46 | + env: |
| 47 | + VERSION_INPUT: ${{ github.event.inputs.version }} |
| 48 | + run: | |
| 49 | + if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then |
| 50 | + VERSION="${GITHUB_REF_NAME#v}" |
| 51 | + elif [[ -n "${VERSION_INPUT}" ]]; then |
| 52 | + VERSION="${VERSION_INPUT}" |
| 53 | + if ! [[ "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.][0-9A-Za-z.-]+)?$ ]]; then |
| 54 | + echo "Invalid version '${VERSION}'; expected SemVer such as 1.2.3 or 1.2.3-rc.1." >&2 |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + else |
| 58 | + VERSION="0.0.0-dev.${GITHUB_RUN_NUMBER}" |
| 59 | + fi |
| 60 | + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" |
| 61 | + echo "Resolved package version: ${VERSION}" |
| 62 | +
|
| 63 | + - name: Restore |
| 64 | + # Locked-mode restore fails the build if any packages.lock.json is missing or out of date, so the |
| 65 | + # restore is reproducible and a forgotten lock-file update cannot slip through. |
| 66 | + run: dotnet restore SQLite.CodeFirst.slnx --locked-mode |
| 67 | + |
| 68 | + - name: Build |
| 69 | + run: dotnet build SQLite.CodeFirst.slnx -c Release --no-restore -p:Version=${{ steps.version.outputs.version }} |
| 70 | + |
| 71 | + - name: Test |
| 72 | + # The test project runs on Microsoft.Testing.Platform (opted in via global.json), so target the |
| 73 | + # solution with the native MTP dotnet test syntax. |
| 74 | + run: dotnet test --solution SQLite.CodeFirst.slnx -c Release --no-build |
| 75 | + |
| 76 | + - name: Pack |
| 77 | + run: > |
| 78 | + dotnet pack SQLite.CodeFirst/SQLite.CodeFirst.csproj -c Release --no-build |
| 79 | + -p:Version=${{ steps.version.outputs.version }} |
| 80 | + -o ${{ github.workspace }}/artifacts |
| 81 | +
|
| 82 | + - name: Upload package artifact |
| 83 | + uses: actions/upload-artifact@v7 |
| 84 | + with: |
| 85 | + name: nuget-package |
| 86 | + path: | |
| 87 | + ${{ github.workspace }}/artifacts/*.nupkg |
| 88 | + ${{ github.workspace }}/artifacts/*.snupkg |
| 89 | +
|
| 90 | + # --- NuGet publishing via Trusted Publishing (OIDC). --- |
| 91 | + # No long-lived API key: NuGet/login swaps the GitHub OIDC token for a short-lived key, authorized |
| 92 | + # by the trusted publishing policy on nuget.org (the policy must name this workflow file, cd.yml). |
| 93 | + # Gated to tag pushes so a manual run stays a dry run and never pushes the 0.0.0-dev.* version. |
| 94 | + # https://learn.microsoft.com/nuget/nuget-org/trusted-publishing |
| 95 | + - name: NuGet login (OIDC) |
| 96 | + id: nuget-login |
| 97 | + if: startsWith(github.ref, 'refs/tags/v') |
| 98 | + uses: NuGet/login@v1 |
| 99 | + with: |
| 100 | + user: ${{ secrets.NUGET_USER }} # nuget.org account/profile name, NOT the email address. |
| 101 | + |
| 102 | + - name: Publish to NuGet |
| 103 | + if: startsWith(github.ref, 'refs/tags/v') |
| 104 | + run: > |
| 105 | + dotnet nuget push "${{ github.workspace }}/artifacts/*.nupkg" |
| 106 | + --api-key "${{ steps.nuget-login.outputs.NUGET_API_KEY }}" |
| 107 | + --source https://api.nuget.org/v3/index.json |
| 108 | + --skip-duplicate |
| 109 | +
|
| 110 | + # --- GitHub Release. --- |
| 111 | + # Created only on tag pushes, after a successful NuGet publish, so the Release reflects what was |
| 112 | + # actually shipped. Uses the gh CLI (preinstalled on the runner) rather than a third-party action. |
| 113 | + # --generate-notes builds the notes from commits/PRs since the previous tag; --verify-tag guards |
| 114 | + # against creating a release for a tag the runner cannot see. The .nupkg/.snupkg are attached so |
| 115 | + # the package is downloadable straight from the Release page, not just the workflow run. |
| 116 | + - name: Create GitHub Release |
| 117 | + if: startsWith(github.ref, 'refs/tags/v') |
| 118 | + shell: bash |
| 119 | + # The tag is passed through an env var rather than interpolated into the script, matching the |
| 120 | + # version step above. |
| 121 | + env: |
| 122 | + GH_TOKEN: ${{ github.token }} |
| 123 | + TAG: ${{ github.ref_name }} |
| 124 | + run: | |
| 125 | + # SemVer pre-release tags carry a hyphen (e.g. v1.2.3-rc.1); mark those as GitHub pre-releases. |
| 126 | + prerelease="" |
| 127 | + if [[ "${TAG}" == *-* ]]; then prerelease="--prerelease"; fi |
| 128 | + gh release create "${TAG}" \ |
| 129 | + --title "${TAG}" \ |
| 130 | + --generate-notes \ |
| 131 | + --verify-tag \ |
| 132 | + ${prerelease} \ |
| 133 | + "${GITHUB_WORKSPACE}/artifacts/"*.nupkg \ |
| 134 | + "${GITHUB_WORKSPACE}/artifacts/"*.snupkg |
0 commit comments