Skip to main content

Variables reference

Find information for supported variables, naming conventions, limits, and contexts in GitHub Actions workflows.

This article lists the supported variables you can use in GitHub Actions workflows, including environment variables, configuration variables, and default variables provided by GitHub. Use this reference to look up variable names, naming conventions, limits, and supported contexts when configuring your workflows.

For more information about variables, see Variables.

Default environment variables

The default environment variables that GitHub sets are available to every step in a workflow.

Because default environment variables are set by GitHub and not defined in a workflow, they are not accessible through the env context. However, most of the default variables have a corresponding, and similarly named, context property. For example, the value of the GITHUB_REF variable can be read during workflow processing using the ${{ github.ref }} context property.

不能覆盖名为 GITHUB_*RUNNER_* 的默认环境变量的值。 目前可以覆盖 CI 变量的值。 但是,并不能保证这总是可行。 For more information about setting environment variables, see 在变量中存储信息 and GitHub Actions 的工作流命令.

We strongly recommend that actions use variables to access the filesystem rather than using hardcoded file paths. GitHub sets variables for actions to use in all runner environments.

VariableDescription
CIAlways set to true.
GITHUB_ACTIONThe name of the action currently running, or the id of a step. For example, for an action, __repo-owner_name-of-action-repo.

GitHub removes special characters, and uses the name __run when the current step runs a script without an id. If you use the same script or action more than once in the same job, the name will include a suffix that consists of the sequence number preceded by an underscore. For example, the first script you run will have the name __run, and the second script will be named __run_2. Similarly, the second invocation of actions/checkout will be actionscheckout2.
GITHUB_ACTION_PATHThe path where an action is located. This property is only supported in composite actions. You can use this path to change directories to where the action is located and access other files in that same repository. For example, /home/runner/work/_actions/repo-owner/name-of-action-repo/v1.
GITHUB_ACTION_REPOSITORYFor a step executing an action, this is the owner and repository name of the action. For example, actions/checkout.
GITHUB_ACTIONSAlways set to true when GitHub Actions is running the workflow. You can use this variable to differentiate when tests are being run locally or by GitHub Actions.
GITHUB_ACTORThe name of the person or app that initiated the workflow. For example, octocat.
GITHUB_ACTOR_ID触发初始工作流运行的个人或应用的帐户 ID。 例如 1234567。 请注意,这与参与者用户名不同。
GITHUB_API_URLReturns the API URL. For example: https://api.github.com.
GITHUB_BASE_REFThe name of the base ref or target branch of the pull request in a workflow run. This is only set when the event that triggers a workflow run is either pull_request or pull_request_target. For example, main.
GITHUB_ENVThe path on the runner to the file that sets variables from workflow commands. The path to this file is unique to the current step and changes for each step in a job. For example, /home/runner/work/_temp/_runner_file_commands/set_env_87406d6e-4979-4d42-98e1-3dab1f48b13a. For more information, see GitHub Actions 的工作流命令.
GITHUB_EVENT_NAMEThe name of the event that triggered the workflow. For example, workflow_dispatch.
GITHUB_EVENT_PATHThe path to the file on the runner that contains the full event webhook payload. For example, /github/workflow/event.json.
GITHUB_GRAPHQL_URLReturns the GraphQL API URL. For example: https://api.github.com/graphql.
GITHUB_HEAD_REFThe head ref or source branch of the pull request in a workflow run. This property is only set when the event that triggers a workflow run is either pull_request or pull_request_target. For example, feature-branch-1.
GITHUB_JOBThe job_id of the current job. For example, greeting_job.
GITHUB_OUTPUTThe path on the runner to the file that sets the current step's outputs from workflow commands. The path to this file is unique to the current step and changes for each step in a job. For example, /home/runner/work/_temp/_runner_file_commands/set_output_a50ef383-b063-46d9-9157-57953fc9f3f0. For more information, see GitHub Actions 的工作流命令.
GITHUB_PATHThe path on the runner to the file that sets system PATH variables from workflow commands. The path to this file is unique to the current step and changes for each step in a job. For example, /home/runner/work/_temp/_runner_file_commands/add_path_899b9445-ad4a-400c-aa89-249f18632cf5. For more information, see GitHub Actions 的工作流命令.
GITHUB_REF触发工作流运行的分支或标记的格式完整的参考。 对于 push 触发的工作流,这是推送的分支或标记参考。 对于 pull_request 触发的工作流,这是拉取请求合并分支。 对于 release 触发的工作流,这是创建的发布标记。 对于其他触发器,这是触发工作流运行的分支或标记参考。 此变量仅在分支或标记可用于事件类型时才会设置。 给出的引用是完整的,这意味着对于分支,格式为 refs/heads/<branch_name>。 对于拉取请求事件,除非 pull_request_target,则为 refs/pull/<pr_number>/mergepull_request_target 事件具有基础分支中的 ref。 对于标记,格式为 refs/tags/<tag_name>。 例如,refs/heads/feature-branch-1
GITHUB_REF_NAME触发工作流运行的分支或标记的短参考名称。 此值与 GitHub 上显示的分支或标记名称匹配。 例如,feature-branch-1

