Skip to content

Commit 6a8580f

Browse files
Document the $/ self-referencing uses syntax for actions and reusable workflows (#62055)
Co-authored-by: Joe Clark <31087804+jc-clark@users.noreply.github.com>
1 parent f13d248 commit 6a8580f

6 files changed

Lines changed: 89 additions & 6 deletions

File tree

content/actions/how-tos/write-workflows/choose-what-workflows-do/find-and-customize-actions.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,51 @@ An action's listing page includes the action's version and the workflow syntax r
6666

6767
### Adding an action from the same repository
6868

69-
If an action is defined in the same repository where your workflow file uses the action, you can reference the action with either the ‌`{owner}/{repo}@{ref}` or `./path/to/dir` syntax in your workflow file.
69+
If an action is defined in the same repository where your workflow file uses the action, you can reference the action with the `$/path/to/dir` self repository reference, or with the `{owner}/{repo}@{ref}` or `./path/to/dir` syntax in your workflow file. The `$/` syntax is not available in {% data variables.product.prodname_ghe_server %}.
70+
71+
Example repository file structure:
72+
73+
```shell
74+
|-- hello-world (repository)
75+
| |__ .github
76+
| └── workflows
77+
| └── my-first-workflow.yml
78+
| └── actions
79+
| |__ hello-world-action
80+
| └── action.yml
81+
```
82+
83+
We recommend referencing the action with the `$/path/to/dir` self repository reference. This resolves to the same repository at the running commit, so you do not need to check out the repository first. For more information about how `$/` compares to `{owner}/{repo}@{ref}` and `./`, see [AUTOTITLE](/actions/reference/workflows-and-actions/workflow-syntax#example-using-an-action-in-the-same-repository-as-the-workflow-at-the-running-commit-recommended).
84+
85+
Example workflow file using `$/`:
86+
87+
```yaml
88+
jobs:
89+
my_first_job:
90+
runs-on: ubuntu-latest
91+
steps:
92+
# This step references an action in the same repository at the
93+
# running commit. No repository checkout is required.
94+
- name: Use hello-world-action
95+
uses: $/.github/actions/hello-world-action
96+
```
97+
98+
You can also reference the action with the relative `./path/to/dir` syntax, but it is more error-prone. The path is relative (`./`) to the default working directory (`github.workspace`, `$GITHUB_WORKSPACE`), so it requires a checkout step, and if the action checks out the repository to a location different than the workflow, the relative path must be updated.
7099

71-
{% data reusables.actions.workflows.section-referencing-an-action-from-the-same-repository %}
100+
Example workflow file using `./`:
101+
102+
```yaml
103+
jobs:
104+
my_first_job:
105+
runs-on: ubuntu-latest
106+
steps:
107+
# This step checks out a copy of your repository.
108+
- name: My first step - check out repository
109+
uses: {% data reusables.actions.action-checkout %}
110+
# This step references the directory that contains the action.
111+
- name: Use local hello-world-action
112+
uses: ./.github/actions/hello-world-action
113+
```
72114

73115
The `action.yml` file is used to provide metadata for the action. Learn about the content of this file in [AUTOTITLE](/actions/reference/workflows-and-actions/metadata-syntax).
74116

content/actions/reference/workflows-and-actions/metadata-syntax.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ runs:
339339
- uses: actions/checkout@main
340340
# References a subdirectory in a public GitHub repository at a specific branch, ref, or SHA
341341
- uses: actions/aws/ec2@main
342+
# References an action in the same repository at the running commit
343+
- uses: $/.github/actions/my-action
342344
# References a local action
343345
- uses: ./.github/actions/my-action
344346
# References a docker public registry action
@@ -347,6 +349,10 @@ runs:
347349
- uses: docker://alpine:3.8
348350
```
349351

352+
To reference an action stored in the same repository as your composite action, use the `$/` self repository reference, as shown in the `$/.github/actions/my-action` example above. It resolves to that repository at the running commit, so you do not need to check out the repository first, and it must not include an `@{ref}` suffix. The `$/` syntax is not available in {% data variables.product.prodname_ghe_server %}.
353+
354+
For a comparison of `$/`, `{owner}/{repo}@{ref}`, and `./`, see [AUTOTITLE](/actions/reference/workflows-and-actions/workflow-syntax#example-using-an-action-in-the-same-repository-as-the-workflow-at-the-running-commit-recommended).
355+
350356
#### `runs.steps[*].with`
351357

352358
**Optional** A `map` of the input parameters defined by the action. Each input parameter is a key/value pair. For more information, see [Example: Specifying inputs](#example-specifying-inputs).

content/actions/reference/workflows-and-actions/workflow-syntax.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,42 @@ jobs:
597597
uses: actions/aws/ec2@main
598598
```
599599

600+
### Example: Using an action in the same repository as the workflow at the running commit (recommended)
601+
602+
`$/path/to/action`
603+
604+
The `$/` prefix is the self repository reference. It references an action stored in the same repository as the workflow or action that is currently running, and resolves to that repository at the running commit (the same SHA as the running workflow or action). You do not need to check out the repository first, so it is the recommended way to reference an action within its own repository.
605+
606+
The `$/` syntax is not available in {% data variables.product.prodname_ghe_server %}.
607+
608+
A `$/` reference must not include an `@{ref}` suffix. The ref is always the commit the running workflow or action is using, so a reference such as `$/actions/my-action@v1` is invalid.
609+
610+
`$/` always resolves against the repository of the file it appears in, not the repository that called it. For example, if a reusable workflow in one repository is called by a workflow in another repository, a `$/` reference in the called workflow resolves to the called workflow's repository, not the calling workflow's repository. This makes `$/` reliable for action composition, where a relative `./` path would instead resolve against whatever is checked out in the caller's workspace. For using `$/` in a composite action's steps, see [AUTOTITLE](/actions/reference/workflows-and-actions/metadata-syntax#runsstepsuses).
611+
612+
The following table compares the ways to reference an action.
613+
614+
| Syntax | Resolves to | Recommended for |
615+
| ------ | ----------- | --------------- |
616+
| `$/path/to/action` | The same repository as the running workflow or action, at the running commit | Actions in the same repository |
617+
| `{owner}/{repo}@{ref}` | The specified repository at the specified ref | Actions in another repository |
618+
| `./path/to/action` | A path in the runner's checked-out workspace, relative to the default working directory (`{% raw %}${{ github.workspace }}{% endraw %}`) | Edge cases only |
619+
620+
```yaml
621+
on: [push]
622+
623+
jobs:
624+
my_first_job:
625+
runs-on: ubuntu-latest
626+
steps:
627+
# References an action in the same repository at the running commit
628+
- uses: $/.github/actions/hello-world-action
629+
```
630+
600631
### Example: Using an action in the same repository as the workflow
601632

602633
`./path/to/dir`
603634

604-
The path to the directory that contains the action in your workflow's repository. You must check out your repository before using the action.
635+
The path to the directory that contains the action in your workflow's repository. You must check out your repository before using the action, and the `./` path resolves against the runner's workspace rather than the repository of the running workflow. For most cases, use the `$/` syntax shown above instead.
605636

606637
{% data reusables.actions.workflows.section-referencing-an-action-from-the-same-repository %}
607638

data/reusables/actions/allow-specific-actions-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
### Allowing select actions{% ifversion actions-workflow-policy %} and reusable workflows{% endif %} to run
55

6-
When you choose {% data reusables.actions.policy-label-for-select-actions-workflows %}, local actions{% ifversion actions-workflow-policy %} and reusable workflows{% endif %} are allowed, and there are additional options for allowing other specific actions{% ifversion actions-workflow-policy %} and reusable workflows{% endif %}:
6+
When you choose {% data reusables.actions.policy-label-for-select-actions-workflows %}, local actions (`./` and `$/`){% ifversion actions-workflow-policy %} and reusable workflows{% endif %} are allowed, and there are additional options for allowing other specific actions{% ifversion actions-workflow-policy %} and reusable workflows{% endif %}:
77

88
{% data reusables.repositories.settings-permissions-org-policy-note %}
99

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
* `$/.github/workflows/{filename}` for a reusable workflow in the same repository. This is the recommended syntax for referencing a reusable workflow in the same repository. This syntax is not available in {% data variables.product.prodname_ghe_server %}.
12
* `{owner}/{repo}/.github/workflows/{filename}@{ref}` for reusable workflows in {% ifversion fpt %}public and private{% elsif ghec or ghes %}public, internal and private{% endif %} repositories.
23
* `./.github/workflows/{filename}` for reusable workflows in the same repository.
34

4-
In the first option, `{ref}` can be a SHA, a release tag, or a branch name. If a release tag and a branch have the same name, the release tag takes precedence over the branch name. Using the commit SHA is the safest option for stability and security. For more information, see [AUTOTITLE](/actions/reference/security/secure-use#reusing-third-party-workflows).
5+
When you reference a reusable workflow with `{owner}/{repo}` and `@{ref}`, the `{ref}` can be a SHA, a release tag, or a branch name. If a release tag and a branch have the same name, the release tag takes precedence over the branch name. Using the commit SHA is the safest option for stability and security. For more information, see [AUTOTITLE](/actions/reference/security/secure-use#reusing-third-party-workflows).
56

6-
If you use the second syntax option (without `{owner}/{repo}` and `@{ref}`) the called workflow is from the same commit as the caller workflow. Ref prefixes such as `refs/heads` and `refs/tags` are not allowed. You cannot use contexts or expressions in this keyword.
7+
When you reference a reusable workflow in the same repository using `$/` or `./` (without `{owner}/{repo}` and `@{ref}`), the called workflow is from the same commit as the caller workflow. A `$/` reference must not include an `@{ref}` suffix, and `$/` is not available in {% data variables.product.prodname_ghe_server %}. Ref prefixes such as `refs/heads` and `refs/tags` are not allowed. You cannot use contexts or expressions in this keyword.

data/reusables/actions/uses-keyword-example.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ jobs:
44
uses: octo-org/this-repo/.github/workflows/workflow-1.yml@172239021f7ba04fe7327647b213799853a9eb89
55
call-workflow-2-in-local-repo:
66
uses: ./.github/workflows/workflow-2.yml
7+
# The `$/` syntax is not available in GitHub Enterprise Server.
8+
call-workflow-in-same-repo-at-running-commit:
9+
uses: $/.github/workflows/workflow-2.yml
710
call-workflow-in-another-repo:
811
uses: octo-org/another-repo/.github/workflows/workflow.yml@v1
912
```

0 commit comments

Comments
 (0)