GitHub.Actions.Core 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package GitHub.Actions.Core --version 0.0.2                
NuGet\Install-Package GitHub.Actions.Core -Version 0.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GitHub.Actions.Core" Version="0.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GitHub.Actions.Core --version 0.0.2                
#r "nuget: GitHub.Actions.Core, 0.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GitHub.Actions.Core as a Cake Addin
#addin nuget:?package=GitHub.Actions.Core&version=0.0.2

// Install GitHub.Actions.Core as a Cake Tool
#tool nuget:?package=GitHub.Actions.Core&version=0.0.2                

GitHub Actions.Core .NET SDK

The .NET equivalent of the official GitHub actions/toolkit @actions/core project.

Blog

🔗 Hello from the GitHub Actions.Core .NET SDK

Usage

Installing the NuGet package 📦

Welcome to the Actions.Core .NET SDK. This SDK is used to create GitHub Actions in .NET. The SDK is a thin wrapper around the .NET implementation of the GitHub Actions a select few packages from the @actions/toolkit.

Warning: This package is not an official Microsoft or GitHub product. It is a community-driven project.

You'll need to install the GitHub Actions.Core .NET SDK NuGet package to use the .NET APIs. The package is available on NuGet.org. The following is the command to install the package:

Adding package references

Either add the package reference to your project file:

<PackageReference Include="Actions.Core" Version="0.0.1" />

Or use the dotnet add package .NET CLI command:

dotnet add package Actions.Core

Get the ICoreService instance

To use the ICoreService in your .NET project, register the services with an IServiceCollection instance by calling AddGitHubActionsCore and then your consuming code can require the ICoreService via constructor dependency injection.

using Microsoft.Extensions.DependencyInjection;
using Actions.Core;
using Actions.Core.Extensions;

using var provider = new ServiceCollection()
    .AddGitHubActionsCore()
    .BuildServiceProvider();

var core = provider.GetRequiredService<ICoreService>();

Actions.Core

This was modified, but borrowed from the core/README.md.

Core functions for setting results, logging, registering secrets and exporting variables across actions

Using declarations

global using Actions.Core;
Inputs/Outputs

Action inputs can be read with GetInput which returns a string or GetBoolInput which parses a bool based on the yaml 1.2 specification. If required is false, the input should have a default value in action.yml.

Outputs can be set with SetOutputAsync which makes them available to be mapped into inputs of other actions to ensure they are decoupled.

var myInput = core.GetInput("inputName", new InputOptions(true));
var myBoolInput = core.GetBoolInput("boolInputName", new InputOptions(true));
var myMultilineInput = core.GetMultilineInput("multilineInputName", new InputOptions(true));
await core.SetOutputAsync("outputKey", "outputVal");
Exporting variables

Since each step runs in a separate process, you can use ExportVariableAsync to add it to this step and future steps environment blocks.

await core.ExportVariableAsync("envVar", "Val");
Setting a secret

Setting a secret registers the secret with the runner to ensure it is masked in logs.

core.SetSecret("myPassword");
PATH manipulation

To make a tool's path available in the path for the remainder of the job (without altering the machine or containers state), use AddPathAsync. The runner will prepend the path given to the jobs PATH.

await core.AddPathAsync("/path/to/mytool");
Exit codes

You should use this library to set the failing exit code for your action. If status is not set and the script runs to completion, that will lead to a success.

using var provider = new ServiceCollection()
    .AddGitHubActions()
    .BuildServiceProvider();

var core = provider.GetRequiredService<ICoreService>();

try 
{
    // Do stuff
}
catch (Exception ex)
{
  // SetFailed logs the message and sets a failing exit code
  core.SetFailed($"Action failed with error {ex}"");
}
Logging

Finally, this library provides some utilities for logging. Note that debug logging is hidden from the logs by default. This behavior can be toggled by enabling the Step Debug Logs.

using var provider = new ServiceCollection()
    .AddGitHubActions()
    .BuildServiceProvider();

var core = provider.GetRequiredService<ICoreService>();

var myInput = core.GetInput("input");
try
{
    core.Debug("Inside try block");
    
    if (!myInput)
    {
        core.Warning("myInput was not set");
    }
    
    if (core.IsDebug)
    {
        // curl -v https://github.com
    }
    else
    {
        // curl https://github.com
    }
    
    // Do stuff
    core.Info("Output to the actions build log");
    
    core.Notice("This is a message that will also emit an annotation");
}
catch (Exception ex)
{
    core.Error($"Error {ex}, action may still succeed though");
}

This library can also wrap chunks of output in foldable groups.

using var provider = new ServiceCollection()
    .AddGitHubActions()
    .BuildServiceProvider();

var core = provider.GetRequiredService<ICoreService>();

// Manually wrap output
core.StartGroup("Do some function");
SomeFunction();
core.EndGroup();

// Wrap an asynchronous function call
var result = await core.GroupAsync("Do something async", async () =>
{
    var response = await MakeHttpRequestAsync();
    return response
});
Styling output

Colored output is supported in the Action logs via standard ANSI escape codes. 3/4 bit, 8 bit and 24 bit colors are all supported.

Foreground colors:

// 3/4 bit
core.Info("\u001b[35mThis foreground will be magenta");

// 8 bit
core.Info("\u001b[38;5;6mThis foreground will be cyan");

// 24 bit
core.Info("\u001b[38;2;255;0;0mThis foreground will be bright red");

Background colors:

// 3/4 bit
core.Info("\u001b[43mThis background will be yellow");

// 8 bit
core.Info("\u001b[48;5;6mThis background will be cyan");

// 24 bit
core.Info("\u001b[48;2;255;0;0mThis background will be bright red");

Special styles:

core.Info("\u001b[1mBold text");
core.Info("\u001b[3mItalic text");
core.Info("\u001b[4mUnderlined text");

ANSI escape codes can be combined with one another:

core.Info("\u001b[31;46mRed foreground with a cyan background and \u001b[1mbold text at the end");

Note: Escape codes reset at the start of each line.

core.Info("\u001b[35mThis foreground will be magenta");
core.Info("This foreground will reset to the default");
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.1.1 3,958 9/20/2024
8.1.0 3,909 8/3/2024
8.0.16 4,519 6/11/2024
8.0.15 2,531 5/7/2024
8.0.14 134 4/30/2024
8.0.13 1,548 4/8/2024
8.0.12 216 3/6/2024
8.0.11 265 2/2/2024
8.0.10 129 1/31/2024
8.0.9 129 1/29/2024
8.0.8 113 1/27/2024
8.0.7 112 1/25/2024
8.0.4 135 1/23/2024
8.0.3 6,209 11/26/2023
8.0.2 143 11/26/2023
8.0.1 154 11/22/2023
8.0.0 155 11/18/2023
8.0.0-rc.1.23419.4 271 9/14/2023
0.0.2 1,979 12/7/2022
0.0.1 344 12/1/2022