Skip to content

Commit 895f01a

Browse files
committed
fix jobs.<job_id>.services.<service_id>.env should allow contexts (#500)
1 parent 207b9aa commit 895f01a

File tree

7 files changed

+65
-38
lines changed

7 files changed

+65
-38
lines changed

CHANGELOG.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
# [v1.7.5](https://github.com/rhysd/actionlint/releases/tag/v1.7.5) - 2024-12-28
33

44
- Strictly check available contexts in `${{ }}` placeholders following the ['Context availability' table](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#context-availability) in the official document.
5-
- For example, `jobs.<job>.env` allows `github` context but `jobs.<job>.services.<service>.env` doesn't allow any contexts. Now actionlint can catch the mistake.
5+
- For example, `jobs.<job_id>.defaults.run.shell` allows `env` context but `shell` workflow keys in other places allow no context.
66
```yaml
7+
defaults:
8+
run:
9+
# ERROR: No context is available here
10+
shell: ${{ env.SHELL }}
11+
712
jobs:
813
test:
914
runs-on: ubuntu-latest
10-
env:
11-
# OK. `github` context is available here.
12-
COMMIT_SHA: ${{ github.sha }}
13-
services:
14-
redis:
15-
image: redis
16-
env:
17-
# ERROR: No context is available here.
18-
COMMIT_SHA: ${{ github.sha }}
15+
defaults:
16+
run:
17+
# OK: 'env' context is available here
18+
shell: ${{ env.SHELL }}
1919
steps:
20-
- ...
20+
- run: echo hello
21+
# ERROR: No context is available here
22+
shell: ${{ env.SHELL}}
2123
```
2224
- Check a string literal passed to `fromJSON()` call. This pattern is [popular](https://github.com/search?q=fromJSON%28%27+lang%3Ayaml&type=code) to create array or object constants because GitHub Actions does not provide the literal syntax for them. See the [document](https://github.com/rhysd/actionlint/blob/main/docs/checks.md#contexts-and-built-in-functions) for more details. ([#464](https://github.com/rhysd/actionlint/issues/464))
2325
```yaml
@@ -796,7 +798,7 @@
796798
- Allow workflow calls are available in matrix jobs. See [the official announcement](https://github.blog/changelog/2022-08-22-github-actions-improvements-to-reusable-workflows-2/) for more details. ([#197](https://github.com/rhysd/actionlint/issues/197))
797799
```yaml
798800
jobs:
799-
ReusableMatrixJobForDeployment:
801+
ReuseableMatrixJobForDeployment:
800802
strategy:
801803
matrix:
802804
target: [dev, stage, prod]
@@ -929,7 +931,7 @@
929931
```
930932
- Fix usage of local actions (`uses: ./path/to/action`) was not checked when multiple workflow files were passed to `actionlint` command. ([#173](https://github.com/rhysd/actionlint/issues/173))
931933
- Allow `description:` is missing in `secrets:` of reusable workflow call definition since it is optional. ([#174](https://github.com/rhysd/actionlint/issues/174))
932-
- Fix type of property of `github.event.inputs` is string unlike `inputs` context. See [the document](https://github.com/rhysd/actionlint/blob/main/docs/checks.md#workflow-dispatch-event-validation) for more details. ([#181](https://github.com/rhysd/actionlint/issues/181))
934+
- Fix type of propery of `github.event.inputs` is string unlike `inputs` context. See [the document](https://github.com/rhysd/actionlint/blob/main/docs/checks.md#workflow-dispatch-event-validation) for more details. ([#181](https://github.com/rhysd/actionlint/issues/181))
933935
```yaml
934936
on:
935937
workflow_dispatch:

docs/checks.md

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,8 +2626,10 @@ Example input:
26262626
```yaml
26272627
on: push
26282628
2629-
env:
2630-
NAME: rhysd
2629+
defaults:
2630+
run:
2631+
# ERROR: No context is available here
2632+
shell: ${{ env.SHELL }}
26312633
26322634
jobs:
26332635
test:
@@ -2639,19 +2641,19 @@ jobs:
26392641
# ERROR: 'runner' context is not available here
26402642
- ${{ runner.temp }}
26412643
runs-on: ubuntu-latest
2644+
defaults:
2645+
run:
2646+
# OK: 'env' context is available here
2647+
shell: ${{ env.SHELL }}
26422648
env:
26432649
# ERROR: 'env' context is not available here
2644-
NAME: ${{ env.NAME }}
2645-
services:
2646-
redis:
2647-
image: redis
2648-
env:
2649-
# ERROR: No context is allowed here
2650-
COMMIT_SHA: ${{ github.sha }}
2650+
FOO: ${{ env.BAR }}
26512651
steps:
26522652
- env:
26532653
# OK: 'env' context is available here
2654-
NAME: ${{ env.NAME }}
2654+
FOO: ${{ env.BAR }}
2655+
# ERROR: No context is available here
2656+
shell: ${{ env.SHELL}}
26552657
# ERROR: 'success()' function is not available here
26562658
run: echo 'Success? ${{ success() }}'
26572659
# OK: 'success()' function is available here
@@ -2661,25 +2663,29 @@ jobs:
26612663
Output:
26622664

26632665
```
2664-
test.yaml:14:17: context "runner" is not allowed here. available contexts are "github", "inputs", "needs", "vars". see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
2666+
test.yaml:6:16: context "env" is not allowed here. no context is available here. see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
2667+
|
2668+
6 | shell: ${{ env.SHELL }}
2669+
| ^~~~~~~~~
2670+
test.yaml:16:17: context "runner" is not allowed here. available contexts are "github", "inputs", "needs", "vars". see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
26652671
|
2666-
14 | - ${{ runner.temp }}
2672+
16 | - ${{ runner.temp }}
26672673
| ^~~~~~~~~~~
2668-
test.yaml:18:17: context "env" is not allowed here. available contexts are "github", "inputs", "matrix", "needs", "secrets", "strategy", "vars". see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
2674+
test.yaml:24:16: context "env" is not allowed here. available contexts are "github", "inputs", "matrix", "needs", "secrets", "strategy", "vars". see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
26692675
|
2670-
18 | NAME: ${{ env.NAME }}
2671-
| ^~~~~~~~
2672-
test.yaml:24:27: context "github" is not allowed here. no context is available here. see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
2676+
24 | FOO: ${{ env.BAR }}
2677+
| ^~~~~~~
2678+
test.yaml:30:20: context "env" is not allowed here. no context is available here. see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
26732679
|
2674-
24 | COMMIT_SHA: ${{ github.sha }}
2675-
| ^~~~~~~~~~
2676-
test.yaml:30:33: calling function "success" is not allowed here. "success" is only available in "jobs.<job_id>.if", "jobs.<job_id>.steps.if". see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
2680+
30 | shell: ${{ env.SHELL}}
2681+
| ^~~~~~~~~~~
2682+
test.yaml:32:33: calling function "success" is not allowed here. "success" is only available in "jobs.<job_id>.if", "jobs.<job_id>.steps.if". see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability for more details [expression]
26772683
|
2678-
30 | run: echo 'Success? ${{ success() }}'
2684+
32 | run: echo 'Success? ${{ success() }}'
26792685
| ^~~~~~~~~
26802686
```
26812687

2682-
[Playground](https://rhysd.github.io/actionlint/#eNp0j8FOwzAMhu99Ch+QBof2AXJBE0KCQ+Ew7ihNvSawJpXtdFRT3x1lXdsJNF+i/7f953PwCrrINsvQ9yoDeNuWzwrIDlxn2VeoOJmCLOkFYCEt2AyTAmi1kPuZFUDtCI0EGlYLIIe70wkaJzZWxTHQ9/4QjjCO/0Yoeo9UCLbd3KboOU+UsYpeYn7QCebcuhCnmqhTBPq+SGreZ6TeGeR5krB2vMK5VjeoJncxr4JTPb2X5evH5+5lq64PYauXTwS7JTT/u38b7nKgAjQ2wGYXjUHmx/MsT+L+AcZxs/Lu1dr5DQAA//+iDXkG)
2688+
[Playground](https://rhysd.github.io/actionlint/#eNp8j81qwzAQhO96ijkU0h6cB/CltNDSQyDQPIHtrGO3imT2J2kIfvciJ45NIT2J0Xy7MxtDjs6kcW5LdWFeJXcAW0gPIA15n+PhfAaFw3Lz8bZaoe+d+4rlQCqJXlHlQml3uihgXyi3P6MCti1TpZFP0xeQDat3rTZWLo+Rv2sfjyngL8IWAvFSad+NNluQLPW30oJa5otUZrDmt1zRKfXeTcmjcBjB9/V6gl5fPkdElLrb4mw+8d/UveCZnUqCqiZisbGqIpHngZWLeHxC3y9udFvnk/MbAAD//3L9fcg=)
26832689

26842690
Some contexts are only available in some places. For example, `env` context is not available at `jobs.<job_id>.env`, but it is
26852691
available at `jobs.<job_id>.steps.env`.

rule_expression.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ func (rule *RuleExpression) checkContainer(c *Container, workflowKey, childWorkf
485485
rule.checkString(c.Credentials.Username, k)
486486
rule.checkString(c.Credentials.Password, k)
487487
}
488-
rule.checkEnv(c.Env, workflowKey+".env.<env_id>") // e.g. jobs.<job_id>.container.env.<env_id>
488+
rule.checkEnv(c.Env, childWorkflowKey+".env.<env_id>") // e.g. jobs.<job_id>.container.env.<env_id>
489489
rule.checkStrings(c.Ports, workflowKey)
490490
rule.checkStrings(c.Volumes, workflowKey)
491491
rule.checkString(c.Options, workflowKey)

testdata/err/context_availability.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
/test\.yaml:106:18: context "runner" is not allowed here\. .+ \[expression\]/
1818
/test\.yaml:111:20: context "env" is not allowed here\. .+ \[expression\]/
1919
/test\.yaml:115:25: context "runner" is not allowed here\. .+ \[expression\]/
20-
/test\.yaml:121:23: context "runner" is not allowed here\. .+ \[expression\]/
2120
/test\.yaml:127:17: context "env" is not allowed here\. .+ \[expression\]/
2221
/test\.yaml:134:23: context "env" is not allowed here\. .+ \[expression\]/
2322
/test\.yaml:139:23: context "env" is not allowed here\. .+ \[expression\]/

testdata/err/context_availability.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ jobs:
111111
image: ${{ env.IMAGE_NAME }}
112112
# jobs.<job_id>.services.<service_id>.credentials
113113
credentials:
114-
# ERROR
114+
# ERROR because runner is not available
115115
username: ${{ runner.name }}
116116
# OK
117117
password: ${{ env.MY_PASSWORD }}
118118
# jobs.<job_id>.services.<service_id>.env.<env_id>
119119
env:
120-
# ERROR
120+
# OK (#500)
121121
RUNNER: ${{ runner.name }}
122122
# jobs.<job_id>.strategy
123123
strategy:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/test\.yaml:6:16: context "env" is not allowed here\. no context is available here\. .+ \[expression\]/
2+
/test\.yaml:18:20: context "env" is not allowed here\. no context is available here\. .+ \[expression\]/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
on: push
2+
3+
defaults:
4+
run:
5+
# ERROR: No context is available here
6+
shell: ${{ env.SHELL }}
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
defaults:
12+
run:
13+
# OK: 'env' context is available here
14+
shell: ${{ env.SHELL }}
15+
steps:
16+
- run: echo hello
17+
# ERROR: No context is available here
18+
shell: ${{ env.SHELL}}

0 commit comments

Comments
 (0)