Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify labels and group for self-hosted runners #1363

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions build/Build.GlobalSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Nuke.Common.Utilities;
using Nuke.Utilities.Text.Yaml;
using static Nuke.Common.ControlFlow;
using static Nuke.Common.IO.FileSystemTasks;
using static Nuke.Common.ProjectModel.SolutionModelTasks;
using static Nuke.Common.Tools.Git.GitTasks;

Expand Down Expand Up @@ -67,18 +66,16 @@ IEnumerable<GitRepository> ExternalRepositories

if ((RootDirectory / $"{Solution.FileName}.DotSettings").FileExists())
{
CopyFile(
source: RootDirectory / $"{Solution.FileName}.DotSettings",
(RootDirectory / $"{Solution.FileName}.DotSettings").Copy(
target: RootDirectory / $"{global.FileName}.DotSettings",
FileExistsPolicy.Overwrite);
policy: ExistsPolicy.FileOverwrite);
}

if ((RootDirectory / $"{Solution.FileName}.DotSettings.user").FileExists())
{
CopyFile(
source: RootDirectory / $"{Solution.FileName}.DotSettings.user",
(RootDirectory / $"{Solution.FileName}.DotSettings.user").Copy(
target: RootDirectory / $"{global.FileName}.DotSettings.user",
FileExistsPolicy.Overwrite);
policy: ExistsPolicy.FileOverwrite);
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# ------------------------------------------------------------------------------
# <auto-generated>
#
# This code was generated.
#
# - To turn off auto-generation set:
#
# [TestGitHubActions (AutoGenerate = false)]
#
# - To trigger manual generation invoke:
#
# nuke --generate-configuration GitHubActions_test --host GitHubActions
#
# </auto-generated>
# ------------------------------------------------------------------------------

name: test

on: [push, pull_request]

permissions:
contents: write
actions: read

jobs:
ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
uses: actions/cache@v4
with:
path: |
.nuke/temp
~/.nuget/packages
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
- name: 'Run: Test'
run: ./build.cmd Test
env:
ApiKey: ${{ secrets.API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 'Publish: src'
uses: actions/upload-artifact@v4
with:
name: src
path: src
- name: 'Publish: test-results'
uses: actions/upload-artifact@v4
with:
name: test-results
path: output/test-results
- name: 'Publish: coverage-report.zip'
uses: actions/upload-artifact@v4
with:
name: coverage-report.zip
path: output/coverage-report.zip
self-hosted:
name: self-hosted
runs-on: [ self-hosted, label1, label2 ]
steps:
- uses: actions/checkout@v3
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
uses: actions/cache@v4
with:
path: |
.nuke/temp
~/.nuget/packages
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
- name: 'Run: Test'
run: ./build.cmd Test
env:
ApiKey: ${{ secrets.API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 'Publish: src'
uses: actions/upload-artifact@v4
with:
name: src
path: src
- name: 'Publish: test-results'
uses: actions/upload-artifact@v4
with:
name: test-results
path: output/test-results
- name: 'Publish: coverage-report.zip'
uses: actions/upload-artifact@v4
with:
name: coverage-report.zip
path: output/coverage-report.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# ------------------------------------------------------------------------------
# <auto-generated>
#
# This code was generated.
#
# - To turn off auto-generation set:
#
# [TestGitHubActions (AutoGenerate = false)]
#
# - To trigger manual generation invoke:
#
# nuke --generate-configuration GitHubActions_test --host GitHubActions
#
# </auto-generated>
# ------------------------------------------------------------------------------

name: test

on: [push, pull_request]

permissions:
contents: write
actions: read

jobs:
ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
uses: actions/cache@v4
with:
path: |
.nuke/temp
~/.nuget/packages
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
- name: 'Run: Test'
run: ./build.cmd Test
env:
ApiKey: ${{ secrets.API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 'Publish: src'
uses: actions/upload-artifact@v4
with:
name: src
path: src
- name: 'Publish: test-results'
uses: actions/upload-artifact@v4
with:
name: test-results
path: output/test-results
- name: 'Publish: coverage-report.zip'
uses: actions/upload-artifact@v4
with:
name: coverage-report.zip
path: output/coverage-report.zip
self-hosted:
name: self-hosted
runs-on:
group: SomeGroup
labels: [ self-hosted, label1, label2 ]
steps:
- uses: actions/checkout@v3
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
uses: actions/cache@v4
with:
path: |
.nuke/temp
~/.nuget/packages
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
- name: 'Run: Test'
run: ./build.cmd Test
env:
ApiKey: ${{ secrets.API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 'Publish: src'
uses: actions/upload-artifact@v4
with:
name: src
path: src
- name: 'Publish: test-results'
uses: actions/upload-artifact@v4
with:
name: test-results
path: output/test-results
- name: 'Publish: coverage-report.zip'
uses: actions/upload-artifact@v4
with:
name: coverage-report.zip
path: output/coverage-report.zip
35 changes: 35 additions & 0 deletions source/Nuke.Common.Tests/CI/ConfigurationGenerationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,41 @@ public class TestBuild : NukeBuild
}
);

yield return
(
"self-hosted",
new TestGitHubActionsAttribute(
GitHubActionsImage.UbuntuLatest,
GitHubActionsImage.SelfHosted)
{
On = new[] { GitHubActionsTrigger.Push, GitHubActionsTrigger.PullRequest },
CustomRunnerLabels = ["label1", "label2"],
CustomRunnerGroup = "SomeGroup",
InvokedTargets = new[] { nameof(Test) },
ImportSecrets = new[] { nameof(ApiKey) },
EnableGitHubToken = true,
WritePermissions = new[] { GitHubActionsPermissions.Contents },
ReadPermissions = new[] { GitHubActionsPermissions.Actions }
}
);

yield return
(
"self-hosted-NoCustomGroup",
new TestGitHubActionsAttribute(
GitHubActionsImage.UbuntuLatest,
GitHubActionsImage.SelfHosted)
{
On = new[] { GitHubActionsTrigger.Push, GitHubActionsTrigger.PullRequest },
CustomRunnerLabels = ["label1", "label2"],
InvokedTargets = new[] { nameof(Test) },
ImportSecrets = new[] { nameof(ApiKey) },
EnableGitHubToken = true,
WritePermissions = new[] { GitHubActionsPermissions.Contents },
ReadPermissions = new[] { GitHubActionsPermissions.Actions }
}
);

yield return
(
null,
Expand Down
7 changes: 7 additions & 0 deletions source/Nuke.Common.Tests/Nuke.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@
<ProjectReference Include="..\Nuke.Common\Nuke.Common.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="CI\ConfigurationGenerationTest.Test_testName=self-hosted_attribute=GitHubActionsAttribute.verified.txt">
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
<DependentUpon>%(ParentFile).cs</DependentUpon>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Maintainers of NUKE.
// Copyright 2023 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

Expand All @@ -16,6 +16,8 @@ public class GitHubActionsJob : ConfigurationEntity
{
public string Name { get; set; }
public GitHubActionsImage Image { get; set; }
public string[] CustomRunnerLabels { get; set; } = [];
public string CustomRunnerGroup { get; set; }
public int TimeoutMinutes { get; set; }
public string ConcurrencyGroup { get; set; }
public bool ConcurrencyCancelInProgress { get; set; }
Expand All @@ -28,7 +30,8 @@ public override void Write(CustomFileWriter writer)
using (writer.Indent())
{
writer.WriteLine($"name: {Name}");
writer.WriteLine($"runs-on: {Image.GetValue()}");

WriteRunsOn(writer);

if (TimeoutMinutes > 0)
{
Expand Down Expand Up @@ -63,4 +66,36 @@ public override void Write(CustomFileWriter writer)
}
}
}

private void WriteRunsOn(CustomFileWriter writer)
{
if (Image != GitHubActionsImage.SelfHosted)
{
writer.WriteLine($"runs-on: {Image.GetValue()}");
return;
}

if (!CustomRunnerGroup.IsNullOrWhiteSpace())
{
writer.WriteLine($"runs-on:");

using (writer.Indent())
{
writer.WriteLine($"group: {CustomRunnerGroup}");

if (CustomRunnerLabels.Length > 0)
{
writer.WriteInlineArray("labels", [Image.GetValue(), .. CustomRunnerLabels]);
}
}
}
else if (CustomRunnerLabels.Length > 0)
{
writer.WriteInlineArray("runs-on", [Image.GetValue(), .. CustomRunnerLabels]);
}
else
{
writer.WriteLine($"runs-on: {Image.GetValue()}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Linq;
using Nuke.Common.Utilities;

namespace Nuke.Common.CI.GitHubActions.Configuration;

public static class GithubActionsCustomWriterExtensions
{
public static void WriteInlineArray(this CustomFileWriter writer, string property, string[] values)
{
if (values.Length == 0)
{
return;
}

if (values.Length <= 1)
{
writer.WriteLine($"{property}: {values.Single()}");
return;
}

writer.WriteLine($"{property}: [ {values.JoinCommaSpace()} ]");
}
}
5 changes: 5 additions & 0 deletions source/Nuke.Common/CI/GitHubActions/GitHubActionsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public GitHubActionsAttribute(

public string[] InvokedTargets { get; set; } = new string[0];

public string[] CustomRunnerLabels { get; set; } = [];
public string CustomRunnerGroup { get; set; }

public GitHubActionsSubmodules Submodules
{
set => _submodules = value;
Expand Down Expand Up @@ -130,6 +133,8 @@ protected virtual GitHubActionsJob GetJobs(GitHubActionsImage image, IReadOnlyCo
Name = image.GetValue().Replace(".", "_"),
Steps = GetSteps(image, relevantTargets).ToArray(),
Image = image,
CustomRunnerGroup = CustomRunnerGroup,
CustomRunnerLabels = CustomRunnerLabels,
TimeoutMinutes = TimeoutMinutes,
ConcurrencyGroup = JobConcurrencyGroup,
ConcurrencyCancelInProgress = JobConcurrencyCancelInProgress
Expand Down
Loading