Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple when conditions #1087

Merged
merged 8 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 42 additions & 29 deletions docs/docs/20-usage/20-pipeline-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,20 @@ For more details check the [secrets docs](/docs/usage/secrets/).

### `when` - Conditional Execution

Woodpecker supports defining conditions for pipeline step by a `when` block. If all conditions in the `when` block evaluate to true the step is executed, otherwise it is skipped.
Woodpecker supports defining a list of conditions for a pipeline step by using a `when` block. If at least one of the conditions in the `when` block evaluate to true the step is executed, otherwise it is skipped. A condition can be a check like:

```diff
pipeline:
slack:
image: plugins/slack
settings:
channel: dev
+ when:
+ - event: pull_request
+ repo: test/test
+ - event: push
+ branch: main
```

#### `repo`

Expand All @@ -277,7 +290,7 @@ Example conditional execution by repository:
settings:
channel: dev
+ when:
+ repo: test/test
+ - repo: test/test
```

#### `branch`
Expand All @@ -295,7 +308,7 @@ pipeline:
settings:
channel: dev
+ when:
+ branch: master
+ - branch: master
```

> The step now triggers on master, but also if the target branch of a pull request is `master`. Add an event condition to limit it further to pushes on master only.
Expand All @@ -304,23 +317,23 @@ Execute a step if the branch is `master` or `develop`:

```diff
when:
branch: [master, develop]
- branch: [master, develop]
```

Execute a step if the branch starts with `prefix/*`:

```diff
when:
branch: prefix/*
- branch: prefix/*
```

Execute a step using custom include and exclude logic:

```diff
when:
branch:
include: [ master, release/* ]
exclude: [ release/1.0.0, release/1.1.* ]
- branch:
include: [ master, release/* ]
exclude: [ release/1.0.0, release/1.1.* ]
```

#### `event`
Expand All @@ -329,29 +342,29 @@ Execute a step if the build event is a `tag`:

```diff
when:
event: tag
- event: tag
```

Execute a step if the pipeline event is a `push` to a specified branch:

```diff
when:
event: push
+ branch: main
- event: push
+ branch: main
```

Execute a step for all non-pull request events:

```diff
when:
event: [push, tag, deployment]
- event: [push, tag, deployment]
```

Execute a step for all build events:

```diff
when:
event: [push, pull_request, tag, deployment]
- event: [push, pull_request, tag, deployment]
```

#### `tag`
Expand All @@ -361,8 +374,8 @@ Use glob expression to execute a step if the tag name starts with `v`:

```diff
when:
event: tag
tag: v*
- event: tag
tag: v*
```

#### `status`
Expand All @@ -376,7 +389,7 @@ pipeline:
settings:
channel: dev
+ when:
+ status: [ success, failure ]
+ - status: [ success, failure ]
```

#### `platform`
Expand All @@ -389,14 +402,14 @@ Execute a step for a specific platform:

```diff
when:
platform: linux/amd64
- platform: linux/amd64
```

Execute a step for a specific platform using wildcards:

```diff
when:
platform: [ linux/*, windows/amd64 ]
- platform: [ linux/*, windows/amd64 ]
```

#### `environment`
Expand All @@ -405,8 +418,8 @@ Execute a step for deployment events matching the target deployment environment:

```diff
when:
environment: production
event: deployment
- environment: production
- event: deployment
```

#### `matrix`
Expand All @@ -415,9 +428,9 @@ Execute a step for a single matrix permutation:

```diff
when:
matrix:
GO_VERSION: 1.5
REDIS_VERSION: 2.8
- matrix:
GO_VERSION: 1.5
REDIS_VERSION: 2.8
```

#### `instance`
Expand All @@ -426,7 +439,7 @@ Execute a step only on a certain Woodpecker instance matching the specified host

```diff
when:
instance: stage.woodpecker.company.com
- instance: stage.woodpecker.company.com
```

#### `path`
Expand All @@ -441,17 +454,17 @@ Execute a step only on a pipeline with certain files being changed:

```diff
when:
path: "src/*"
- path: "src/*"
```

You can use [glob patterns](https://github.com/bmatcuk/doublestar#patterns) to match the changed files and specify if the step should run if a file matching that pattern has been changed `include` or if some files have **not** been changed `exclude`.

```diff
when:
path:
include: [ '.woodpecker/*.yml', '*.ini' ]
exclude: [ '*.md', 'docs/**' ]
ignore_message: "[ALL]"
- path:
include: [ '.woodpecker/*.yml', '*.ini' ]
exclude: [ '*.md', 'docs/**' ]
ignore_message: "[ALL]"
```

**Hint:** Passing a defined ignore-message like `[ALL]` inside the commit message will ignore all path conditions.
Expand Down
8 changes: 4 additions & 4 deletions pipeline/frontend/yaml/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config {
config.Stages = append(config.Stages, stage)
} else if !c.local && !conf.SkipClone {
for i, container := range conf.Clone.Containers {
if !container.Constraints.Match(c.metadata) {
if !container.When.Match(c.metadata) {
continue
}
stage := new(backend.Stage)
Expand All @@ -172,7 +172,7 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config {
stage.Alias = nameServices

for i, container := range conf.Services.Containers {
if !container.Constraints.Match(c.metadata) {
if !container.When.Match(c.metadata) {
continue
}

Expand All @@ -188,11 +188,11 @@ func (c *Compiler) Compile(conf *yaml.Config) *backend.Config {
var group string
for i, container := range conf.Pipeline.Containers {
// Skip if local and should not run local
if c.local && !container.Constraints.Local.Bool() {
if c.local && !container.When.IsLocal() {
continue
}

if !container.Constraints.Match(c.metadata) {
if !container.When.Match(c.metadata) {
continue
}

Expand Down
16 changes: 10 additions & 6 deletions pipeline/frontend/yaml/compiler/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
cpuSet = c.reslimit.CPUSet
}

// all constraints must exclude success.
onSuccess := container.When.IsEmpty() ||
!container.When.ExcludesStatus("success")
// at least one constraint must include the status failure.
onFailure := container.When.IncludesStatus("failure")

return &backend.Step{
Name: name,
Alias: container.Name,
Expand Down Expand Up @@ -172,11 +178,9 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
CPUShares: cpuShares,
CPUSet: cpuSet,
AuthConfig: authConfig,
OnSuccess: container.Constraints.Status.Match("success"),
OnFailure: (len(container.Constraints.Status.Include)+
len(container.Constraints.Status.Exclude) != 0) &&
container.Constraints.Status.Match("failure"),
NetworkMode: networkMode,
IpcMode: ipcMode,
OnSuccess: onSuccess,
OnFailure: onFailure,
NetworkMode: networkMode,
IpcMode: ipcMode,
}
}
4 changes: 2 additions & 2 deletions pipeline/frontend/yaml/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func TestParse(t *testing.T) {
}
g.Assert(out.Pipeline.Containers[0].Name).Equal("notify_fail")
g.Assert(out.Pipeline.Containers[0].Image).Equal("plugins/slack")
g.Assert(len(out.Pipeline.Containers[0].Constraints.Event.Include)).Equal(0)
g.Assert(len(out.Pipeline.Containers[0].When.Constraints)).Equal(0)
g.Assert(out.Pipeline.Containers[1].Name).Equal("notify_success")
g.Assert(out.Pipeline.Containers[1].Image).Equal("plugins/slack")
g.Assert(out.Pipeline.Containers[1].Constraints.Event.Include).Equal([]string{"success"})
g.Assert(out.Pipeline.Containers[1].When.Constraints[0].Event.Include).Equal([]string{"success"})
})
})
})
Expand Down
Loading