Skip to content

Commit 6dd7b39

Browse files
qwerty287anbraten
authored andcommitted
Deprecate environment filter and improve errors (woodpecker-ci#3634)
Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com>
1 parent a029a14 commit 6dd7b39

File tree

11 files changed

+120
-25
lines changed

11 files changed

+120
-25
lines changed

docs/docs/20-usage/20-workflow-syntax.md

+1-15
Original file line numberDiff line numberDiff line change
@@ -359,20 +359,6 @@ when:
359359
- platform: [linux/*, windows/amd64]
360360
```
361361

362-
<!-- markdownlint-disable no-duplicate-heading -->
363-
364-
#### `environment`
365-
366-
<!-- markdownlint-enable no-duplicate-heading -->
367-
368-
Execute a step for deployment events matching the target deployment environment:
369-
370-
```yaml
371-
when:
372-
- environment: production
373-
- event: deployment
374-
```
375-
376362
#### `matrix`
377363

378364
Execute a step for a single matrix permutation:
@@ -758,7 +744,7 @@ Workflows that should run even on failure should set the `runs_on` tag. See [her
758744
Woodpecker gives the ability to configure privileged mode in the YAML. You can use this parameter to launch containers with escalated capabilities.
759745

760746
:::info
761-
Privileged mode is only available to trusted repositories and for security reasons should only be used in private environments. See [project settings](./71-project-settings.md#trusted) to enable trusted mode.
747+
Privileged mode is only available to trusted repositories and for security reasons should only be used in private environments. See [project settings](./75-project-settings.md#trusted) to enable trusted mode.
762748
:::
763749

764750
```diff

docs/docs/20-usage/25-workflows.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ In case there is a single configuration in `.woodpecker.yaml` Woodpecker will cr
66

77
By placing the configurations in a folder which is by default named `.woodpecker/` Woodpecker will create a pipeline with multiple workflows each named by the file they are defined in. Only `.yml` and `.yaml` files will be used and files in any subfolders like `.woodpecker/sub-folder/test.yaml` will be ignored.
88

9-
You can also set some custom path like `.my-ci/pipelines/` instead of `.woodpecker/` in the [project settings](./71-project-settings.md).
9+
You can also set some custom path like `.my-ci/pipelines/` instead of `.woodpecker/` in the [project settings](./75-project-settings.md).
1010

1111
## Benefits of using workflows
1212

docs/docs/20-usage/70-volumes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Woodpecker gives the ability to define Docker volumes in the YAML. You can use this parameter to mount files or folders on the host machine into your containers.
44

55
:::note
6-
Volumes are only available to trusted repositories and for security reasons should only be used in private environments. See [project settings](./71-project-settings.md#trusted) to enable trusted mode.
6+
Volumes are only available to trusted repositories and for security reasons should only be used in private environments. See [project settings](./75-project-settings.md#trusted) to enable trusted mode.
77
:::
88

99
```diff

docs/docs/20-usage/72-linter.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Linter
2+
3+
Woodpecker automatically lints your workflow files for errors, deprecations and bad habits. Errors and warnings are shown in the UI for any pipelines.
4+
5+
![errors and warnings in UI](./linter-warnings-errors.png)
6+
7+
## Running the linter from CLI
8+
9+
You can run the linter also manually from the CLI:
10+
11+
```shell
12+
woodpecker-cli lint <workflow files>
13+
```
14+
15+
## Bad habit warnings
16+
17+
Woodpecker warns you if your configuration contains some bad habits.
18+
19+
### Event filter for all steps
20+
21+
All your items in `when` blocks should have an `event` filter, so no step runs on all events. This is recommended because if new events are added, your steps probably shouldn't run on those as well.
22+
23+
Examples of an **incorrect** config for this rule:
24+
25+
```yaml
26+
when:
27+
- branch: main
28+
- event: tag
29+
```
30+
31+
This will trigger the warning because the first item (`branch: main`) does not filter with an event.
32+
33+
```yaml
34+
steps:
35+
- name: test
36+
when:
37+
branch: main
38+
39+
- name: deploy
40+
when:
41+
event: tag
42+
```
43+
44+
Examples of a **correct** config for this rule:
45+
46+
```yaml
47+
when:
48+
- branch: main
49+
event: push
50+
- event: tag
51+
```
52+
53+
```yaml
54+
steps:
55+
- name: test
56+
when:
57+
event: [tag, push]
58+
59+
- name: deploy
60+
when:
61+
- event: tag
62+
```
113 KB
Loading

docs/docs/91-migrations.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Some versions need some changes to the server configuration or the pipeline conf
1111
- Deprecated uppercasing all secret env vars, instead, the value of the `secrets` property is used. [Read more](./20-usage/40-secrets.md#use-secrets-in-commands)
1212
- Deprecated alternative names for secrets, use `environment` with `from_secret`
1313
- Deprecated slice definition for env vars
14+
- Deprecated `environment` filter, use `when.evaluate`
1415

1516
## 2.0.0
1617

@@ -66,7 +67,7 @@ Some versions need some changes to the server configuration or the pipeline conf
6667

6768
Only projects created after updating will have an empty value by default. Existing projects will stick to the current pipeline path which is `.drone.yml` in most cases.
6869

69-
Read more about it at the [Project Settings](./20-usage/71-project-settings.md#pipeline-path)
70+
Read more about it at the [Project Settings](./20-usage/75-project-settings.md#pipeline-path)
7071

7172
- From version `0.15.0` ongoing there will be three types of docker images: `latest`, `next` and `x.x.x` with an alpine variant for each type like `latest-alpine`.
7273
If you used `latest` before to try pre-release features you should switch to `next` after this release.

pipeline/errors/error.go

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ type DeprecationErrorData struct {
1919
Docs string `json:"docs"`
2020
}
2121

22+
type BadHabitErrorData struct {
23+
File string `json:"file"`
24+
Field string `json:"field"`
25+
Docs string `json:"docs"`
26+
}
27+
2228
func GetLinterData(e *types.PipelineError) *LinterErrorData {
2329
if e.Type != types.PipelineErrorTypeLinter {
2430
return nil

pipeline/frontend/yaml/linter/linter.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,39 @@ func (l *Linter) lintDeprecations(config *WorkflowConfig) (err error) {
305305
Data: errors.DeprecationErrorData{
306306
File: config.File,
307307
Field: fmt.Sprintf("steps.%s.secrets[%d]", step.Name, i),
308-
Docs: "https://woodpecker-ci.org/docs/usage/workflow-syntax#event",
308+
Docs: "https://woodpecker-ci.org/docs/usage/secrets#use-secrets-in-settings-and-environment",
309+
},
310+
IsWarning: true,
311+
})
312+
}
313+
}
314+
}
315+
316+
for i, c := range parsed.When.Constraints {
317+
if !c.Environment.IsEmpty() {
318+
err = multierr.Append(err, &errorTypes.PipelineError{
319+
Type: errorTypes.PipelineErrorTypeDeprecation,
320+
Message: "environment filters are deprecated, use evaluate with CI_PIPELINE_DEPLOY_TARGET",
321+
Data: errors.DeprecationErrorData{
322+
File: config.File,
323+
Field: fmt.Sprintf("when[%d].environment", i),
324+
Docs: "https://woodpecker-ci.org/docs/usage/workflow-syntax#evaluate",
325+
},
326+
IsWarning: true,
327+
})
328+
}
329+
}
330+
331+
for _, step := range parsed.Steps.ContainerList {
332+
for i, c := range step.When.Constraints {
333+
if !c.Environment.IsEmpty() {
334+
err = multierr.Append(err, &errorTypes.PipelineError{
335+
Type: errorTypes.PipelineErrorTypeDeprecation,
336+
Message: "environment filters are deprecated, use evaluate with CI_PIPELINE_DEPLOY_TARGET",
337+
Data: errors.DeprecationErrorData{
338+
File: config.File,
339+
Field: fmt.Sprintf("steps.%s.when[%d].environment", step.Name, i),
340+
Docs: "https://woodpecker-ci.org/docs/usage/workflow-syntax#evaluate",
309341
},
310342
IsWarning: true,
311343
})
@@ -351,10 +383,11 @@ func (l *Linter) lintBadHabits(config *WorkflowConfig) (err error) {
351383
if field != "" {
352384
err = multierr.Append(err, &errorTypes.PipelineError{
353385
Type: errorTypes.PipelineErrorTypeBadHabit,
354-
Message: "Please set an event filter on all when branches",
355-
Data: errors.LinterErrorData{
386+
Message: "Please set an event filter for all steps or the whole workflow on all items of the when block",
387+
Data: errors.BadHabitErrorData{
356388
File: config.File,
357389
Field: field,
390+
Docs: "https://woodpecker-ci.org/docs/usage/linter#event-filter-for-all-steps",
358391
},
359392
IsWarning: true,
360393
})

pipeline/frontend/yaml/linter/linter_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ func TestBadHabits(t *testing.T) {
189189
}{
190190
{
191191
from: "steps: { build: { image: golang } }",
192-
want: "Please set an event filter on all when branches",
192+
want: "Please set an event filter for all steps or the whole workflow on all items of the when block",
193193
},
194194
{
195195
from: "when: [{branch: xyz}, {event: push}]\nsteps: { build: { image: golang } }",
196-
want: "Please set an event filter on all when branches",
196+
want: "Please set an event filter for all steps or the whole workflow on all items of the when block",
197197
},
198198
}
199199

web/src/views/repo/pipeline/PipelineErrors.vue

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
}"
1212
/>
1313
<span>[{{ error.type }}]</span>
14-
<span v-if="isLinterError(error) || isDeprecationError(error)" class="whitespace-nowrap">
14+
<span
15+
v-if="isLinterError(error) || isDeprecationError(error) || isBadHabitError(error)"
16+
class="whitespace-nowrap"
17+
>
1518
<span v-if="error.data?.file" class="font-bold">{{ error.data?.file }}: </span>
1619
<span>{{ error.data?.field }}</span>
1720
</span>
1821
<span v-else />
1922
<a
20-
v-if="isDeprecationError(error)"
23+
v-if="isDeprecationError(error) || isBadHabitError(error)"
2124
:href="error.data?.docs"
2225
target="_blank"
2326
class="underline col-span-full col-start-2 md:col-span-auto md:col-start-auto"
@@ -52,6 +55,10 @@ function isDeprecationError(
5255
): error is PipelineError<{ file: string; field: string; docs: string }> {
5356
return error.type === 'deprecation';
5457
}
58+
59+
function isBadHabitError(error: PipelineError): error is PipelineError<{ file?: string; field: string; docs: string }> {
60+
return error.type === 'bad_habit';
61+
}
5562
</script>
5663

5764
<style scoped>

0 commit comments

Comments
 (0)