Reusable workflows
Reference information for reusable workflows.
Access to reusable workflows
A reusable workflow can be used by another workflow if any of the following is true:
-
Both workflows are in the same repository.
-
The called workflow is stored in a public repository on GitHub Enterprise Server.
You cannot directly use reusable workflows defined on GitHub.com. Instead store a copy of the reusable workflow on 你的 GitHub Enterprise Server 实例, and call the workflow from that path.
-
The called workflow is stored in an internal repository and the settings for that repository allow it to be accessed. For more information, see 与企业共享操作和工作流.
-
The called workflow is stored in a private repository and the settings for that repository allow it to be accessed. For more information, see 与企业共享操作和工作流.
The following table shows the accessibility of reusable workflows to a caller workflow, depending on the visibility of the host repository.
Caller repository | Accessible workflows repositories |
---|---|
private | private , internal , and public |
internal | internal , and public |
public | public |
The Actions permissions on the callers repository's Actions settings page must be configured to allow the use of actions and reusable workflows - see 管理存储库的 GitHub Actions 设置.
For internal or private repositories, the Access policy on the Actions settings page of the called workflow's repository must be explicitly configured to allow access from repositories containing caller workflows - see 管理存储库的 GitHub Actions 设置.
注意
为了增强安全性,GitHub Actions 不支持对操作或可重用工作流进行重定向。 这意味着,当所有者、操作存储库的名称或操作名称发生更改时,使用该操作并具有先前名称的任何工作流都将失败。
Limitations of reusable worklows
-
You can connect up to four levels of workflows. For more information, see Nesting reusable workflows.
-
You can call a maximum of 20 unique reusable workflows from a single workflow file. This limit includes any trees of nested reusable workflows that may be called starting from your top-level caller workflow file.
For example, top-level-caller-workflow.yml → called-workflow-1.yml → called-workflow-2.yml counts as 2 reusable workflows.
-
Any environment variables set in an
env
context defined at the workflow level in the caller workflow are not propagated to the called workflow. For more information, see 在变量中存储信息 and 上下文参考. -
Similarly, environment variables set in the
env
context, defined in the called workflow, are not accessible in theenv
context of the caller workflow. Instead, you must use outputs of the reusable workflow. For more information, see Using outputs from a reusable workflow. -
To reuse variables in multiple workflows, set them at the organization, repository, or environment levels and reference them using the
vars
context. For more information see 在变量中存储信息 and 上下文参考. -
Reusable workflows are called directly within a job, and not from within a job step. You cannot, therefore, use
GITHUB_ENV
to pass values to job steps in the caller workflow.
Supported keywords for jobs that call a reusable workflow
When you call a reusable workflow, you can only use the following keywords in the job containing the call:
-
注意
- If
jobs.<job_id>.permissions
is not specified in the calling job, the called workflow will have the default permissions for theGITHUB_TOKEN
. For more information, see GitHub Actions 的工作流语法. - The
GITHUB_TOKEN
permissions passed from the caller workflow can be only downgraded (not elevated) by the called workflow. - If you use
jobs.<job_id>.concurrency.cancel-in-progress: true
, don't use the same value forjobs.<job_id>.concurrency.group
in the called and caller workflows as this will cause the workflow that's already running to be cancelled. A called workflow uses the name of its caller workflow in ${{ github.workflow }}, so using this context as the value ofjobs.<job_id>.concurrency.group
in both caller and called workflows will cause the caller workflow to be cancelled when the called workflow runs.
- If
How reusable workflows use runners
GitHub-hosted runners
The assignment of GitHub-hosted runners is always evaluated using only the caller's context. Billing for GitHub-hosted runners is always associated with the caller. The caller workflow cannot use GitHub-hosted runners from the called repository. For more information, see GitHub 托管的运行程序.
Self-hosted runners
Called workflows that are owned by the same user or organization or enterprise as the caller workflow can access self-hosted runners from the caller's context. This means that a called workflow can access self-hosted runners that are:
- In the caller repository
- In the caller repository's organization or enterprise, provided that the runner has been made available to the caller repository
Access and permissions for nested workflows
A workflow that contains nested reusable workflows will fail if any of the nested workflows is inaccessible to the initial caller workflow. For more information, see Access to reusable workflows.
GITHUB_TOKEN
permissions can only be the same or more restrictive in nested workflows. For example, in the workflow chain A > B > C, if workflow A has package: read
token permission, then B and C cannot have package: write
permission. For more information, see 在工作流中使用 GITHUB_TOKEN 进行身份验证.
For information on how to use the API to determine which workflow files were involved in a particular workflow run, see 重用工作流.
Behavior of reusable workflows when re-running jobs
可使用 SHA、发布标记或分支名称引用公共存储库中的可重用工作流。 有关详细信息,请参阅“重用工作流”。
重新运行使用可重用工作流且引用不是 SHA 的工作流时,有一些行为需要注意:
- 重新运行工作流中的所有作业时将使用指定引用中的可重用工作流。 有关重新运行工作流中所有作业的详细信息,请参阅 重新运行工作流程和作业。
- 重新运行失败的作业或工作流中的特定作业时将使用第一次尝试的同一提交 SHA 中的可重用工作流。 有关重新运行工作流中失败作业的详细信息,请参阅 重新运行工作流程和作业。 有关重新运行工作流中特定作业的详细信息,请参阅 重新运行工作流程和作业。
github
context
When a reusable workflow is triggered by a caller workflow, the github
context is always associated with the caller workflow. The called workflow is automatically granted access to github.token
and secrets.GITHUB_TOKEN
. For more information about the github
context, see 上下文参考.
Workflow templates
Reference information to use when creating workflow templates for your organization.
Workflow template availability
You can use templates in repositories that match or have more restricted visibility than the template repository.
- Workflow templates in a public
.github
repository are available to all repository types. - Workflow templates in an internal
.github
repository are only available to internal and private repositories. - Workflow templates in a private
.github
repository are only available to private repositories.
Granting access for private/internal repositories
If you're using a private or internal .github
repository, you need to grant Read access to users or teams who should be able to use the templates.
The $default-branch
placeholder
If you need to refer to a repository's default branch, you can use the $default-branch
placeholder in your workflow template. When a workflow is created the placeholder will be automatically replaced with the name of the repository's default branch.
Placeholder values in the runs-on
key
The following values in the runs-on
key are also treated as placeholders:
ubuntu-latest
is replaced with[ self-hosted ]
windows-latest
is replaced with[ self-hosted, windows ]
macos-latest"
is replaced with[ self-hosted, macOS ]
Example workflow template file
This file named octo-organization-ci.yml
demonstrates a basic workflow.
name: Octo Organization CI on: push: branches: [ $default-branch ] pull_request: branches: [ $default-branch ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Run a one-line script run: echo Hello from Octo Organization
name: Octo Organization CI
on:
push:
branches: [ $default-branch ]
pull_request:
branches: [ $default-branch ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run a one-line script
run: echo Hello from Octo Organization
Metadata file requirements
The metadata file must have the same name as the workflow file, but instead of the .yml
extension, it must be appended with .properties.json
. For example, this file named octo-organization-ci.properties.json
contains the metadata for a workflow file named octo-organization-ci.yml
:
{ "name": "Octo Organization Workflow", "description": "Octo Organization CI workflow template.", "iconName": "example-icon", "categories": [ "Go" ], "filePatterns": [ "package.json$", "^Dockerfile", ".*\\.md$" ] }
{
"name": "Octo Organization Workflow",
"description": "Octo Organization CI workflow template.",
"iconName": "example-icon",
"categories": [
"Go"
],
"filePatterns": [
"package.json$",
"^Dockerfile",
".*\\.md$"
]
}
-
name
- Required. The name of the workflow. This is displayed in the list of available workflows. -
description
- Required. The description of the workflow. This is displayed in the list of available workflows. -
iconName
- Optional. Specifies an icon for the workflow that is displayed in the list of workflows.iconName
can one of the following types:- An SVG file that is stored in the
workflow-templates
directory. To reference a file, the value must be the file name without the file extension. For example, an SVG file namedexample-icon.svg
is referenced asexample-icon
. - An icon from GitHub's set of Octicons. To reference an octicon, the value must be
octicon <icon name>
. For example,octicon smiley
.
- An SVG file that is stored in the
-
categories
- Optional. Defines the categories that the workflow is shown under. You can use category names from the following lists:- General category names from the starter-workflows repository.
- Linguist languages from the list in the linguist repository.
- Supported tech stacks from the list in the starter-workflows repository.
-
filePatterns
- Optional. Allows the workflow to be used if the user's repository has a file in its root directory that matches a defined regular expression.