Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 694f7fc

Browse files
committed
Add ability to specify labels and group for self-hosted runners
1 parent 6eb7796 commit 694f7fc

7 files changed

+292
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# ------------------------------------------------------------------------------
2+
# <auto-generated>
3+
#
4+
# This code was generated.
5+
#
6+
# - To turn off auto-generation set:
7+
#
8+
# [TestGitHubActions (AutoGenerate = false)]
9+
#
10+
# - To trigger manual generation invoke:
11+
#
12+
# nuke --generate-configuration GitHubActions_test --host GitHubActions
13+
#
14+
# </auto-generated>
15+
# ------------------------------------------------------------------------------
16+
17+
name: test
18+
19+
on: [push, pull_request]
20+
21+
permissions:
22+
contents: write
23+
actions: read
24+
25+
jobs:
26+
ubuntu-latest:
27+
name: ubuntu-latest
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v3
31+
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
32+
uses: actions/cache@v4
33+
with:
34+
path: |
35+
.nuke/temp
36+
~/.nuget/packages
37+
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
38+
- name: 'Run: Test'
39+
run: ./build.cmd Test
40+
env:
41+
ApiKey: ${{ secrets.API_KEY }}
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
- name: 'Publish: src'
44+
uses: actions/upload-artifact@v4
45+
with:
46+
name: src
47+
path: src
48+
- name: 'Publish: test-results'
49+
uses: actions/upload-artifact@v4
50+
with:
51+
name: test-results
52+
path: output/test-results
53+
- name: 'Publish: coverage-report.zip'
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: coverage-report.zip
57+
path: output/coverage-report.zip
58+
self-hosted:
59+
name: self-hosted
60+
runs-on: [ self-hosted, label1, label2 ]
61+
steps:
62+
- uses: actions/checkout@v3
63+
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
64+
uses: actions/cache@v4
65+
with:
66+
path: |
67+
.nuke/temp
68+
~/.nuget/packages
69+
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
70+
- name: 'Run: Test'
71+
run: ./build.cmd Test
72+
env:
73+
ApiKey: ${{ secrets.API_KEY }}
74+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
75+
- name: 'Publish: src'
76+
uses: actions/upload-artifact@v4
77+
with:
78+
name: src
79+
path: src
80+
- name: 'Publish: test-results'
81+
uses: actions/upload-artifact@v4
82+
with:
83+
name: test-results
84+
path: output/test-results
85+
- name: 'Publish: coverage-report.zip'
86+
uses: actions/upload-artifact@v4
87+
with:
88+
name: coverage-report.zip
89+
path: output/coverage-report.zip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# ------------------------------------------------------------------------------
2+
# <auto-generated>
3+
#
4+
# This code was generated.
5+
#
6+
# - To turn off auto-generation set:
7+
#
8+
# [TestGitHubActions (AutoGenerate = false)]
9+
#
10+
# - To trigger manual generation invoke:
11+
#
12+
# nuke --generate-configuration GitHubActions_test --host GitHubActions
13+
#
14+
# </auto-generated>
15+
# ------------------------------------------------------------------------------
16+
17+
name: test
18+
19+
on: [push, pull_request]
20+
21+
permissions:
22+
contents: write
23+
actions: read
24+
25+
jobs:
26+
ubuntu-latest:
27+
name: ubuntu-latest
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v3
31+
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
32+
uses: actions/cache@v4
33+
with:
34+
path: |
35+
.nuke/temp
36+
~/.nuget/packages
37+
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
38+
- name: 'Run: Test'
39+
run: ./build.cmd Test
40+
env:
41+
ApiKey: ${{ secrets.API_KEY }}
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
- name: 'Publish: src'
44+
uses: actions/upload-artifact@v4
45+
with:
46+
name: src
47+
path: src
48+
- name: 'Publish: test-results'
49+
uses: actions/upload-artifact@v4
50+
with:
51+
name: test-results
52+
path: output/test-results
53+
- name: 'Publish: coverage-report.zip'
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: coverage-report.zip
57+
path: output/coverage-report.zip
58+
self-hosted:
59+
name: self-hosted
60+
runs-on:
61+
group: SomeGroup
62+
labels: [ self-hosted, label1, label2 ]
63+
steps:
64+
- uses: actions/checkout@v3
65+
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
66+
uses: actions/cache@v4
67+
with:
68+
path: |
69+
.nuke/temp
70+
~/.nuget/packages
71+
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}
72+
- name: 'Run: Test'
73+
run: ./build.cmd Test
74+
env:
75+
ApiKey: ${{ secrets.API_KEY }}
76+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77+
- name: 'Publish: src'
78+
uses: actions/upload-artifact@v4
79+
with:
80+
name: src
81+
path: src
82+
- name: 'Publish: test-results'
83+
uses: actions/upload-artifact@v4
84+
with:
85+
name: test-results
86+
path: output/test-results
87+
- name: 'Publish: coverage-report.zip'
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: coverage-report.zip
91+
path: output/coverage-report.zip