拉取请求的格式为 <pr_number>/merge
GITHUB_REF_PROTECTED如果为触发工作流运行的 ref 配置分支保护 或 规则集 ,则为 true
GITHUB_REF_TYPE触发工作流运行的 ref 类型。 有效值为 branch or tag
GITHUB_REPOSITORYThe owner and repository name. For example, octocat/Hello-World.
GITHUB_REPOSITORY_ID存储库的 ID。 例如 123456789。 请注意,这与存储库名称不同。
GITHUB_REPOSITORY_OWNERThe repository owner's name. For example, octocat.
GITHUB_REPOSITORY_OWNER_ID存储库所有者的帐户 ID。 例如 1234567。 请注意,这与所有者名称不同。
GITHUB_RETENTION_DAYSThe number of days that workflow run logs and artifacts are kept. For example, 90.
GITHUB_RUN_ATTEMPTA unique number for each attempt of a particular workflow run in a repository. This number begins at 1 for the workflow run's first attempt, and increments with each re-run. For example, 3.
GITHUB_RUN_ID存储库中每个工作流运行的唯一编号。 如果您重新执行工作流程运行,此编号不变。 For example, 1658821493.
GITHUB_RUN_NUMBER仓库中特定工作流程每个运行的唯一编号。 工作流首次运行时,此编号从 1 开始,并随着每次新的运行而递增。 如果您重新执行工作流程运行,此编号不变。 For example, 3.
GITHUB_SERVER_URLThe URL of the GitHub server. For example: https://github.com.
GITHUB_SHA触发工作流的提交 SHA。 此提交 SHA 的值取决于触发工作流程的事件。 有关详细信息,请参阅“触发工作流的事件”。 例如,ffac537e6cbbf934b08745a378932722df287a53
GITHUB_STEP_SUMMARYThe path on the runner to the file that contains job summaries from workflow commands. The path to this file is unique to the current step and changes for each step in a job. For example, /home/runner/_layout/_work/_temp/_runner_file_commands/step_summary_1cb22d7f-5663-41a8-9ffc-13472605c76c. For more information, see GitHub Actions 的工作流命令.
GITHUB_TRIGGERING_ACTOR发起工作流运行的用户的用户名。 如果工作流运行是重新运行,则此值可能与 github.actor 不同。 即使启动重新运行的参与者 (github.triggering_actor) 具有不同的权限,任何工作流重新运行都将使用 github.actor 的权限。
GITHUB_WORKFLOWThe name of the workflow. For example, My test workflow. If the workflow file doesn't specify a name, the value of this variable is the full path of the workflow file in the repository.
GITHUB_WORKFLOW_REF工作流的引用路径。 例如,octocat/hello-world/.github/workflows/my-workflow.yml@refs/heads/my_branch
GITHUB_WORKFLOW_SHA工作流文件的提交 SHA。
GITHUB_WORKSPACEThe default working directory on the runner for steps, and the default location of your repository when using the checkout action. For example, /home/runner/work/my-repo-name/my-repo-name.
RUNNER_ARCH执行作业的运行器的体系结构。 可能的值为 X86X64ARMARM64
RUNNER_DEBUG仅当启用调试日志记录并且始终具有值 1 时,才会进行此设置。 它可以用作指示器,以便在自己的作业步骤中启用更多调试或详细日志记录。
RUNNER_ENVIRONMENT执行作业的运行器的环境。 可能的值包括:对于 GitHub 提供的 GitHub 托管的运行器为 github-hosted,对于存储库所有者配置的自承载运行器为 self-hosted
RUNNER_NAME执行作业的运行器的名称。 此名称在工作流运行中可能并不唯一,因为存储库和组织级别的运行器可以使用同一名称。 For example, Hosted Agent
RUNNER_OS执行作业的运行器的操作系统。 可能的值为 LinuxWindowsmacOS。 For example, Windows
RUNNER_TEMP运行器临时目录的路径。 此目录在每个作业的开始和结束时都是空的。 注意,如果运行者的用户帐户没有权限删除这些文件,则不会被删除。 For example, D:\a\_temp
RUNNER_TOOL_CACHE包含 GitHub 托管运行器预安装工具的目录路径。 有关详细信息,请参阅“使用 GitHub 托管的运行器”。 For example, C:\hostedtoolcache\windows

