Skip to content

Commit 660454f

Browse files
committed
(GH-2120) pushing nuget to GitHub Packages
1 parent 5c7346c commit 660454f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ jobs:
169169
fail-fast: false
170170
env:
171171
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
172+
GITHUB_USERNAME: ${{ secrets.GITHUB_USERNAME }}
172173
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
173174
NUGET_API_URL: ${{ secrets.NUGET_API_URL }}
174175
CHOCOLATEY_API_KEY: ${{ secrets.CHOCOLATEY_API_KEY }}

build/publish.cake

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,39 @@ Task("Publish-NuGet-Internal")
9999
{
100100
if (FileExists(package.PackagePath))
101101
{
102-
// Push the package.
103-
DotNetCoreNuGetPush(package.PackagePath.FullPath, new DotNetCoreNuGetPushSettings
102+
// Push the package to nuget.org
103+
NuGetPush(package.PackagePath, new NuGetPushSettings
104104
{
105105
ApiKey = apiKey,
106106
Source = apiUrl
107107
});
108+
109+
// Push the package to GitHub Packages
110+
if (parameters.IsRunningOnGitHubActions) {
111+
var token = parameters.Credentials.GitHub.Token;
112+
if(string.IsNullOrEmpty(token)) {
113+
throw new InvalidOperationException("Could not resolve Github token.");
114+
}
115+
var userName = parameters.Credentials.GitHub.UserName;
116+
if(string.IsNullOrEmpty(userName)) {
117+
throw new InvalidOperationException("Could not resolve Github userName.");
118+
}
119+
120+
var source = $"https://nuget.pkg.github.com/{BuildParameters.MainRepoOwner}/index.json";
121+
122+
var nugetSourceSettings = new NuGetSourcesSettings
123+
{
124+
UserName = parameters.Credentials.GitHub.UserName,
125+
Password = token
126+
};
127+
128+
Information("Adding NuGet source with user/pass...");
129+
NuGetAddSource("GitHub", source, nugetSourceSettings);
130+
NuGetPush(package.PackagePath, new NuGetPushSettings
131+
{
132+
Source = source
133+
});
134+
}
108135
}
109136
}
110137
})

build/utils/credentials.cake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ public class BuildCredentials
2727

2828
public class GitHubCredentials
2929
{
30+
public string UserName { get; private set; }
3031
public string Token { get; private set; }
3132

32-
public GitHubCredentials(string token)
33+
public GitHubCredentials(string userName, string token)
3334
{
3435
Token = token;
3536
}
3637

3738
public static GitHubCredentials GetGitHubCredentials(ICakeContext context)
3839
{
39-
return new GitHubCredentials(context.EnvironmentVariable("GITHUB_TOKEN"));
40+
return new GitHubCredentials(
41+
context.EnvironmentVariable("GITHUB_USERNAME"),
42+
context.EnvironmentVariable("GITHUB_TOKEN"));
4043
}
4144
}
4245

0 commit comments

Comments
 (0)