Skip to content

Commit

Permalink
feat(cicd): add ConcurrencyGroup and ConcurrencyCancelInProgress to G…
Browse files Browse the repository at this point in the history
…itHubActionsAttribute (#1299)
  • Loading branch information
lahma committed Jul 3, 2024
1 parent bdb9287 commit b8273bc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ on:
schedule:
- cron: '* 0 * * *'

concurrency:
group: ${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
macos-latest:
name: macos-latest
runs-on: macos-latest
timeout-minutes: 30
concurrency:
group: ${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.run_id }}
group: custom-job-group
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -95,7 +99,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 30
concurrency:
group: ${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.run_id }}
group: custom-job-group
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -140,7 +144,7 @@ jobs:
runs-on: windows-latest
timeout-minutes: 30
concurrency:
group: ${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.run_id }}
group: custom-job-group
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 3 additions & 1 deletion source/Nuke.Common.Tests/CI/ConfigurationGenerationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ yield return
Progress = false,
Filter = "tree:0",
TimeoutMinutes = 30,
JobConcurrencyCancelInProgress = true
ConcurrencyCancelInProgress = true,
JobConcurrencyCancelInProgress = true,
JobConcurrencyGroup = "custom-job-group"
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class GitHubActionsConfiguration : ConfigurationEntity
public GitHubActionsTrigger[] ShortTriggers { get; set; }
public GitHubActionsDetailedTrigger[] DetailedTriggers { get; set; }
public (GitHubActionsPermissions Type, string Permission)[] Permissions { get; set; }
public string ConcurrencyGroup { get; set; }
public bool ConcurrencyCancelInProgress { get; set; }
public GitHubActionsJob[] Jobs { get; set; }

public override void Write(CustomFileWriter writer)
Expand Down Expand Up @@ -46,6 +48,28 @@ public override void Write(CustomFileWriter writer)
}
}

if (!ConcurrencyGroup.IsNullOrWhiteSpace() || ConcurrencyCancelInProgress)
{
writer.WriteLine();
writer.WriteLine("concurrency:");
using (writer.Indent())
{
var group = ConcurrencyGroup;
if (group.IsNullOrWhiteSpace())
{
// create a default value that only cancels in-progress runs of the same workflow
// we don't fall back to github.ref which would disable multiple runs in main/master which is usually what is wanted
group = "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.run_id }}";
}

writer.WriteLine($"group: {group}");
if (ConcurrencyCancelInProgress)
{
writer.WriteLine("cancel-in-progress: true");
}
}
}

writer.WriteLine();

writer.WriteLine("jobs:");
Expand Down
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 @@ -77,6 +77,9 @@ public class GitHubActionsAttribute : ConfigurationAttributeBase

public int TimeoutMinutes { get; set; }

public string ConcurrencyGroup { get; set; }
public bool ConcurrencyCancelInProgress { get; set; }

public string JobConcurrencyGroup { get; set; }
public bool JobConcurrencyCancelInProgress { get; set; }

Expand Down Expand Up @@ -126,6 +129,8 @@ public override ConfigurationEntity GetConfiguration(IReadOnlyCollection<Executa
DetailedTriggers = GetTriggers().ToArray(),
Permissions = WritePermissions.Select(x => (x, "write"))
.Concat(ReadPermissions.Select(x => (x, "read"))).ToArray(),
ConcurrencyGroup = ConcurrencyGroup,
ConcurrencyCancelInProgress = ConcurrencyCancelInProgress,
Jobs = _images.Select(x => GetJobs(x, relevantTargets)).ToArray()
};

Expand Down

0 comments on commit b8273bc

Please sign in to comment.