Skip to content

Commit e5637db

Browse files
authored
Merge pull request #92 from jasonwbarnett/jwb/add-if-support
feat: Add support for conditionals (if key) in Step struct
2 parents 2b90950 + 21d5c66 commit e5637db

File tree

5 files changed

+110
-12
lines changed

5 files changed

+110
-12
lines changed

README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ For example, in the following configuration:
4545
steps:
4646
- label: "Triggering pipelines"
4747
plugins:
48-
- monorepo-diff#v1.4.0:
48+
- monorepo-diff#v1.5.0:
4949
diff: "git diff --name-only HEAD~1"
5050
watch:
5151
- path: "**/*"
@@ -70,14 +70,15 @@ This is a sub-section that provides configuration for running commands or trigge
7070
- [Trigger](https://buildkite.com/docs/pipelines/configure/step-types/trigger-step)
7171
- [Command](https://buildkite.com/docs/pipelines/configure/step-types/command-step)
7272
- [Group](https://buildkite.com/docs/pipelines/configure/step-types/group-step)
73+
- [Conditionals](https://buildkite.com/docs/pipelines/conditionals)
7374

7475
:warning: This plugin may accept configurations that are not valid pipeline steps, this is a known issue to keep its code simple and flexible.
7576

7677
```yaml
7778
steps:
7879
- label: "Triggering pipelines"
7980
plugins:
80-
- monorepo-diff#v1.4.0:
81+
- monorepo-diff#v1.5.0:
8182
watch:
8283
- path: app/
8384
config:
@@ -108,7 +109,7 @@ steps:
108109
steps:
109110
- label: "Triggering pipelines with plugin"
110111
plugins:
111-
- monorepo-diff#v1.4.0:
112+
- monorepo-diff#v1.5.0:
112113
watch:
113114
- path: test/.buildkite/
114115
config: # Required [trigger step configuration]
@@ -127,6 +128,34 @@ steps:
127128
- When changes are detected in the path `test/.buildkite/` it triggers the pipeline `test-pipeline`
128129
- If the changes are made to either `app/` or `app/bin/service/` it triggers the pipeline `data-generator`
129130

131+
**Conditional Step Execution (`if`):**
132+
133+
The plugin supports conditional execution of pipeline steps using the `if` key, matching Buildkite’s pipeline conditional syntax. The `if` key allows you to control when a step runs, based on branch names, environment variables, build metadata, or custom expressions.
134+
135+
**Example**
136+
137+
```yaml
138+
steps:
139+
- label: "Triggering pipelines with plugin"
140+
plugins:
141+
- monorepo-diff#v1.5.0:
142+
diff: git diff --name-only HEAD~1
143+
watch:
144+
- path: services/api
145+
config:
146+
trigger: deploy-api
147+
if: build.branch == 'main' || build.branch =~ /^release\//
148+
- path: services/web
149+
config:
150+
command: echo "Deploy Web"
151+
if: build.tag != null
152+
```
153+
154+
In the example above,
155+
- The `deploy-api` trigger will only run on the main branch or branches matching `release/*`.
156+
- The `web deployment` command will run only if the build has a tag.
157+
158+
130159
#### `diff` (optional)
131160

132161
This will run the script provided to determine the folder changes.
@@ -172,7 +201,7 @@ git diff --name-only "$LATEST_TAG"
172201
steps:
173202
- label: "Triggering pipelines"
174203
plugins:
175-
- monorepo-diff#v1.4.0:
204+
- monorepo-diff#v1.5.0:
176205
diff: "git diff --name-only HEAD~1"
177206
watch:
178207
- path: "bar-service/"
@@ -199,7 +228,7 @@ A default `config` to run if no paths are matched, the `config` key is not requi
199228
steps:
200229
- label: "Triggering pipelines"
201230
plugins:
202-
- monorepo-diff#v1.4.0:
231+
- monorepo-diff#v1.5.0:
203232
diff: "git diff --name-only HEAD~1"
204233
watch:
205234
- path: "bar-service/"
@@ -221,7 +250,7 @@ The object values provided in this configuration will be appended to `env` prope
221250
steps:
222251
- label: "Triggering pipelines"
223252
plugins:
224-
- monorepo-diff#v1.4.0:
253+
- monorepo-diff#v1.5.0:
225254
diff: "git diff --name-only HEAD~1"
226255
watch:
227256
- path: "foo-service/"
@@ -243,7 +272,7 @@ Add `log_level` property to set the log level. Supported log levels are `debug`
243272
steps:
244273
- label: "Triggering pipelines"
245274
plugins:
246-
- monorepo-diff#v1.4.0:
275+
- monorepo-diff#v1.5.0:
247276
diff: "git diff --name-only HEAD~1"
248277
log_level: "debug" # defaults to "info"
249278
watch:
@@ -274,7 +303,7 @@ By setting `wait` to `true`, the build will wait until the triggered pipeline bu
274303
steps:
275304
- label: "Triggering pipelines"
276305
plugins:
277-
- monorepo-diff#v1.4.0:
306+
- monorepo-diff#v1.5.0:
278307
diff: "git diff --name-only $(head -n 1 last_successful_build)"
279308
interpolation: false
280309
env:

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
2-
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
3-
github.com/bmatcuk/doublestar/v4 v4.9.0 h1:DBvuZxjdKkRP/dr4GVV4w2fnmrk5Hxc90T51LZjv0JA=
4-
github.com/bmatcuk/doublestar/v4 v4.9.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
51
github.com/bmatcuk/doublestar/v4 v4.9.1 h1:X8jg9rRZmJd4yRy7ZeNDRnM+T3ZfHv15JiBJ/avrEXE=
62
github.com/bmatcuk/doublestar/v4 v4.9.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
73
github.com/buildkite/bintest/v3 v3.3.0 h1:RTWcSaJRlOT6t/K311ejPf+0J3LE/QEODzVG3vlLnWo=

pipeline_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,36 @@ func TestGeneratePipelineWithNoStepsAndNoHooks(t *testing.T) {
547547

548548
assert.Equal(t, want, string(got))
549549
}
550+
551+
func TestGeneratePipelineWithCondition(t *testing.T) {
552+
steps := []Step{
553+
{
554+
Command: "echo deploy to production",
555+
Label: "Deploy",
556+
Condition: "build.branch == 'main' && build.pull_request.id == null",
557+
},
558+
{
559+
Trigger: "test-pipeline",
560+
Condition: "build.message =~ /\\[deploy\\]/",
561+
},
562+
}
563+
564+
want := `steps:
565+
- label: Deploy
566+
if: build.branch == 'main' && build.pull_request.id == null
567+
command: echo deploy to production
568+
- trigger: test-pipeline
569+
if: build.message =~ /\[deploy\]/
570+
`
571+
572+
plugin := Plugin{Wait: false}
573+
574+
pipeline, _, err := generatePipeline(steps, plugin)
575+
require.NoError(t, err)
576+
defer os.Remove(pipeline.Name())
577+
578+
got, err := os.ReadFile(pipeline.Name())
579+
require.NoError(t, err)
580+
581+
assert.Equal(t, want, string(got))
582+
}

plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type Step struct {
8080
Trigger string `yaml:"trigger,omitempty"`
8181
Label string `yaml:"label,omitempty"`
8282
Branches string `yaml:"branches,omitempty"`
83+
Condition string `json:"if,omitempty" yaml:"if,omitempty"`
8384
Build Build `yaml:"build,omitempty"`
8485
Command interface{} `yaml:"command,omitempty"`
8586
Commands interface{} `yaml:"commands,omitempty"`

plugin_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,3 +824,42 @@ func TestPluginLevelMetadataNotAppliedToCommandSteps(t *testing.T) {
824824
assert.Equal(t, "echo Make Changes to Bin", commandStep.Command)
825825
assert.Nil(t, commandStep.Build.Metadata, "Command step should not have metadata applied")
826826
}
827+
828+
func TestPluginShouldPreserveStepCondition(t *testing.T) {
829+
param := `[{
830+
"github.com/buildkite-plugins/monorepo-diff-buildkite-plugin#commit": {
831+
"watch": [
832+
{
833+
"path": "service/**/*",
834+
"config": {
835+
"command": "echo deploy",
836+
"if": "build.branch == 'main' && build.pull_request.id == null"
837+
}
838+
}
839+
]
840+
}
841+
}]`
842+
843+
got, err := initializePlugin(param)
844+
assert.NoError(t, err)
845+
846+
expected := Plugin{
847+
Diff: "git diff --name-only HEAD~1",
848+
Wait: false,
849+
LogLevel: "info",
850+
Interpolation: true,
851+
Watch: []WatchConfig{
852+
{
853+
Paths: []string{"service/**/*"},
854+
Step: Step{
855+
Command: "echo deploy",
856+
Condition: "build.branch == 'main' && build.pull_request.id == null",
857+
},
858+
},
859+
},
860+
}
861+
862+
if diff := cmp.Diff(expected, got); diff != "" {
863+
t.Fatalf("plugin diff (-want +got):\n%s", diff)
864+
}
865+
}

0 commit comments

Comments
 (0)