From 21e2bb86570258f13e07952b0088da26a8c7a1a9 Mon Sep 17 00:00:00 2001 From: Casey Lee Date: Wed, 26 Feb 2020 23:29:43 -0800 Subject: [PATCH] fix #108 - support matrix expressions in job name (#109) --- pkg/model/planner.go | 3 +-- pkg/runner/expression.go | 2 +- pkg/runner/run_context.go | 17 +++++++++++++++-- pkg/runner/runner.go | 26 ++++++++++++++------------ pkg/runner/runner_test.go | 2 ++ pkg/runner/testdata/matrix/push.yml | 1 + 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/pkg/model/planner.go b/pkg/model/planner.go index 0519765..ad4d43e 100644 --- a/pkg/model/planner.go +++ b/pkg/model/planner.go @@ -1,7 +1,6 @@ package model import ( - "fmt" "io/ioutil" "math" "os" @@ -39,7 +38,7 @@ func (r *Run) String() string { if jobName == "" { jobName = r.JobID } - return fmt.Sprintf("%s/%s", r.Workflow.Name, jobName) + return jobName } // Job returns the job for this Run diff --git a/pkg/runner/expression.go b/pkg/runner/expression.go index 652800e..e7b533b 100644 --- a/pkg/runner/expression.go +++ b/pkg/runner/expression.go @@ -272,7 +272,7 @@ func (rc *RunContext) vmRunner() func(*otto.Otto) { runner := map[string]interface{}{ "os": "Linux", "temp": "/tmp", - "tool_cache": "/tmp", + "tool_cache": "/opt/hostedtoolcache", } return func(vm *otto.Otto) { diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index c8dbffa..21c0eba 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -20,6 +20,7 @@ import ( // RunContext contains info about current job type RunContext struct { + Name string Config *Config Matrix map[string]interface{} Run *model.Run @@ -32,6 +33,10 @@ type RunContext struct { JobContainer container.Container } +func (rc *RunContext) String() string { + return fmt.Sprintf("%s/%s", rc.Run.Workflow.Name, rc.Name) +} + type stepResult struct { Success bool `json:"success"` Outputs map[string]string `json:"outputs"` @@ -46,7 +51,7 @@ func (rc *RunContext) GetEnv() map[string]string { } func (rc *RunContext) jobContainerName() string { - return createContainerName("act", rc.Run.String()) + return createContainerName("act", rc.String()) } func (rc *RunContext) startJobContainer() common.Executor { @@ -156,6 +161,14 @@ func (rc *RunContext) ActionCacheDir() string { // Executor returns a pipeline executor for all the steps in the job func (rc *RunContext) Executor() common.Executor { steps := make([]common.Executor, 0) + + steps = append(steps, func(ctx context.Context) error { + if len(rc.Matrix) > 0 { + common.Logger(ctx).Infof("\U0001F9EA Matrix: %v", rc.Matrix) + } + return nil + }) + steps = append(steps, rc.startJobContainer()) for i, step := range rc.Run.Job().Steps { @@ -209,7 +222,7 @@ func (rc *RunContext) isEnabled(ctx context.Context) bool { platformName := rc.ExprEval.Interpolate(rc.Run.Job().RunsOn) if img, ok := rc.Config.Platforms[strings.ToLower(platformName)]; !ok || img == "" { - log.Infof(" \U0001F6A7 Skipping unsupported platform '%s'", platformName) + log.Infof("\U0001F6A7 Skipping unsupported platform '%s'", platformName) return false } return true diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 3befe0c..b045ade 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -52,7 +52,7 @@ func New(runnerConfig *Config) (Runner, error) { } func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor { - maxJobNameLen := plan.MaxRunNameLen() + maxJobNameLen := 0 pipeline := make([]common.Executor, 0) for _, stage := range plan.Stages { @@ -61,16 +61,17 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor { job := run.Job() matrixes := job.GetMatrixes() - jobName := fmt.Sprintf("%-*s", maxJobNameLen, run.String()) - for _, matrix := range matrixes { - m := matrix - runExecutor := runner.newRunExecutor(run, matrix) + for i, matrix := range matrixes { + rc := runner.newRunContext(run, matrix) + if len(matrix) > 1 { + rc.Name = fmt.Sprintf("%s-%d", rc.Name, i+1) + } + if len(rc.String()) > maxJobNameLen { + maxJobNameLen = len(rc.String()) + } stageExecutor = append(stageExecutor, func(ctx context.Context) error { - ctx = WithJobLogger(ctx, jobName) - if len(m) > 0 { - common.Logger(ctx).Infof("\U0001F9EA Matrix: %v", m) - } - return runExecutor(ctx) + jobName := fmt.Sprintf("%-*s", maxJobNameLen, rc.String()) + return rc.Executor()(WithJobLogger(ctx, jobName)) }) } } @@ -80,7 +81,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor { return common.NewPipelineExecutor(pipeline...) } -func (runner *runnerImpl) newRunExecutor(run *model.Run, matrix map[string]interface{}) common.Executor { +func (runner *runnerImpl) newRunContext(run *model.Run, matrix map[string]interface{}) *RunContext { rc := &RunContext{ Config: runner.config, Run: run, @@ -89,5 +90,6 @@ func (runner *runnerImpl) newRunExecutor(run *model.Run, matrix map[string]inter Matrix: matrix, } rc.ExprEval = rc.NewExpressionEvaluator() - return rc.Executor() + rc.Name = rc.ExprEval.Interpolate(run.String()) + return rc } diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 3be32ae..60aa98c 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -49,6 +49,8 @@ func TestRunEvent(t *testing.T) { {"remote-action-js", "push", ""}, {"local-action-docker-url", "push", ""}, {"local-action-dockerfile", "push", ""}, + {"matrix", "push", ""}, + {"commands", "push", ""}, } log.SetLevel(log.DebugLevel) diff --git a/pkg/runner/testdata/matrix/push.yml b/pkg/runner/testdata/matrix/push.yml index c1dc793..549c85b 100644 --- a/pkg/runner/testdata/matrix/push.yml +++ b/pkg/runner/testdata/matrix/push.yml @@ -3,6 +3,7 @@ on: push jobs: build: + name: PHP ${{ matrix.os }} ${{ matrix.node}} runs-on: ${{ matrix.os }} steps: - run: echo ${NODE_VERSION} | grep ${{ matrix.node }}