From 51b616760601375842bc28a8e54cef26807631de Mon Sep 17 00:00:00 2001 From: Casey Lee Date: Mon, 24 Feb 2020 22:35:08 -0800 Subject: [PATCH] fix 99: support string and map variants of container --- pkg/model/workflow.go | 22 ++++++++++++++++- pkg/model/workflow_test.go | 28 ++++++++++++++++++++++ pkg/runner/run_context.go | 5 ++-- pkg/runner/step_context.go | 5 ++-- pkg/runner/testdata/job-container/push.yml | 8 +++++++ 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index 33cf6e7..7241426 100644 --- a/pkg/model/workflow.go +++ b/pkg/model/workflow.go @@ -61,9 +61,9 @@ type Job struct { If string `yaml:"if"` Steps []*Step `yaml:"steps"` TimeoutMinutes int64 `yaml:"timeout-minutes"` - Container *ContainerSpec `yaml:"container"` Services map[string]*ContainerSpec `yaml:"services"` Strategy *Strategy `yaml:"strategy"` + RawContainer yaml.Node `yaml:"container"` } // Strategy for the job @@ -73,6 +73,26 @@ type Strategy struct { Matrix map[string][]interface{} `yaml:"matrix"` } +// Container details for the job +func (j *Job) Container() *ContainerSpec { + var val *ContainerSpec + switch j.RawContainer.Kind { + case yaml.ScalarNode: + val = new(ContainerSpec) + err := j.RawContainer.Decode(&val.Image) + if err != nil { + log.Fatal(err) + } + case yaml.MappingNode: + val = new(ContainerSpec) + err := j.RawContainer.Decode(val) + if err != nil { + log.Fatal(err) + } + } + return val +} + // Needs list for Job func (j *Job) Needs() []string { diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go index 42a05ee..92df87d 100644 --- a/pkg/model/workflow_test.go +++ b/pkg/model/workflow_test.go @@ -70,3 +70,31 @@ jobs: assert.Contains(t, workflow.On(), "push") assert.Contains(t, workflow.On(), "pull_request") } + +func TestReadWorkflow_StringContainer(t *testing.T) { + yaml := ` +name: local-action-docker-url + +jobs: + test: + container: nginx:latest + runs-on: ubuntu-latest + steps: + - uses: ./actions/docker-url + test2: + container: + image: nginx:latest + env: + foo: bar + runs-on: ubuntu-latest + steps: + - uses: ./actions/docker-url +` + + workflow, err := ReadWorkflow(strings.NewReader(yaml)) + assert.NoError(t, err, "read workflow should succeed") + assert.Len(t, workflow.Jobs, 2) + assert.Contains(t, workflow.Jobs["test"].Container().Image, "nginx:latest") + assert.Contains(t, workflow.Jobs["test2"].Container().Image, "nginx:latest") + assert.Contains(t, workflow.Jobs["test2"].Container().Env["foo"], "bar") +} diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index 09d3ed2..d02117b 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -53,8 +53,9 @@ func (rc *RunContext) startJobContainer() common.Executor { job := rc.Run.Job() var image string - if job.Container != nil { - image = job.Container.Image + c := job.Container() + if c != nil { + image = c.Image } else { platformName := rc.ExprEval.Interpolate(job.RunsOn) image = rc.Config.Platforms[strings.ToLower(platformName)] diff --git a/pkg/runner/step_context.go b/pkg/runner/step_context.go index cece632..60beb64 100644 --- a/pkg/runner/step_context.go +++ b/pkg/runner/step_context.go @@ -87,8 +87,9 @@ func (sc *StepContext) setupEnv() common.Executor { step := sc.Step return func(ctx context.Context) error { var env map[string]string - if job.Container != nil { - env = mergeMaps(rc.GetEnv(), job.Container.Env, step.GetEnv()) + c := job.Container() + if c != nil { + env = mergeMaps(rc.GetEnv(), c.Env, step.GetEnv()) } else { env = mergeMaps(rc.GetEnv(), step.GetEnv()) } diff --git a/pkg/runner/testdata/job-container/push.yml b/pkg/runner/testdata/job-container/push.yml index fb61f20..f4eab3d 100644 --- a/pkg/runner/testdata/job-container/push.yml +++ b/pkg/runner/testdata/job-container/push.yml @@ -10,3 +10,11 @@ jobs: TEST_ENV: test-value steps: - run: echo ${TEST_ENV} | grep test-value + + test2: + runs-on: ubuntu-latest + container: node:10.16-jessie + steps: + - run: echo ${TEST_ENV} | grep test-value + env: + TEST_ENV: test-value