注意

If you need to use a workflow run's URL from within a job, you can combine these variables: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID

Naming conventions for configuration variables

The following rules apply to configuration variable names:

  • 只能包含字母数字字符([a-z][A-Z][0-9])或下划线 (_)。 不允许空格。
  • 不得以 GITHUB_ 前缀开头。
  • 不能以数字开头。
  • 引用时不区分大小写。 无论输入格式如何,GitHub 始终以大写形式存储机密名称。
  • 在创建的仓库、组织或企业中必须是唯一的。

Naming conventions for environment variables

When you set an environment variable, you cannot use any of the default environment variable names. For a complete list of default environment variables, see Variables reference below. If you attempt to override the value of one of these default variables, the assignment is ignored.

注意

You can list the entire set of environment variables that are available to a workflow step by using run: env in a step and then examining the output for the step.

Configuration variable precedence

If a variable with the same name exists at multiple levels, the variable at the lowest level takes precedence. For example, if an organization-level variable has the same name as a repository-level variable, then the repository-level variable takes precedence. Similarly, if an organization, repository, and environment all have a variable with the same name, the environment-level variable takes precedence.

For reusable workflows, the variables from the caller workflow's repository are used. Variables from the repository that contains the called workflow are not made available to the caller workflow.

Limits for configuration variables

Individual variables are limited to 48 KB in size.

You can store up to 1,000 organization variables, 500 variables per repository, and 100 variables per environment. The total combined size limit for organization and repository variables is 256 KB per workflow run.

A workflow created in a repository can access the following number of variables:

  • Up to 500 repository variables, if the total size of repository variables is less than 256 KB. If the total size of repository variables exceeds 256 KB, only the repository variables that fall below the limit will be available (as sorted alphabetically by variable name).
  • Up to 1,000 organization variables, if the total combined size of repository and organization variables is less than 256 KB. If the total combined size of organization and repository variables exceeds 256 KB, only the organization variables that fall below that limit will be available (after accounting for repository variables and as sorted alphabetically by variable name).
  • Up to 100 environment-level variables.

注意

Environment-level variables do not count toward the 256 KB total size limit. If you exceed the combined size limit for repository and organization variables and still need additional variables, you can use an environment and define additional variables in the environment.

Supported contexts

You will commonly use either the env or github context to access variable values in parts of the workflow that are processed before jobs are sent to runners.

警告

Do not print the github context to logs. It contains sensitive information.

ContextUse caseExample
envReference custom variables defined in the workflow.${{ env.MY_VARIABLE }}
githubReference information about the workflow run and the event that triggered the run.${{ github.repository }}