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>
This commit is contained in:
parent
1c16fd1967
commit
610358e1c3
3 changed files with 67 additions and 18 deletions
|
@ -15,7 +15,7 @@ func (i *Input) newPlatforms() map[string]string {
|
||||||
for _, p := range i.platforms {
|
for _, p := range i.platforms {
|
||||||
pParts := strings.Split(p, "=")
|
pParts := strings.Split(p, "=")
|
||||||
if len(pParts) == 2 {
|
if len(pParts) == 2 {
|
||||||
platforms[pParts[0]] = pParts[1]
|
platforms[strings.ToLower(pParts[0])] = pParts[1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return platforms
|
return platforms
|
||||||
|
|
|
@ -626,14 +626,7 @@ func (rc *RunContext) containerImage(ctx context.Context) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *RunContext) runsOnImage(ctx context.Context) string {
|
func (rc *RunContext) runsOnImage(ctx context.Context) string {
|
||||||
job := rc.Run.Job()
|
for _, platformName := range rc.runsOnPlatformNames(ctx) {
|
||||||
|
|
||||||
if job.RunsOn() == nil {
|
|
||||||
common.Logger(ctx).Errorf("'runs-on' key not defined in %s", rc.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, runnerLabel := range job.RunsOn() {
|
|
||||||
platformName := rc.ExprEval.Interpolate(ctx, runnerLabel)
|
|
||||||
image := rc.Config.Platforms[strings.ToLower(platformName)]
|
image := rc.Config.Platforms[strings.ToLower(platformName)]
|
||||||
if image != "" {
|
if image != "" {
|
||||||
return image
|
return image
|
||||||
|
@ -643,6 +636,22 @@ func (rc *RunContext) runsOnImage(ctx context.Context) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *RunContext) runsOnPlatformNames(ctx context.Context) []string {
|
||||||
|
job := rc.Run.Job()
|
||||||
|
|
||||||
|
if job.RunsOn() == nil {
|
||||||
|
common.Logger(ctx).Errorf("'runs-on' key not defined in %s", rc.String())
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rc.ExprEval.EvaluateYamlNode(ctx, &job.RawRunsOn); err != nil {
|
||||||
|
common.Logger(ctx).Errorf("Error while evaluating runs-on: %v", err)
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return job.RunsOn()
|
||||||
|
}
|
||||||
|
|
||||||
func (rc *RunContext) platformImage(ctx context.Context) string {
|
func (rc *RunContext) platformImage(ctx context.Context) string {
|
||||||
if containerImage := rc.containerImage(ctx); containerImage != "" {
|
if containerImage := rc.containerImage(ctx); containerImage != "" {
|
||||||
return containerImage
|
return containerImage
|
||||||
|
@ -684,12 +693,7 @@ func (rc *RunContext) isEnabled(ctx context.Context) (bool, error) {
|
||||||
|
|
||||||
img := rc.platformImage(ctx)
|
img := rc.platformImage(ctx)
|
||||||
if img == "" {
|
if img == "" {
|
||||||
if job.RunsOn() == nil {
|
for _, platformName := range rc.runsOnPlatformNames(ctx) {
|
||||||
l.Errorf("'runs-on' key not defined in %s", rc.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, runnerLabel := range job.RunsOn() {
|
|
||||||
platformName := rc.ExprEval.Interpolate(ctx, runnerLabel)
|
|
||||||
l.Infof("\U0001F6A7 Skipping unsupported platform -- Try running with `-P %+v=...`", platformName)
|
l.Infof("\U0001F6A7 Skipping unsupported platform -- Try running with `-P %+v=...`", platformName)
|
||||||
}
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
|
@ -923,9 +927,7 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon
|
||||||
setActionRuntimeVars(rc, env)
|
setActionRuntimeVars(rc, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
job := rc.Run.Job()
|
for _, platformName := range rc.runsOnPlatformNames(ctx) {
|
||||||
for _, runnerLabel := range job.RunsOn() {
|
|
||||||
platformName := rc.ExprEval.Interpolate(ctx, runnerLabel)
|
|
||||||
if platformName != "" {
|
if platformName != "" {
|
||||||
if platformName == "ubuntu-latest" {
|
if platformName == "ubuntu-latest" {
|
||||||
// hardcode current ubuntu-latest since we have no way to check that 'on the fly'
|
// hardcode current ubuntu-latest since we have no way to check that 'on the fly'
|
||||||
|
|
|
@ -470,6 +470,53 @@ func createJob(t *testing.T, input string, result string) *model.Job {
|
||||||
return job
|
return job
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunContextRunsOnPlatformNames(t *testing.T) {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
assertObject := assert.New(t)
|
||||||
|
|
||||||
|
rc := createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
|
||||||
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ${{ 'ubuntu-latest' }}`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
|
||||||
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: [self-hosted, my-runner]`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
|
||||||
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: [self-hosted, "${{ 'my-runner' }}"]`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{"self-hosted", "my-runner"}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
|
||||||
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ${{ fromJSON('["ubuntu-latest"]') }}`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{"ubuntu-latest"}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
|
||||||
|
// test missing / invalid runs-on
|
||||||
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `name: something`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
|
||||||
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on:
|
||||||
|
mapping: value`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
|
||||||
|
rc = createIfTestRunContext(map[string]*model.Job{
|
||||||
|
"job1": createJob(t, `runs-on: ${{ invalid expression }}`, ""),
|
||||||
|
})
|
||||||
|
assertObject.Equal([]string{}, rc.runsOnPlatformNames(context.Background()))
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunContextIsEnabled(t *testing.T) {
|
func TestRunContextIsEnabled(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
assertObject := assert.New(t)
|
assertObject := assert.New(t)
|
||||||
|
|
Loading…
Add table
Reference in a new issue