Skip to content

Commit 0cf3161

Browse files
authored
Q2 cr 15513 working directory step hook (#760)
* Add syntax for working directory in step hook Added section for referencing working directoy in step hook and commented out limitation on the same * Update hooks.md * Update hooks.md Added inline xref to working directory section * Update hooks.md Added <step-name> to parentSteps syntax
1 parent f55dc1c commit 0cf3161

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

_docs/pipelines/hooks.md

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ It is possible to define all possible hooks (`on_elected`, `on_finish`, `on_succ
161161

162162
## Step hooks
163163

164-
Hooks can also be defined for individual steps inside a pipeline. This capability allows for more granular control on defining prepare/cleanup phases for specific steps.
164+
You can also define hooks for individual steps inside a pipeline. This capability allows you to enforce more granular control on defining prepare/cleanup phases for specific steps.
165165

166-
The syntax for step hooks is the same as pipeline hooks (`on_elected`, `on_finish`, `on_success`, `on_fail`), you just need to put the respective segment under a step instead of the root of the pipeline.
166+
The syntax for step hooks is the same as pipeline hooks: `on_elected`, `on_finish`, `on_success`, `on_fail`. The only difference is that you need to add the respective segment within a step instead of the at the root of the pipeline.
167167

168168
For example, this pipeline will always run a cleanup step after integration tests (even if the tests themselves fail).
169169

@@ -200,7 +200,7 @@ steps:
200200
{% endhighlight %}
201201

202202

203-
Logs for steps hooks are shown in the log window of the step itself.
203+
Logs for steps hooks are displayed in the log window of the step itself.
204204

205205
{% include
206206
image.html
@@ -212,7 +212,7 @@ caption="Hooks before a pipeline"
212212
max-width="80%"
213213
%}
214214

215-
As with pipeline hooks, it is possible to define multiple hook conditions for each step.
215+
As with pipeline hooks, you can also define multiple hook conditions for each step.
216216

217217
`codefresh.yml`
218218
{% highlight yaml %}
@@ -254,17 +254,17 @@ steps:
254254
The order of events in the example above is the following:
255255

256256
1. The `on_elected` segment executes first (authentication)
257-
1. The step itself executes (the security scan)
258-
1. The `on_fail` segment executes (only if the step throws an error code)
257+
1. The `Security Scanning` step itself executes the security scan
258+
1. The `on_fail` segment executes only if the step throws an error code
259259
1. The `on_finish` segment always executes at the end
260260

261261

262-
## Running steps/plugins in hooks
262+
### Running steps/plugins in hooks
263263

264-
Hooks can use [steps/plugins](https://steps.codefresh.io){:target="\_blank"}. With plugins you have to specify:
265-
266-
- The type field for the step/plugin.
267-
- The arguments needed for the step/plugin.
264+
Hooks can use [steps/plugins](https://steps.codefresh.io){:target="\_blank"}.
265+
With plugins you have to specify:
266+
- The type field for the step/plugin
267+
- The arguments needed for the step/plugin
268268

269269
`codefresh.yml`
270270
{% highlight yaml %}
@@ -298,13 +298,16 @@ steps:
298298
{% endraw %}
299299
{% endhighlight %}
300300

301-
## Controlling errors inside pipeline/step hooks
301+
### Controlling errors inside pipeline/step hooks
302302

303-
By default if a step fails within a pipeline, the whole pipeline will stop and be marked as failed.
304-
This is also true for `on_elected` segments as well. If they fail, then the whole pipeline will fail (regardless of the position of the segment in a pipeline or step). However, this only applies to `on_elected` segments.
305-
`on_success`, `on_fail` and `on_finish` segments do not affect the pipeline outcome at all, and a pipeline will continue even if one of these segments fails.
303+
By default, if a step fails within a pipeline, pipeline execution is terminated and the pipeline is marked as failed.
304+
* `on_elected` segments
305+
If `on_elected` segments fail, regardless of the position of the segment in a pipeline or step, the whole pipeline will fail.
306+
* `on_success`, `on_fail` and `on_finish` segments
307+
`on_success`, `on_fail` and `on_finish` segments do not affect the pipeline outcome. A pipeline will continue execution even if one of these segments fails.
306308

307-
For example the following pipeline will fail right away, because the pipeline hook fails at the beginning.
309+
**Pipeline execution example with `on_elected` segment**
310+
The following pipeline will fail right away, because the pipeline hook fails at the beginning.
308311

309312
`codefresh.yml`
310313
{% highlight yaml %}
@@ -351,6 +354,7 @@ steps:
351354
{% endraw %}
352355
{% endhighlight %}
353356

357+
354358
This pipeline will now execute successfully and `step1` will still run as normal, because we have used the `fail_fast` property. You can also use the `fail_fast` property on step hooks as well:
355359

356360
`codefresh.yml`
@@ -379,7 +383,7 @@ steps:
379383
>Notice that the `fail_fast` property is only available for `on_elected` hooks. The other types of hooks (`on_finish`, `on_success`, `on_fail`) do not affect the outcome of the pipeline in any way. Even if they fail, the pipeline will continue running to completion. This behavior is not configurable.
380384
381385

382-
## Using multiple steps for hooks
386+
### Using multiple steps for hooks
383387

384388
In all the previous examples, each hook was a single step running on a single Docker image. You can also define multiple steps for each hook. This is possible by inserting an extra `steps` keyword inside the hook and listing multiple Docker images under it:
385389

@@ -445,6 +449,10 @@ steps:
445449

446450
You can use multiple steps in a hook in both the pipeline and the step level.
447451

452+
### Referencing the 'working_directory' in step hooks
453+
To access the [`working_directory`]({{site.baseurl}}/docs/pipelines/what-is-the-codefresh-yaml/#working-directories) of a regular step through a hook, use the prefix `parentSteps.<step-name>` For example, to access the `working_directory` of the `clone` step, use {% raw %} `${{parentSteps.clone}}` {% endraw %}.
454+
455+
448456

449457
## Using annotations and labels in hooks
450458

@@ -525,9 +533,10 @@ The pipeline is not correct, because the first segment of annotations is directl
525533

526534
## Syntactic sugar syntax
527535

528-
To simplify the syntax for hooks, the following simplifications are also offered:
536+
We offer the following options to simplify the syntax for hooks.
529537

530-
If you do not want to use metadata or annotations in your hook the keyword `exec` can be omitted:
538+
**Not using metadata or annotations in your hook**
539+
Omit the keyword `exec`:
531540

532541
`codefresh.yml`
533542
{% highlight yaml %}
@@ -552,6 +561,7 @@ steps:
552561
{% endhighlight %}
553562

554563

564+
**Not specify teh Docker image**
555565
If you do not want to specify the Docker image you can simply omit it. Codefresh will use the `alpine` image in that case to run the hook:
556566

557567

@@ -581,25 +591,7 @@ steps:
581591
{% endhighlight %}
582592

583593

584-
If you don't use metadata or annotations, you can also completely remove the `exec` keyword and just mention the commands you want to run (`alpine` image will be used by default):
585594

586-
`codefresh.yml`
587-
{% highlight yaml %}
588-
{% raw %}
589-
version: "1.0"
590-
hooks:
591-
on_elected: # no exec/image keyword - alpine image will be used
592-
- echo "Pipeline starting"
593-
steps:
594-
build:
595-
type: build
596-
image_name: my_image
597-
tag: master
598-
hooks:
599-
on_success: # no exec/image keyword - alpine image will be used
600-
- echo "Docker image was built successfully"
601-
{% endraw %}
602-
{% endhighlight %}
603595

604596
## Using Type Steps / Plugins in hooks
605597

@@ -625,14 +617,14 @@ hooks:
625617

626618
With the current implementation of hooks, the following limitations are present:
627619

628-
* The [debugger]({{site.baseurl}}/docs/pipelines/debugging-pipelines/) cannot inspect commands inside hook segments
629-
* Hooks are not supported for [parallel steps]({{site.baseurl}}/docs/pipelines/advanced-workflows/)
630-
* Storage integrations don't resolve in hooks (for example, [test reports]({{site.baseurl}}/docs/testing/test-reports/#producing-allure-test-reports-from-codefresh-pipelines))
631-
* Step hook does not support the working_directory field aka `working_directory: ${{clone}}`
620+
* The [debugger]({{site.baseurl}}/docs/pipelines/debugging-pipelines/) cannot inspect commands inside hook segments.
621+
* Hooks are not supported for [parallel steps]({{site.baseurl}}/docs/pipelines/advanced-workflows/).
622+
* Storage integrations don't resolve in hooks (for example, [test reports]({{site.baseurl}}/docs/testing/test-reports/#producing-allure-test-reports-from-codefresh-pipelines)).
632623

633624
## Related articles
634625
[Conditional execution of steps]({{site.baseurl}}/docs/pipelines/conditional-execution-of-steps/)
635-
[Working directories]({{site.baseurl}}/docs/pipelines/what-is-the-codefresh-yaml/#working-directories)
626+
[Working directories]({{site.baseurl}}/docs/pipelines/what-is-the-codefresh-yaml/#working-directories)
627+
[Build step in pipelines]({{site.baseurl}}/docs/pipelines/steps/build/)
636628
[Annotations in pipelines]({{site.baseurl}}/docs/pipelines/annotations/)
637629

638630

0 commit comments

Comments
 (0)