Skip to content

Commit bf389c4

Browse files
authored
Switch publishing to nuget.org from api-key to short lived token (#260)
* Switch publishing to nuget.org from api-key to short lived token based on NuGet Trusted Publishing source.
1 parent 5316a24 commit bf389c4

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

.github/workflows/dotnet-push-package.yml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ env:
1919
jobs:
2020
build:
2121
runs-on: ubuntu-latest
22+
permissions:
23+
contents: read
24+
packages: write
25+
id-token: write
2226
steps:
2327
# Checkout the code
2428
- uses: actions/checkout@v5
@@ -69,17 +73,29 @@ jobs:
6973
if: steps.core_changes.outputs.should_publish == 'false'
7074
run: echo "Core library package inputs are unchanged since the previous release tag. No new NuGet package will be built or published."
7175

72-
# Build, pack and push NuGet package
73-
- name: Build, pack and push NuGet package
76+
# Build and pack NuGet package
77+
- name: Build and pack NuGet package
7478
if: steps.core_changes.outputs.should_publish == 'true'
75-
env:
76-
GITHUB_PACKAGE_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77-
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
7879
run: |
7980
VERSION="${GITHUB_REF_NAME#v}"
8081
8182
dotnet build "${PROJECT_FILE}" -c "${CONFIGURATION}" /Property:Version="$VERSION"
8283
dotnet pack "${PROJECT_FILE}" --no-build -c "${CONFIGURATION}" -p:PackageVersion="$VERSION" -o out
84+
85+
# Exchange the GitHub Actions OIDC token for a short-lived, single-use NuGet API key.
86+
- name: NuGet login (OIDC to temporary API key)
87+
if: steps.core_changes.outputs.should_publish == 'true'
88+
uses: NuGet/login@8d196754b4036150537f80ac539e15c2f1028841 # v1
89+
id: nuget_login
90+
with:
91+
user: ${{ secrets.NUGET_USER }}
92+
93+
- name: Push NuGet package
94+
if: steps.core_changes.outputs.should_publish == 'true'
95+
env:
96+
GITHUB_PACKAGE_TOKEN: ${{ secrets.GITHUB_TOKEN }}
97+
NUGET_API_KEY: ${{ steps.nuget_login.outputs.NUGET_API_KEY }}
98+
run: |
8399
dotnet nuget push "./out/*.nupkg" --source "${PACKAGE_REPO}" --api-key "${GITHUB_PACKAGE_TOKEN}" --skip-duplicate
84100
dotnet nuget push "./out/*.nupkg" --source https://api.nuget.org/v3/index.json --api-key "${NUGET_API_KEY}" --skip-duplicate
85101

0 commit comments

Comments
 (0)