Skip to content

Commit 610358e

Browse files
Support array expressions in runs-on (#2088)
* Support array expressions in runs-on * Simplify appproach to use EvaluateYamlNode, fix case-sensitivity bug --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 1c16fd1 commit 610358e

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

cmd/platforms.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (i *Input) newPlatforms() map[string]string {
1515
for _, p := range i.platforms {
1616
pParts := strings.Split(p, "=")
1717
if len(pParts) == 2 {
18-
platforms[pParts[0]] = pParts[1]
18+
platforms[strings.ToLower(pParts[0])] = pParts[1]
1919
}
2020
}
2121
return platforms

pkg/runner/run_context.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -626,21 +626,30 @@ func (rc *RunContext) containerImage(ctx context.Context) string {
626626
}
627627

628628
func (rc *RunContext) runsOnImage(ctx context.Context) string {
629+
for _, platformName := range rc.runsOnPlatformNames(ctx) {
630+
image := rc.Config.Platforms[strings.ToLower(platformName)]
631+
if image != "" {
632+
return image
633+
}
634+
}
635+
636+
return ""
637+
}
638+
639+
func (rc *RunContext) runsOnPlatformNames(ctx context.Context) []string {
629640
job := rc.Run.Job()
630641

631642
if job.RunsOn() == nil {
632643
common.Logger(ctx).Errorf("'runs-on' key not defined in %s", rc.String())
644+
return []string{}
633645
}
634646

635-
for _, runnerLabel := range job.RunsOn() {
636-
platformName := rc.ExprEval.Interpolate(ctx, runnerLabel)
637-
image := rc.Config.Platforms[strings.ToLower(platformName)]
638-
if image != "" {
639-
return image
640-
}
647+
if err := rc.ExprEval.EvaluateYamlNode(ctx, &job.RawRunsOn); err != nil {
648+
common.Logger(ctx).Errorf("Error while evaluating runs-on: %v", err)
649+
return []string{}
641650
}
642651

643-
return ""
652+
return job.RunsOn()
644653
}
645654

646655
func (rc *RunContext) platformImage(ctx context.Context) string {
@@ -684,12 +693,7 @@ func (rc *RunContext) isEnabled(ctx context.Context) (bool, error) {
684693

685694
img := rc.platformImage(ctx)
686695
if img == "" {
687-
if job.RunsOn() == nil {
688-
l.Errorf("'runs-on' key not defined in %s", rc.String())
689-
}
690-
691-
for _, runnerLabel := range job.RunsOn() {
692-
platformName := rc.ExprEval.Interpolate(ctx, runnerLabel)
696+
for _, platformName := range rc.runsOnPlatformNames(ctx) {
693697
l.Infof("\U0001F6A7 Skipping unsupported platform -- Try running with `-P %+v=...`", platformName)
694698
}
695699
return false, nil
@@ -923,9 +927,7 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon
923927
setActionRuntimeVars(rc, env)
924928
}
925929

926-
job := rc.Run.Job()
927-
for _, runnerLabel := range job.RunsOn() {
928-
platformName := rc.ExprEval.Interpolate(ctx, runnerLabel)
930+
for _, platformName := range rc.runsOnPlatformNames(ctx) {
929931
if platformName != "" {
930932
if platformName == "ubuntu-latest" {
931933
// hardcode current ubuntu-latest since we have no way to check that 'on the fly'

pkg/runner/run_context_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,53 @@ func createJob(t *testing.T, input string, result string) *model.Job {
470470
return job
471471
}
472472

473+
func TestRunContextRunsOnPlatformNames(t *testing.T) {
474+
log.SetLevel(log.DebugLevel)
475+
assertObject := assert.New(t)
476+
477+
rc := createIfTestRunContext(map[string]*model.Job{
478+
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
479+
})
480+
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
481+
482+
rc = createIfTestRunContext(map[string]*model.Job{
483+
"job1": createJob(t, `runs-on: ${{ 'ubuntu-latest' }}`, ""),
484+
})
485+
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
486+
487+
rc = createIfTestRunContext(map[string]*model.Job{
488+
"job1": createJob(t, `runs-on: [self-hosted, my-runner]`, ""),
489+
})
490+
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(context.Background()))
491+
492+
rc = createIfTestRunContext(map[string]*model.Job{
493+
"job1": createJob(t, `runs-on: [self-hosted, "${{ 'my-runner' }}"]`, ""),
494+
})
495+
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(context.Background()))
496+
497+
rc = createIfTestRunContext(map[string]*model.Job{
498+
"job1": createJob(t, `runs-on: ${{ fromJSON('["ubuntu-latest"]') }}`, ""),
499+
})
500+
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
501+
502+
// test missing / invalid runs-on
503+
rc = createIfTestRunContext(map[string]*model.Job{
504+
"job1": createJob(t, `name: something`, ""),
505+
})
506+
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
507+
508+
rc = createIfTestRunContext(map[string]*model.Job{
509+
"job1": createJob(t, `runs-on:
510+
mapping: value`, ""),
511+
})
512+
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
513+
514+
rc = createIfTestRunContext(map[string]*model.Job{
515+
"job1": createJob(t, `runs-on: ${{ invalid expression }}`, ""),
516+
})
517+
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
518+
}
519+
473520
func TestRunContextIsEnabled(t *testing.T) {
474521
log.SetLevel(log.DebugLevel)
475522
assertObject := assert.New(t)

0 commit comments

Comments
 (0)