source/Nuke.Common.Tests/CI/ConfigurationGenerationTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,41 @@ public class TestBuild : NukeBuild
162162
}
163163
);
164164

165+
yield return
166+
(
167+
"self-hosted",
168+
new TestGitHubActionsAttribute(
169+
GitHubActionsImage.UbuntuLatest,
170+
GitHubActionsImage.SelfHosted)
171+
{
172+
On = new[] { GitHubActionsTrigger.Push, GitHubActionsTrigger.PullRequest },
173+
CustomRunnerLabels = ["label1", "label2"],
174+
CustomRunnerGroup = "SomeGroup",
175+
InvokedTargets = new[] { nameof(Test) },
176+
ImportSecrets = new[] { nameof(ApiKey) },
177+
EnableGitHubToken = true,
178+
WritePermissions = new[] { GitHubActionsPermissions.Contents },
179+
ReadPermissions = new[] { GitHubActionsPermissions.Actions }
180+
}
181+
);
182+
183+
yield return
184+
(
185+
"self-hosted-NoCustomGroup",
186+
new TestGitHubActionsAttribute(
187+
GitHubActionsImage.UbuntuLatest,
188+
GitHubActionsImage.SelfHosted)
189+
{
190+
On = new[] { GitHubActionsTrigger.Push, GitHubActionsTrigger.PullRequest },
191+
CustomRunnerLabels = ["label1", "label2"],
192+
InvokedTargets = new[] { nameof(Test) },
193+
ImportSecrets = new[] { nameof(ApiKey) },
194+
EnableGitHubToken = true,
195+
WritePermissions = new[] { GitHubActionsPermissions.Contents },
196+
ReadPermissions = new[] { GitHubActionsPermissions.Actions }
197+
}
198+
);
199+
165200
yield return
166201
(
167202
null,

source/Nuke.Common.Tests/Nuke.Common.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@
88
<ProjectReference Include="..\Nuke.Common\Nuke.Common.csproj" />
99
</ItemGroup>
1010

11+
<ItemGroup>
12+
<None Update="CI\ConfigurationGenerationTest.Test_testName=self-hosted_attribute=GitHubActionsAttribute.verified.txt">
13+
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
14+
<DependentUpon>%(ParentFile).cs</DependentUpon>
15+
</None>
16+
</ItemGroup>
17+
1118
</Project>

source/Nuke.Common/CI/GitHubActions/Configuration/GitHubActionsJob.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Maintainers of NUKE.
1+
// Copyright 2023 Maintainers of NUKE.
22
// Distributed under the MIT License.
33
// https://github.com/nuke-build/nuke/blob/master/LICENSE
44

@@ -16,6 +16,8 @@ public class GitHubActionsJob : ConfigurationEntity
1616
{
1717
public string Name { get; set; }
1818
public GitHubActionsImage Image { get; set; }
19+
public string[] CustomRunnerLabels { get; set; } = [];
20+
public string CustomRunnerGroup { get; set; }
1921
public int TimeoutMinutes { get; set; }
2022
public string ConcurrencyGroup { get; set; }
2123
public bool ConcurrencyCancelInProgress { get; set; }
@@ -28,7 +30,8 @@ public override void Write(CustomFileWriter writer)
2830
using (writer.Indent())
2931
{
3032
writer.WriteLine($"name: {Name}");
31-
writer.WriteLine($"runs-on: {Image.GetValue()}");
33+
34+
WriteRunsOn(writer);
3235

3336
if (TimeoutMinutes > 0)
3437
{
@@ -63,4 +66,36 @@ public override void Write(CustomFileWriter writer)
6366
}
6467
}
6568
}
69+
70+
private void WriteRunsOn(CustomFileWriter writer)
71+
{
72+
if (Image != GitHubActionsImage.SelfHosted)
73+
{
74+
writer.WriteLine($"runs-on: {Image.GetValue()}");
75+
return;
76+
}
77+
78+
if (!CustomRunnerGroup.IsNullOrWhiteSpace())
79+
{
80+
writer.WriteLine($"runs-on:");
81+
82+
using (writer.Indent())
83+
{
84+
writer.WriteLine($"group: {CustomRunnerGroup}");
85+
86+
if (CustomRunnerLabels.Length > 0)
87+
{
88+
writer.WriteInlineArray("labels", [Image.GetValue(), .. CustomRunnerLabels]);
89+
}
90+
}
91+
}
92+
else if (CustomRunnerLabels.Length > 0)
93+
{
94+
writer.WriteInlineArray("runs-on", [Image.GetValue(), .. CustomRunnerLabels]);
95+
}
96+
else
97+
{
98+
writer.WriteLine($"runs-on: {Image.GetValue()}");
99+
}
100+
}
66101
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2023 Maintainers of NUKE.
2+
// Distributed under the MIT License.
3+
// https://github.com/nuke-build/nuke/blob/master/LICENSE
4+
5+
using System;
6+
using System.Linq;
7+
using Nuke.Common.Utilities;
8+
9+
namespace Nuke.Common.CI.GitHubActions.Configuration;
10+
11+
public static class GithubActionsCustomWriterExtensions
12+
{
13+
public static void WriteInlineArray(this CustomFileWriter writer, string property, string[] values)
14+
{
15+
if (values.Length == 0)
16+
{
17+
return;
18+
}
19+
20+
if (values.Length <= 1)
21+
{
22+
writer.WriteLine($"{property}: {values.Single()}");
23+
return;
24+
}
25+
26+
writer.WriteLine($"{property}: [ {values.JoinCommaSpace()} ]");
27+
}
28+
}

source/Nuke.Common/CI/GitHubActions/GitHubActionsAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public GitHubActionsAttribute(
8080

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

83+
public string[] CustomRunnerLabels { get; set; } = [];
84+
public string CustomRunnerGroup { get; set; }
85+
8386
public GitHubActionsSubmodules Submodules
8487
{
8588
set => _submodules = value;
@@ -130,6 +133,8 @@ protected virtual GitHubActionsJob GetJobs(GitHubActionsImage image, IReadOnlyCo
130133
Name = image.GetValue().Replace(".", "_"),
131134
Steps = GetSteps(image, relevantTargets).ToArray(),
132135
Image = image,
136+
CustomRunnerGroup = CustomRunnerGroup,
137+
CustomRunnerLabels = CustomRunnerLabels,
133138
TimeoutMinutes = TimeoutMinutes,
134139
ConcurrencyGroup = JobConcurrencyGroup,
135140
ConcurrencyCancelInProgress = JobConcurrencyCancelInProgress

0 commit comments

Comments
 (0)