diff --git a/pipeline.go b/pipeline.go index 8919210..b5697dc 100644 --- a/pipeline.go +++ b/pipeline.go @@ -197,12 +197,23 @@ func lowerStep(step Step, context *Context, stepContext *StepContext) (Step, err return nil, err } + name, ok := step["name"].(string) + if !ok { + name = "" + } + step["name"] = strings.TrimSpace(fmt.Sprintf(":%s: %s", stepContext.EmojiName, name)) + + // Block and wait steps must not contains agents or env, so we return early here + _, isBlockStep := step["block"] + _, isWaitStep := step["wait"] + if isBlockStep || isWaitStep { + return step, nil + } + agents, ok := step["agents"].(map[interface{}]interface{}) if !ok { agents = make(map[interface{}]interface{}) - if _, exists := step["wait"]; !exists { - step["agents"] = agents - } + step["agents"] = agents } agents["queue"] = stepContext.QueueName agents["environment"] = stepContext.EnvironmentName @@ -210,22 +221,15 @@ func lowerStep(step Step, context *Context, stepContext *StepContext) (Step, err env, ok := step["env"].(map[interface{}]interface{}) if !ok { env = make(map[interface{}]interface{}) - if _, exists := step["wait"]; !exists { - step["env"] = env - } + step["env"] = env } + env["JOBSWORTH_CAUTIOUS"] = stepContext.CautiousStr() env["JOBSWORTH_CODEBASE"] = context.CodebaseName() env["JOBSWORTH_CODE_VERSION"] = context.CodeVersion env["JOBSWORTH_SOURCE_GIT_COMMIT_ID"] = context.SourceGitCommitId env["JOBSWORTH_ENVIRONMENT"] = stepContext.EnvironmentName - name, ok := step["name"].(string) - if !ok { - name = "" - } - step["name"] = strings.TrimSpace(fmt.Sprintf(":%s: %s", stepContext.EmojiName, name)) - if step["command"] != nil && stepContext.PreventConcurrency && step["concurrency"] == nil && step["concurrency_group"] == nil { step["concurrency_group"] = fmt.Sprintf("%s/%s", stepContext.EnvironmentName, context.BuildkitePipelineSlug) diff --git a/pipeline_test.go b/pipeline_test.go index e2ae16d..d59df45 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -67,6 +67,29 @@ wait: ~ } } +func TestBlockStep(t *testing.T) { + context := &Context{} + stepContext := &StepContext{} + + step := Step{} + stepBytes := []byte(` +block: ~ +name: "my block" +if: 1 == 1 +`) + yaml.Unmarshal(stepBytes, &step) + step, err := lowerStep(step, context, stepContext) + if err != nil { + t.Error("lowerStep returned err:", err) + } + if !reflect.DeepEqual(step["env"], nil) { + t.Errorf("env should not be set for a block step") + } + if !reflect.DeepEqual(step["agents"], nil) { + t.Errorf("agents should not be set for a block step") + } +} + func TestInterpolate(t *testing.T) { context := &Context{} stepContext := &StepContext{