Skip to main content

このバージョンの GitHub Enterprise はこの日付をもって終了となりました: 2022-10-12. 重大なセキュリティの問題に対してであっても、パッチリリースは作成されません。 パフォーマンスの向上、セキュリティの向上、新機能の向上を図るために、最新バージョンの GitHub Enterprise にアップグレードします。 アップグレードに関するヘルプについては、GitHub Enterprise サポートにお問い合わせく� さい

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 ホステッド ランナーは、現在 GitHub Enterprise Server でサポートされていません。 GitHub public roadmap で、今後の計画的なサポートの詳細を確認できます。

About environment variables

You can use environment variables to store information that you want to reference in your workflow. You reference environment variables within a workflow step or an action, and the variables are interpolated on the runner machine that runs your workflow. Commands that run in actions or workflow steps can create, read, and modify environment variables.

You can set your own custom environment variables, you can use the default environment variables that GitHub sets automatically, and you can also use any other environment variables that are set in the working environment on the runner. Environment variables are case-sensitive.

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for:

name: Greeting on variable day

on:
  workflow_dispatch

env:
  DAY_OF_WEEK: Monday

jobs:
  greeting_job:
    runs-on: ubuntu-latest
    env:
      Greeting: Hello
    steps:
      - name: "Say Hello Mona it's Monday"
        run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"
        env:
          First_Name: Mona

The example above shows three custom environment variables being used in an echo command: $DAY_OF_WEEK, $Greeting, and $First_Name. The values for these environment variables are set, and scoped, at the workflow, job, and step level respectively.

Because environment variable interpolation is done after a workflow job is sent to a runner machine, you must use the appropriate syntax for the shell that's used on the runner. In this example, the workflow specifies ubuntu-latest. By default, Linux runners use the bash shell, so you must use the syntax $NAME. If the workflow specified a Windows runner, you would use the syntax for PowerShell, $env:NAME. For more information about shells, see "Workflow syntax for GitHub Actions."

Note: 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.

Using contexts to access environment variable values

In addition to environment variables, GitHub Actions also allows you to set and read values using contexts. Environment variables and contexts are intended for use at different points in the workflow.

Environment variables are always interpolated on the virtual machine runner. However, parts of a workflow are processed by GitHub Actions and are not sent to the runner. You cannot use environment variables in these parts of a workflow file. Instead, you can use contexts. For example, an if conditional, which determines whether a job or step is sent to the runner, is always processed by GitHub Actions. You can use a context in an if conditional statement to access the value of an environment variable.

env:
  DAY_OF_WEEK: Monday

jobs:
  greeting_job:
    runs-on: ubuntu-latest
    env:
      Greeting: Hello
    steps:
      - name: "Say Hello Mona it's Monday"
        if: ${{ env.DAY_OF_WEEK == 'Monday' }}
        run: echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"
        env:
          First_Name: Mona

In this modification of the first example, we've introduced an if conditional. The workflow step is now only run if DAYS_OF_WEEK is set to "Monday". We access this value from the if conditional statement by using the env context.

Note: Contexts are usually denoted using the dollar sign and curly braces, as ${{ context.property }}. In an if conditional, the ${{ and }} are optional, but if you use them they must enclose the entire comparison statement, as shown above.

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

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

There are many other contexts that you can use for a variety of purposes in your workflows. For more information, see "Contexts." For details of where you can use specific contexts within a workflow, see "Context availability."

Other types of variables

In most places in a workflow, the only types of variables that you can use are either environment variables, such as $MY_VARIABLE, or the equivalent context property, such as ${{ env.MY_VARIABLE }}. Exceptions are:

  • Inputs for the workflow_call and workflow_dispatch events, which allow you to pass values to a workflow. For more information, see on.workflow_call.inputs and on.workflow_dispatch.inputs.
  • Job outputs, which allow you to pass values between jobs in a workflow. For more information, see jobs.<job_id>.outputs.
  • The variables in a format expression, which allow you to replace parts of a string. For more information, see format.

Naming conventions for environment variables

When you set a custom environment variable, you cannot use any of the default environment variable names. For a complete list of these, see "Default environment variables" below. 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, GITHUB_ENV, and GITHUB_WORKSPACE default environment variables are exceptions to this convention.

