我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

Using environment variables

GitHub sets default environment variables for each GitHub 操作 workflow run. You can also set custom environment variables in your workflow file.

GitHub 操作 可用于 GitHub Free、GitHub Pro、GitHub Team、GitHub Enterprise Cloud 和 GitHub One。 GitHub 操作 is not available for private repositories owned by accounts using legacy per-repository plans. 更多信息请参阅“GitHub 的产品”。

本文内容

About environment variables

GitHub sets default environment variables that are available to every step in a workflow run. Environment variables are case-sensitive. Commands run in actions or steps can create, read, and modify environment variables.

To set custom environment variables, you need to specify the variables in the workflow file. You can define environment variables for a step, job, or entire workflow using the jobs.<job_id>.steps.env, jobs.<job_id>.env, and env keywords. 更多信息请参阅“GitHub 的工作流程语法”。

steps:
  - name: Hello world
    run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
    env:
      FIRST_NAME: Mona
      middle_name: The
      Last_Name: Octocat

You can also use the set-env workflow command to set an environment variable that the following steps in a workflow can use. The set-env command can be used directly by an action or as a shell command in a workflow file using the run keyword. For more information, see "Workflow commands for GitHub 操作."

默认环境变量

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

环境变量描述
HOME用于存储用户数据的 GitHub 主目录路径。 例如 /github/home
GITHUB_WORKFLOW工作流程的名称。
GITHUB_RUN_IDA unique number for each run within a repository. This number does not change if you re-run the workflow run.
GITHUB_RUN_NUMBERA unique number for each run of a particular workflow in a repository. This number begins at 1 for the workflow's first run, and increments with each new run. This number does not change if you re-run the workflow run.
GITHUB_ACTION操作唯一的标识符 (id)。
GITHUB_ACTIONSAlways set to true when GitHub 操作 is running the workflow. You can use this variable to differentiate when tests are being run locally or by GitHub 操作.
GITHUB_ACTOR发起工作流程的个人或应用程序的名称。 例如 octocat
GITHUB_REPOSITORY所有者和仓库名称。 例如 octocat/Hello-World
GITHUB_EVENT_NAME触发工作流程的 web 挂钩事件的名称。
GITHUB_EVENT_PATH具有完整 web 挂钩事件有效负载的文件路径。 例如 /github/workflow/event.json
GITHUB_WORKSPACEGitHub 工作空间目录路径。 如果您的工作流程使用 actions/checkout 操作,工作空间目录将包含存储仓库副本的子目录。 如果不使用 actions/checkout 操作,该目录将为空。 例如 /home/runner/work/my-repo-name/my-repo-name
GITHUB_SHA触发工作流程的提交 SHA。 例如 ffac537e6cbbf934b08745a378932722df287a53
GITHUB_REF触发工作流程的分支或标记参考。 例如 refs/heads/feature-branch-1。 如果分支或标记都不适用于事件类型,则变量不会存在。
GITHUB_HEAD_REF仅为复刻的仓库设置。 头部仓库的分支。
GITHUB_BASE_REF仅为复刻的仓库设置。 基础仓库的分支。

Naming conventions for environment variables

Note: GitHub reserves the GITHUB_ environment variable prefix for internal use by GitHub. 设置有 GITHUB_ 前缀的环境变量或密码将导致错误。

您设置的指向文件系统上某个位置的任何新环境变量都应该有 _PATH 后缀。 HOMEGITHUB_WORKSPACE 默认变量例外于此约定,因为 "home" 和 "workspace" 一词已经暗示位置。

问问别人

找不到要找的内容?

联系我们