Environment variables

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

注意: GitHub Actions 目前正在测试用于 GitHub AE 。

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. For more information, see "Workflow syntax for 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

To use the value of an environment variable in a workflow file, you should use the env context. If you want to use the value of an environment variable inside a runner, you can use the runner operating system's normal method for reading environment variables.

If you use the workflow file's run key to read environment variables from within the runner operating system (as shown in the example above), the variable is substituted in the runner operating system after the job is sent to the runner. For other parts of a workflow file, you must use the env context to read environment variables; this is because workflow keys (such as if) require the variable to be substituted during workflow processing before it is sent to the runner.

You can also use the GITHUB_ENV environment file to set an environment variable that the following steps in a job can use. The environment file 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 Actions."

Default environment variables

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.

Environment variableDescription
CIAlways set to true.
GITHUB_WORKFLOWThe name of the workflow.
GITHUB_RUN_ID仓库中每个运行的唯一编号。 如果您重新执行工作流程运行,此编号不变。
GITHUB_RUN_NUMBER仓库中特定工作流程每个运行的唯一编号。 此编号从 1(对应于工作流程的第一个运行)开始,然后随着每个新的运行而递增。 如果您重新执行工作流程运行,此编号不变。
GITHUB_JOBThe job_id of the current job.
GITHUB_ACTIONThe unique identifier (id) of the action.
GITHUB_ACTION_PATHThe path where your action is located. You can use this path to access files located in the same repository as your action. This variable is only supported in composite actions.
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_REPOSITORYThe owner and repository name. For example, octocat/Hello-World.
GITHUB_EVENT_NAMEThe name of the webhook event that triggered the workflow.
GITHUB_EVENT_PATHThe path of the file with the complete webhook event payload. For example, /github/workflow/event.json.
GITHUB_WORKSPACEThe GitHub workspace directory path, initially empty. For example, /home/runner/work/my-repo-name/my-repo-name. The actions/checkout action will check out files, by default a copy of your repository, within this directory.
GITHUB_SHAThe commit SHA that triggered the workflow. For example, ffac537e6cbbf934b08745a378932722df287a53.
GITHUB_REFThe branch or tag ref that triggered the workflow. For example, refs/heads/feature-branch-1. If neither a branch or tag is available for the event type, the variable will not exist.
GITHUB_HEAD_REFOnly set for pull request events. The name of the head branch.
GITHUB_BASE_REFOnly set for pull request events. The name of the base branch.
GITHUB_SERVER_URLReturns the URL of the GitHub AE server. For example: https://[hostname].
GITHUB_API_URLReturns the API URL. For example: https://[hostname]/api/v3.
GITHUB_GRAPHQL_URLReturns the GraphQL API URL. For example: https://api.[hostname]/graphql.
RUNNER_NAMEThe name of the runner executing the job.
RUNNER_OS执行作业的运行器的操作系统。 可能的值为 LinuxWindowsmacOS
RUNNER_TEMP运行器临时目录的路径。 此目录在每个作业的开始和结束时都是空的。 注意,如果运行者的用户帐户没有权限删除这些文件,则不会被删除。

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

Determining when to use default environment variables or contexts

GitHub Actions 包含一个称为上下文的变量集和一个称为默认环境变量的类似变量集。 这些变量预期用于工作流程中的不同点:

  • 默认环境变量:这些变量仅存在于执行作业的运行器上。 更多信息请参阅“默认环境变量”。
  • 上下文:您可以在工作流程的任何时候使用大多数上下文,包括当默认环境变量不可用时。 例如,您可以使用带表达式的上下文执行初始处理,然后将作业路由到运行器以供执行;这允许您使用带有条件 if 关键字的上下文来确定步骤是否应运行。 作业运行后,您还可以从执行作业的运行器(如 runner.os)检索上下文变量。 有关您可以在工作流程中使用各种上下文的详细信息,请参阅“上下文可用性”。

下面的示例演示了这些不同类型的环境变量如何在一个作业中一起使用:

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"

在此示例中,if 语句检查 github.ref 上下文以确定当前分支名称;如果名称是 refs/heads/main,则执行后续步骤。 if 检查由 GitHub Actions 处理,作业仅在结果为 true 时才发送到运行器。 作业发送到运行器后, 该步骤将执行并引用运行器的 $GITHUB_REF 环境变量。

Naming conventions for environment variables

When you set a custom environment variable, you cannot use any of the default environment variable names listed above with the prefix GITHUB_. If you attempt to override the value of one of these default environment variables, the assignment is ignored.

Any new environment variables you set that point to a location on the filesystem should have a _PATH suffix. The HOME and GITHUB_WORKSPACE default variables are exceptions to this convention because the words "home" and "workspace" already imply a location.

此文档对您有帮助吗?

隐私政策

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或者, 了解如何参与。