Skip to content

Commit

Permalink
Allow block step (#9)
Browse files Browse the repository at this point in the history
* Allow block step

* add test

* pr comments
  • Loading branch information
gmenih authored Oct 23, 2023
1 parent dcdda65 commit 35a2785
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,35 +197,39 @@ 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

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)
Expand Down
23 changes: 23 additions & 0 deletions pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 35a2785

Please sign in to comment.