Skip to content

Commit c7cc68d

Browse files
authored
Make NuGet publish secure
1 parent 2632bb3 commit c7cc68d

3 files changed

Lines changed: 39 additions & 95 deletions

File tree

.github/workflows/continuous.yml

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,58 @@ on:
44
push:
55
branches:
66
- main
7+
tags:
8+
- 'v*'
79
pull_request:
810
branches:
911
- main
12+
- 'release/*'
1013

1114
jobs:
12-
windows-latest:
13-
name: windows-latest
15+
build:
1416
runs-on: windows-latest
17+
1518
steps:
16-
- uses: actions/checkout@v4
19+
- uses: actions/checkout@v6
1720
with:
1821
fetch-depth: 0
1922

20-
- uses: actions/setup-dotnet@v4
23+
- uses: actions/setup-dotnet@v5
2124
with:
2225
dotnet-version: |
2326
6.0.x
2427
25-
2628
- name: Run './build.cmd Verify Cover Pack'
2729
run: ./build.cmd Verify Cover Pack --no-logo
30+
31+
- name: Collect NuGet for publish
32+
uses: actions/upload-artifact@v7
33+
if: startsWith(github.ref, 'refs/tags/v')
34+
with:
35+
path: 'artifacts'
36+
name: 'nuget-packages'
37+
38+
publish:
39+
needs: build
40+
if: startsWith(github.ref, 'refs/tags/v')
41+
permissions:
42+
id-token: write
43+
environment: nuget-push
44+
runs-on: ubuntu-latest
45+
46+
steps:
47+
- name: Download artifact
48+
uses: actions/download-artifact@v8
49+
with:
50+
name: nuget-packages
51+
52+
- name: NuGet login (OIDC → temp API key)
53+
uses: NuGet/login@v1
54+
id: nugetLogin
55+
with:
56+
user: ${{ secrets.NUGET_USER }}
57+
58+
- name: Push to NuGet
59+
run: dotnet nuget push **/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json
60+
env:
61+
NUGET_API_KEY: ${{ steps.nugetLogin.outputs.NUGET_API_KEY }}

.github/workflows/release.yml

Lines changed: 0 additions & 52 deletions
This file was deleted.

build/Build.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@
1818

1919
[ShutdownDotNetAfterServerBuild]
2020
[DotNetVerbosityMapping]
21-
[GitHubActions(
22-
"continuous",
23-
GitHubActionsImage.WindowsLatest,
24-
AutoGenerate = false,
25-
OnPullRequestBranches = new[] { MasterBranch, ReleaseBranch },
26-
PublishArtifacts = false,
27-
InvokedTargets = new[] { nameof(Verify), nameof(Cover), nameof(Pack) })]
28-
[GitHubActions(
29-
"release",
30-
GitHubActionsImage.WindowsLatest,
31-
AutoGenerate = false,
32-
OnPushTags = new[] { "v*" },
33-
PublishArtifacts = true,
34-
InvokedTargets = new[] { nameof(Verify), nameof(Cover), nameof(Publish) },
35-
ImportSecrets = new[] { Secrets.NuGetApiKey })]
3621
partial class Build : NukeBuild
3722
{
3823
public static int Main() => Execute<Build>(x => x.Compile);
@@ -50,9 +35,6 @@ partial class Build : NukeBuild
5035

5136
[Parameter("Forces the continuous integration build flag")] readonly bool CI;
5237

53-
[Secret] [Parameter("NuGet API Key (secret)", Name = Secrets.NuGetApiKey)] readonly string NuGetApiKey;
54-
readonly string NuGetSource = "https://api.nuget.org/v3/index.json";
55-
5638
IEnumerable<Project> Excluded => new[]
5739
{
5840
Solution.GetProject("_build"),
@@ -208,24 +190,4 @@ partial class Build : NukeBuild
208190
.Add("/p:CheckEolTargetFramework=false"))
209191
.CombineWith(FSharpLibraries, (s, p) => s.SetProject(p)));
210192
});
211-
212-
Target Publish => _ => _
213-
.DependsOn(Pack)
214-
.Consumes(Pack)
215-
.Executes(() =>
216-
{
217-
DotNetNuGetPush(s => s
218-
.EnableSkipDuplicate()
219-
.When(
220-
GitHubActions.IsOnSemVerTag(),
221-
v => v
222-
.SetApiKey(NuGetApiKey)
223-
.SetSource(NuGetSource))
224-
.CombineWith(Packages, (_, p) => _.SetTargetPath(p)));
225-
});
226-
227-
public static class Secrets
228-
{
229-
public const string NuGetApiKey = "NUGET_API_KEY";
230-
}
231193
}

0 commit comments

Comments
 (0)