Default environment variables

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

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_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 access files located in the same repository as the action. 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_API_URLReturns the API URL. For example: http(s)://HOSTNAME/api/v3.
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 environment variables from workflow commands. 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 "Workflow commands for 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: http(s)://HOSTNAME/api/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_PATHThe path on the runner to the file that sets system PATH variables from workflow commands. 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 "Workflow commands for GitHub Actions."
GITHUB_REFワークフローの実行をトリガーしたブランチまたはタグ ref。 push によってトリガーされるワークフローの� �合、これはプッシュされたブランチまたはタグの参照です。 pull_request によってトリガーされるワークフローの� �合、これは pull request のマージ ブランチです。 release によってトリガーされるワークフローの� �合、これは作成されたリリース タグです。 その他のトリガーの� �合、これはワークフロー実行をトリガーしたブランチまたはタグの参照です。 これは、イベントの種類に対してブランチまたはタグを使用できる� �合にのみ設定されます。 指定する参照は完全な形式です。つまり、ブランチの� �合の形式は refs/heads/<branch_name>、pull request の� �合は refs/pull/<pr_number>/merge、タグの� �合は refs/tags/<tag_name> です。 たとえば、「 refs/heads/feature-branch-1 」のように入力します。
GITHUB_REPOSITORYThe owner and repository name. For example, octocat/Hello-World.
GITHUB_REPOSITORY_OWNERThe repository owner's name. For example, octocat.
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 Enterprise Server server. For example: https://HOSTNAME.
GITHUB_SHAワークフローをトリガーしたコミット SHA。 このコミット SHA の値は、ワークフローをトリガーしたイベントによって異なります。 詳細については、「ワークフローをトリガーするイベント」を参照してく� さい。 たとえば、「 ffac537e6cbbf934b08745a378932722df287a53 」のように入力します。
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_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_DEBUGこれは、デバッグ ログが有効になっている� �合にのみ設定され、値は常に 1 です。 独自のジョブ手� �で追� のデバッグまたは詳細ログを有効にするためのインジケーターとして役に立ちます。
RUNNER_NAMEジョブを実行しているランナーの名前。 For example, Hosted Agent
RUNNER_OSジョブを実行しているランナーのオペレーティングシステ� 。 設定可能な値は、LinuxWindows、または macOS です。 For example, Windows
RUNNER_TEMPランナー上の一時ディレクトリへのパス。 このディレクトリは、各ジョブの開始及び終了時点で空になります。 ランナーのユーザアカウントが削除する権限を持っていない� �合、ファイルは削除されないことに注意してく� さい。 For example, D:\a\_temp
RUNNER_TOOL_CACHEGitHubホストランナーにプレインストールされているツールを含むディレクトリへのパス。 詳細については、「GitHub ホスト ランナーの概要」を参照してく� さい。 For example, C:\hostedtoolcache\windows

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
  • Most of the default environment variables have a corresponding, and similarly named, context property. For example, the value of the GITHUB_REF environment variable can be read during workflow processing using the ${{ github.ref }} context property.

Detecting the operating system

You can write a single workflow file that can be used for different operating systems by using the RUNNER_OS default environment variable and the corresponding context property ${{ runner.os }}. For example, the following workflow could be run successfully if you changed the operating system from macos-latest to windows-latest without having to alter the syntax of the environment variables, which differs depending on the shell being used by the runner.

jobs:
  if-Windows-else:
    runs-on: macos-latest
    steps:
      - name: condition 1
        if: runner.os == 'Windows'
        run: echo "The operating system on the runner is $env:RUNNER_OS."
      - name: condition 2
        if: runner.os != 'Windows'
        run: echo "The operating system on the runner is not Windows, it's $RUNNER_OS."

In this example, the two if statements check the os property of the runner context to determine the operating system of the runner. if conditionals are processed by GitHub Actions, and only steps where the check resolves as true are sent to the runner. Here one of the checks will always be true and the other false, so only one of these steps is sent to the runner. Once the job is sent to the runner, the step is executed and the environment variable in the echo command is interpolated using the appropriate syntax ($env:NAME for PowerShell on Windows, and $NAME for bash and sh on Linux and MacOS). In this example, the statement runs-on: macos-latest means that the second step will be run.

Passing values between steps and jobs in a workflow

If you generate a value in one step of a job, you can use the value in subsequent steps of the same job by assigning the value to an existing or new environment variable and then writing this to the GITHUB_ENV environment file. The environment file can be used directly by an action, or from a shell command in the workflow file by using the run keyword. For more information, see "Workflow commands for GitHub Actions."

If you want to pass a value from a step in one job in a workflow to a step in another job in the workflow, you can define the value as a job output. You can then reference this job output from a step in another job. For more information, see "Workflow syntax for GitHub Actions."