关于环境变量
GitHub 设置适用于工作流程运行中每个步骤的默认环境变量。 环境变量区分大小写。 在操作或步骤中运行的命令可以创建、读取和修改环境变量。
要设置自定义环境变量,您需要在工作流程文件中指定变量。 您可以使用 jobs.<job_id>.steps[*].env
、jobs.<job_id>.env
和 env
关键字定义步骤、作业或整个工作流程的环境变量。 更多信息请参阅“GitHub 的工作流程语法”。
jobs:
weekday_job:
runs-on: ubuntu-latest
env:
DAY_OF_WEEK: Mon
steps:
- name: "Hello world when it's Monday"
if: env.DAY_OF_WEEK == 'Mon'
run: echo "Hello $FIRST_NAME $middle_name $Last_Name, today is Monday!"
env:
FIRST_NAME: Mona
middle_name: The
Last_Name: Octocat
要在工作流程文件中使用环境变量的值,您应该使用 env
上下文。 如果要在运行器中使用环境变量的值,您可以使用运行器操作系统的正常方法来读取环境变量。
如果使用工作流程文件的 run
键从运行器操作系统中读取环境变量(如上例所示),则在作业发送到运行器后,该变量将在运行器操作系统中被替换。 对于工作流程文件的其他部分,必须使用 env
上下文来读取环境变量;这是因为工作流程键(例如 if
)需要在发送到运行器之前,在工作流程处理过程中替换变量。
您也可以使用 GITHUB_ENV
environment file 设置工作流程中的以下步骤可以使用的环境变量。 环境文件可直接由操作使用,或使用 run
关键字作为工作流程文件中的 shell 命令。 更多信息请参阅“GitHub Actions 的工作流程命令”。
默认环境变量
强烈建议操作使用环境变量访问文件系统,而非使用硬编码的文件路径。 GitHub 设置供操作用于所有运行器环境中的环境变量。
环境变量 | 描述 |
---|---|
CI | 始终设置为 true 。 |
GITHUB_WORKFLOW | 工作流程的名称。 |
GITHUB_RUN_ID | 仓库中每个运行的唯一编号。 如果您重新执行工作流程运行,此编号不变。 |
GITHUB_RUN_NUMBER | 仓库中特定工作流程每个运行的唯一编号。 此编号从 1(对应于工作流程的第一个运行)开始,然后随着每个新的运行而递增。 如果您重新执行工作流程运行,此编号不变。 |
GITHUB_ACTION | 操作唯一的标识符 (id )。 |
GITHUB_ACTIONS | 当 GitHub Actions 运行工作流程时,始终设置为 true 。 您可以使用此变量来区分测试是在本地运行还是通过 GitHub Actions 运行。 |
GITHUB_ACTOR | 发起工作流程的个人或应用程序的名称。 例如 octocat 。 |
GITHUB_REPOSITORY | 所有者和仓库名称。 例如 octocat/Hello-World 。 |
GITHUB_EVENT_NAME | 触发工作流程的 web 挂钩事件的名称。 |
GITHUB_EVENT_PATH | 具有完整 web 挂钩事件有效负载的文件路径。 例如 /github/workflow/event.json 。 |
GITHUB_WORKSPACE | GitHub 工作空间目录路径。 如果您的工作流程使用 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 | 仅为拉取请求事件设置。 基础分支的名称。 |
GITHUB_SERVER_URL | 返回 GitHub 服务器的 URL。 当 GitHub Actions 运行工作流程时,始终设置为 true 。 |
GITHUB_API_URL | 返回 API URL。 返回 GitHub 服务器的 URL。 例如:https://github.com 。 |
GITHUB_GRAPHQL_URL | 返回 GraphQL API URL。 例如:https://api.github.com/graphql 。 |
注:如果需要在作业中使用工作流程运行的 URL,您可以组合这些环境变量:$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
确定何时使用默认环境变量或上下文
GitHub Actions includes a collection of variables called contexts and a similar collection of variables called default environment variables. These variables are intended for use at different points in the workflow:
- Default environment variables: These variables exist only on the runner that is executing your job. 更多信息请参阅“默认环境变量”。
- Contexts: You can use most contexts at any point in your workflow, including when default environment variables would be unavailable. For example, you can use contexts with expressions to perform initial processing before the job is routed to a runner for execution; this allows you to use a context with the conditional
if
keyword to determine whether a step should run. Once the job is running, you can also retrieve context variables from the runner that is executing the job, such asrunner.os
. 更多信息请参阅“上下文”。
The following example demonstrates how these different types of environment variables can be used together in a job:
name: CI
on: push
jobs:
prod-check:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to production server on branch $GITHUB_REF"
In this example, the if
statement checks the github.ref
context to determine the current branch name; if the name is refs/heads/main
, then the subsequent steps are executed. The if
check is processed by GitHub Actions, and the job is only sent to the runner if the result is true
. Once the job is sent to the runner, the step is executed and refers to the $GITHUB_REF
environment variable from the runner.
环境变量命名约定
注: GitHub 会保留 GITHUB_
环境变量前缀供 GitHub 内部使用。 设置有 GITHUB_
前缀的环境变量或密码将导致错误。
您设置的指向文件系统上某个位置的任何新环境变量都应该有 _PATH
后缀。 HOME
和 GITHUB_WORKSPACE
默认变量例外于此约定,因为 "home" 和 "workspace" 一词已经暗示位置。