Search code examples
c#githuboctokit.netnuke-build

How to create a GitHub Release in NukeBuild script / Octokit.Net


I want to create a NukeBuild script that is capable of creating a GitHub Release for me (the CreateReleaseOnGithub Target in the linked build script). However I'm getting a Oktokit.NotFoundException without much else to go on then a URL to the documentation at https://docs.github.com/rest/releases/releases#create-a-release. Part of the stacktrace before it at my own custom build script calling IReleaseClient.Create(string, string, NewReleaseData):

   at Octokit.Connection.HandleErrors(IResponse response) in /_/Octokit/Http/Connection.cs:line 780
   at Octokit.Connection.RunRequest(IRequest request, CancellationToken cancellationToken, Func`2 preprocessResponseBody) in /_/Octokit/Http/Connection.cs:line 761
   at Octokit.Connection.Run[T](IRequest request, CancellationToken cancellationToken, Func`2 preprocessResponseBody) in /_/Octokit/Http/Connection.cs:line 746
   at Octokit.ApiConnection.Post[T](Uri uri, Object data, String accepts, String contentType, CancellationToken cancellationToken)

For reference, consider this the code I'm executing within my Target:

var productInformation = new ProductHeaderValue("MyApp-NukeBuildScript");
GitHubTasks.GitHubClient = new GitHubClient(productInformation)
{
    Credentials = new Credentials(GitHubUser, GitHubPassword) // My GitHub credentials I provide as command line arguments when executing the script
};

var releaseData = new NewRelease(Version)
{
    Prerelease = true,
    Draft = true,
    Body = "Test"
};

Release release = await GitHubTasks.GitHubClient.Repository.Release
    .Create(Repository.GetGitHubOwner(), Repository.GetGitHubName(), releaseData);

where Repository is a field defined within the class as

[GitRepository] readonly GitRepository Repository;

I suspected I might have a typo in the owner or name arguments, and that this would result in not being able to resolve the repository I want to create the Release in. However in an experiment I've executed the following code in a NukeBuild target:

string branchName = await GitHubTasks.GetDefaultBranch(Repository);

which is a piece of Code I pulled from the NukeBuild source code within the GitHubTasks source. And when I run this code, I successfully get the name of my repository.

So it seems that my problem is not related to the Repository.GetGitHubOwner() or Repository.GetGitHubName() arguments being passed to the Create(string, string, ...) method, which leaves me confused.

Why is my piece of code unable to create a Github Release?


Solution

  • I have not been able to make it work with the 'username + password' overload of the Credentials class.

    I did manage to get the intergration to work with a Github Personal Access Token.

    1. Create a Personal Access Token (PAT) in Github (source: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token) Make sure it has the grant for Reading and Writing of contents, in order to be authorized to create Github Releases (as well as uploading files to that release).
    2. Make sure you store the PAT somewhere safe, like a password safe.
    3. Define a Parameter in the NukeBuild script for passing in the PAT. Ensure you decorate the property with [Secret] as you are dealing with secret data here. From the root of the repository, run nuke on the commandline to build the script and populate the build.schema.json file in the .nuke folder.
    4. In the .nuke folder, create a parameters file to store the data, for example parameters.secrets.json, with file contents {} (an empty json object). Consider putting this file in gitignore, depending on your situation.
    5. From your repository root, open command line and run nuke :secrets secrets. Provide a password (also save this somewhere, as you'll need it to decrypt the file again later!).
    6. Navigate the menu and select the PAT parameter you've defined. Fill in the PAT. Then <Save and Exit>. This now saves the PAT in encrypted format in that secrets file.
    7. Run the buildscript target, providing the secrets profile: nuke --target MyTarget --profile secrets. You'll be asked the password for decrypting the secrets file (from step 5). Provide it, and the script will now create the Release in Github.