feat: Updated template to v3 (#30)

* First pass

* comments

* Updated CI

* Use nuget

* Nowarn NETSDK1206

* bump automate compose

* Bump automate sdk

* bump dockerfile

* bump sdk again
This commit is contained in:
Jedd Morgan
2025-06-10 10:04:06 +01:00
committed by GitHub
parent 1261df201f
commit f7d1420687
12 changed files with 112 additions and 54 deletions
@@ -3,7 +3,7 @@ on:
workflow_dispatch: workflow_dispatch:
push: push:
tags: tags:
- '*' - "*"
jobs: jobs:
publish-automate-function-version: # make sure the action works on a clean machine without building publish-automate-function-version: # make sure the action works on a clean machine without building
@@ -11,11 +11,11 @@ jobs:
FUNCTION_SCHEMA_FILE_NAME: functionSchema.json FUNCTION_SCHEMA_FILE_NAME: functionSchema.json
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
- name: Setup .NET Core SDK - name: Setup .NET Core SDK
uses: actions/setup-dotnet@v4.0.0 uses: actions/setup-dotnet@v4
with: with:
dotnet-version: 7.x dotnet-version: 8.x.x
- name: Restore dependencies - name: Restore dependencies
run: dotnet restore run: dotnet restore
- name: Extract functionInputSchema - name: Extract functionInputSchema
@@ -26,7 +26,7 @@ jobs:
dotnet run generate-schema ${HOME}/functionSchema.json dotnet run generate-schema ${HOME}/functionSchema.json
cat ${HOME}/functionSchema.json cat ${HOME}/functionSchema.json
- name: Speckle Automate Function - Build and Publish - name: Speckle Automate Function - Build and Publish
uses: specklesystems/speckle-automate-github-composite-action@0.8.1 uses: specklesystems/speckle-automate-github-composite-action@0.9.0
with: with:
speckle_function_command: "dotnet SpeckleAutomateDotnetExample.dll" speckle_function_command: "dotnet SpeckleAutomateDotnetExample.dll"
speckle_automate_url: ${{ env.SPECKLE_AUTOMATE_URL || vars.SPECKLE_AUTOMATE_URL || 'https://automate.speckle.dev' }} speckle_automate_url: ${{ env.SPECKLE_AUTOMATE_URL || vars.SPECKLE_AUTOMATE_URL || 'https://automate.speckle.dev' }}
+19
View File
@@ -0,0 +1,19 @@
name: "Test Building"
on:
pull_request:
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x.x
- name: Restore dependencies
run: dotnet restore
- name: Build
working-directory: "SpeckleAutomateDotnetExample"
run: |
dotnet build -warnaserror
+2 -2
View File
@@ -1,10 +1,10 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env FROM mcr.microsoft.com/dotnet/sdk:8.0 as build-env
WORKDIR /src WORKDIR /src
COPY SpeckleAutomateDotnetExample/ . COPY SpeckleAutomateDotnetExample/ .
RUN dotnet restore --use-current-runtime RUN dotnet restore --use-current-runtime
RUN dotnet publish --use-current-runtime --self-contained false --no-restore -o /publish RUN dotnet publish --use-current-runtime --self-contained false --no-restore -o /publish
FROM mcr.microsoft.com/dotnet/runtime:7.0 as runtime FROM mcr.microsoft.com/dotnet/runtime:8.0 as runtime
WORKDIR /publish WORKDIR /publish
COPY --from=build-env /publish . COPY --from=build-env /publish .
+4 -3
View File
@@ -33,7 +33,7 @@ This repository contains an example function that is compatible with Speckle Aut
## Getting started ## Getting started
This is essentially a template function, designed to serve as a starting point for creating your own function. The function targets dotnet 7.0 and uses the Speckle.Automate.SDK NuGet package, as well as the Objects Kit. This is essentially a template function, designed to serve as a starting point for creating your own function. The function targets dotnet 8.0 and uses the Speckle.Automate.SDK NuGet package.
At its core every Speckle Automate function is a CLI application with a specific, standardized set of available commands and arguments ([see below](#anatomy-of-a-function)). Each automate function is then built into a Docker image and published onto Speckle Automate. At its core every Speckle Automate function is a CLI application with a specific, standardized set of available commands and arguments ([see below](#anatomy-of-a-function)). Each automate function is then built into a Docker image and published onto Speckle Automate.
@@ -62,13 +62,14 @@ These arguments are automatically provided by the automate platform every time a
### Function Boilerplate (`Program.cs`) ### Function Boilerplate (`Program.cs`)
In this file, you'll find a call to `AutomationRunner.Main<TInput>`, which serves as your function's SDK entry point. This method handles argument parsing and accepts: This Program.cs file is the entry point to your CLI app.
It contains boilerplate to setup the Automate SDK services, Resolve your function, and run it via the call to `IAutomationRunner.Main<TInput>`. This method handles argument parsing and accepts:
- `args` -> the arguments provided by Speckle Automate, and - `args` -> the arguments provided by Speckle Automate, and
- `Func<AutomationContext, TInput>` -> Your custom function that gets executed when the automation is triggered. - `Func<AutomationContext, TInput>` -> Your custom function that gets executed when the automation is triggered.
> [!NOTE] > [!NOTE]
> If your function requires no inputs, there is also `AutomationRunner.Main` (non-generic) which takes in a `Func<AutomationContext>` instead. > If your function requires no inputs, there is also `IAutomationRunner.Main` (non-generic) which takes in a `Func<AutomationContext>` instead.
This sets up a CLI application with two commands: This sets up a CLI application with two commands:
@@ -1,16 +1,14 @@
using Objects;
using Speckle.Automate.Sdk; using Speckle.Automate.Sdk;
using Speckle.Core.Models.Extensions; using Speckle.Sdk.Models.Extensions;
public static class AutomateFunction public class AutomateFunction
{ {
public static async Task Run( public async Task Run(
AutomationContext automationContext, IAutomationContext automationContext,
FunctionInputs functionInputs FunctionInputs functionInputs
) )
{ {
Console.WriteLine("Starting execution"); Console.WriteLine("Starting execution");
_ = typeof(ObjectsKit).Assembly; // INFO: Force objects kit to initialize
Console.WriteLine("Receiving version"); Console.WriteLine("Receiving version");
var commitObject = await automationContext.ReceiveVersion(); var commitObject = await automationContext.ReceiveVersion();
@@ -23,8 +21,11 @@ public static class AutomateFunction
Console.WriteLine($"Counted {count} objects"); Console.WriteLine($"Counted {count} objects");
if (count < functionInputs.SpeckleTypeTargetCount) { if (count < functionInputs.SpeckleTypeTargetCount)
automationContext.MarkRunFailed($"Counted {count} objects where {functionInputs.SpeckleTypeTargetCount} were expected"); {
automationContext.MarkRunFailed(
$"Counted {count} objects where {functionInputs.SpeckleTypeTargetCount} were expected"
);
return; return;
} }
@@ -1,19 +1,19 @@
using Speckle.Automate.Sdk.DataAnnotations;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Speckle.Automate.Sdk.DataAnnotations;
/// <summary> /// <summary>
/// This class describes the user specified variables that the function wants to work with. /// This class describes the user specified variables that the function wants to work with.
/// </summary> /// </summary>
/// This class is used to generate a JSON Schema to ensure that the user provided values /// This class is used to generate a JSON Schema to ensure that the user provided values
/// are valid and match the required schema. /// are valid and match the required schema.
public struct FunctionInputs public readonly struct FunctionInputs
{ {
/// <summary> /// <summary>
/// The object type to count instances of in the given model version. /// The object type to count instances of in the given model version.
/// </summary> /// </summary>
[Required] [Required]
public string SpeckleTypeToCount; public string SpeckleTypeToCount { get; init; }
/// <summary> /// <summary>
/// The total number of the specified type expected. /// The total number of the specified type expected.
@@ -21,12 +21,12 @@ public struct FunctionInputs
[DefaultValue(10)] [DefaultValue(10)]
[Range(1, 100)] [Range(1, 100)]
[Required] [Required]
public int SpeckleTypeTargetCount; public int SpeckleTypeTargetCount { get; init; }
/// <summary> /// <summary>
/// An arbitrary example of using a secret input value. /// An arbitrary example of using a secret input value.
/// </summary> /// </summary>
[Required] [Required]
[Secret] [Secret]
public string ExternalServiceKey; public string ExternalServiceKey { get; init; }
} }
+12 -4
View File
@@ -1,6 +1,14 @@
using Speckle.Automate.Sdk; using Microsoft.Extensions.DependencyInjection;
using Speckle.Automate.Sdk;
//Boilerplate to setup Automate SDK
var serviceCollection = new ServiceCollection();
serviceCollection.AddAutomateSdk();
serviceCollection.AddSingleton<AutomateFunction>();
await using var container = serviceCollection.BuildServiceProvider();
var runner = container.GetRequiredService<IAutomationRunner>();
var function = container.GetRequiredService<AutomateFunction>();
// WARNING do not delete this call, this is the actual execution of your function // WARNING do not delete this call, this is the actual execution of your function
return await AutomationRunner return await runner.Main<FunctionInputs>(args, function.Run);
.Main<FunctionInputs>(args, AutomateFunction.Run)
.ConfigureAwait(false);
@@ -2,14 +2,15 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<NoWarn>$(NoWarn);NETSDK1206</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Speckle.Automate.Sdk" Version="2.21.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Speckle.Objects" Version="2.21.0" /> <PackageReference Include="Speckle.Automate.Sdk" Version="3.4.0-alpha.20" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+24 -16
View File
@@ -1,27 +1,35 @@
namespace TestAutomateFunction; using Microsoft.Extensions.DependencyInjection;
using Speckle.Automate.Sdk; using Speckle.Automate.Sdk;
using Speckle.Automate.Sdk.Test; using Speckle.Automate.Sdk.Test;
using Speckle.Core.Api; using Speckle.Sdk.Api;
using Speckle.Core.Api.GraphQL.Models; using Speckle.Sdk.Api.GraphQL.Models;
using Speckle.Core.Credentials; using Speckle.Sdk.Credentials;
namespace TestAutomateFunction;
[TestFixture] [TestFixture]
public sealed class AutomationContextTest : IDisposable public sealed class AutomationContextTest : IDisposable
{ {
private IClient _client;
private Client client; private Account _account;
private Account account; private IAutomationRunner _runner;
private AutomateFunction _function;
[OneTimeSetUp] [OneTimeSetUp]
public void Setup() public void Setup()
{ {
account = new Account var serviceProvider = ServiceRegistration.GetServiceProvider();
_account = new Account
{ {
token = TestAutomateEnvironment.GetSpeckleToken(), token = TestAutomateEnvironment.GetSpeckleToken(),
serverInfo = new ServerInfo { url = TestAutomateEnvironment.GetSpeckleServerUrl().ToString() } serverInfo = new ServerInfo
{
url = TestAutomateEnvironment.GetSpeckleServerUrl().ToString()
}
}; };
client = new Client(account); _client = serviceProvider.GetRequiredService<IClientFactory>().Create(_account);
_runner = serviceProvider.GetRequiredService<IAutomationRunner>();
_function = serviceProvider.GetRequiredService<AutomateFunction>();
} }
[Test] [Test]
@@ -33,11 +41,11 @@ public sealed class AutomationContextTest : IDisposable
SpeckleTypeTargetCount = 1 SpeckleTypeTargetCount = 1
}; };
var automationRunData = await TestAutomateUtils.CreateTestRun(client); var automationRunData = await TestAutomateUtils.CreateTestRun(_client);
var automationContext = await AutomationRunner.RunFunction( var automationContext = await _runner.RunFunction(
AutomateFunction.Run, _function.Run,
automationRunData, automationRunData,
account.token, _account.token,
inputs inputs
); );
@@ -46,7 +54,7 @@ public sealed class AutomationContextTest : IDisposable
public void Dispose() public void Dispose()
{ {
client.Dispose(); _client.Dispose();
TestAutomateEnvironment.Clear(); TestAutomateEnvironment.Clear();
} }
} }
@@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using Speckle.Automate.Sdk;
namespace TestAutomateFunction;
public static class ServiceRegistration
{
public static IServiceProvider GetServiceProvider()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddAutomateSdk();
serviceCollection.AddSingleton<AutomateFunction>();
return serviceCollection.BuildServiceProvider();
}
}
@@ -1,19 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0" />
<PackageReference Include="NUnit" Version="3.13.3"/> <PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/> <PackageReference Include="NUnit3TestAdapter" Version="5.0.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.3.0"/> <PackageReference Include="NUnit.Analyzers" Version="4.7.0">
<PackageReference Include="coverlet.collector" Version="3.1.2"/> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+1 -1
View File
@@ -1 +1 @@
global using NUnit.Framework; global using NUnit.Framework;