Skip to content

Commit 4e2b19b

Browse files
authored
Merge pull request #4159 from gustafla/patch-1
Update exercise 11.16 hint to work correctly on current GitHub Actions
2 parents ff8f5b6 + 4e59bfe commit 4e2b19b

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/content/11/en/part11d.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ You most likely need functions [contains](https://docs.github.com/en/actions/lea
280280
281281
Developing workflows is not easy, and quite often the only option is trial and error. It might actually be advisable to have a separate repository for getting the configuration right, and when it is done, to copy the right configurations to the actual repository.
282282
283-
It would also make sense to re-use longer conditions by moving them to commonly accessible variables and referring these variables on job/step levels:
283+
It would also make sense to re-use longer conditions by moving them to commonly accessible variables and referring these variables on the step level:
284284
285285
```yaml
286286
name: some workflow name
@@ -292,17 +292,31 @@ env:
292292
jobs:
293293
job1:
294294
# rest of the job
295+
outputs:
296+
# here we produce a record of the outcome of a key step in the job.
297+
# the below will be 'true'
298+
job2_can_run: ${{ steps.final.outcome == 'success' }}
295299
steps:
296300
- if: ${{ env.CONDITION == 'true' }}
297301
run: echo 'this step is executed'
298302

299303
- if: ${{ env.CONDITION == 'false' }}
300304
run: echo 'this step will not be executed'
301305

306+
- if: ${{ env.CONDITION == 'true' }}
307+
# this is important, the id `final` is referenced in job1's `outputs`.
308+
id: final
309+
run: echo
310+
302311
job2:
303-
# this job will be dependent on the above env.CONDITION, note the `github.` prefix which seem to be required while referencing the variable on the job level, but not the step level
304-
if: ${{ github.env.CONDITION == 'true' }}
305-
# rest of the job
312+
needs:
313+
- job1
314+
# this job will be dependent on the above job1's final step, which in turn depends on the CONDITION defined at the beginning of the file.
315+
# note that the `env`-variable cannot be directly accessed on the job level, so we need to use something else,
316+
# such as the outputs from another job.
317+
if: ${{ needs.job1.outputs.job2_can_run == 'true' }}
318+
steps:
319+
# rest of the job
306320
```
307321

308322
It would also be possible to install a tool such as [act](https://github.com/nektos/act) that makes it possible to run your workflows locally. Unless you end up using more involved use cases like creating your [own custom actions](https://docs.github.com/en/free-pro-team@latest/actions/creating-actions), going through the burden of setting up a tool such as act is most likely not worth the trouble.

0 commit comments

Comments
 (0)