|
| 1 | +name: Publish NuGet Package |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: |
| 6 | + - published |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + packages: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Setup .NET |
| 22 | + uses: actions/setup-dotnet@v4 |
| 23 | + with: |
| 24 | + dotnet-version: 10.0.x |
| 25 | + |
| 26 | + - name: Resolve package version |
| 27 | + id: version |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + if [[ "${{ github.event_name }}" == "release" ]]; then |
| 31 | + TAG_NAME="${{ github.event.release.tag_name }}" |
| 32 | + elif [[ "${{ github.ref_type }}" == "tag" ]]; then |
| 33 | + TAG_NAME="${{ github.ref_name }}" |
| 34 | + else |
| 35 | + echo "workflow_dispatch must be run from a tag ref such as v1.0.4." >&2 |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | +
|
| 39 | + PACKAGE_VERSION="${TAG_NAME#v}" |
| 40 | +
|
| 41 | + if [[ -z "${PACKAGE_VERSION}" ]]; then |
| 42 | + echo "Package version could not be resolved from the release tag." >&2 |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +
|
| 46 | + echo "tag_name=${TAG_NAME}" >> "${GITHUB_OUTPUT}" |
| 47 | + echo "package_version=${PACKAGE_VERSION}" >> "${GITHUB_OUTPUT}" |
| 48 | +
|
| 49 | + - name: Restore |
| 50 | + run: dotnet restore YCode.CLI/YCode.CLI.csproj |
| 51 | + |
| 52 | + - name: Pack |
| 53 | + run: > |
| 54 | + dotnet pack YCode.CLI/YCode.CLI.csproj |
| 55 | + --configuration Release |
| 56 | + --no-restore |
| 57 | + -p:ContinuousIntegrationBuild=true |
| 58 | + -p:PackageVersion=${{ steps.version.outputs.package_version }} |
| 59 | + --output ${{ github.workspace }}/artifacts |
| 60 | +
|
| 61 | + - name: Publish to NuGet.org |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + shopt -s nullglob |
| 65 | + packages=("${{ github.workspace }}/artifacts/"*.nupkg) |
| 66 | +
|
| 67 | + if (( ${#packages[@]} == 0 )); then |
| 68 | + echo "No .nupkg files were generated." >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + dotnet nuget push "${packages[@]}" \ |
| 73 | + --api-key "${{ secrets.NUGET_API_KEY }}" \ |
| 74 | + --source "https://api.nuget.org/v3/index.json" \ |
| 75 | + --skip-duplicate |
| 76 | +
|
| 77 | + - name: Configure GitHub Packages source |
| 78 | + run: > |
| 79 | + dotnet nuget add source |
| 80 | + --username "${{ github.repository_owner }}" |
| 81 | + --password "${{ secrets.GITHUB_TOKEN }}" |
| 82 | + --store-password-in-clear-text |
| 83 | + --name github |
| 84 | + "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" |
| 85 | +
|
| 86 | + - name: Publish to GitHub Packages |
| 87 | + shell: bash |
| 88 | + run: | |
| 89 | + shopt -s nullglob |
| 90 | + packages=("${{ github.workspace }}/artifacts/"*.nupkg) |
| 91 | +
|
| 92 | + dotnet nuget push "${packages[@]}" \ |
| 93 | + --source github \ |
| 94 | + --api-key "${{ secrets.GITHUB_TOKEN }}" \ |
| 95 | + --skip-duplicate |
| 96 | +
|
| 97 | + - name: Upload package to GitHub Release |
| 98 | + env: |
| 99 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + shell: bash |
| 101 | + run: | |
| 102 | + shopt -s nullglob |
| 103 | + packages=("${{ github.workspace }}/artifacts/"*.nupkg) |
| 104 | +
|
| 105 | + gh release upload "${{ steps.version.outputs.tag_name }}" "${packages[@]}" --clobber |
0 commit comments