注釈: GitHub Enterprise Server 2.22 での GitHub Actions サポートは、限定パブリックベータです。 To review the external storage requirements and request access to the beta, see "Getting started with GitHub Actions for GitHub Enterprise Server."
Note: GitHub-hosted runners are not currently supported on GitHub Enterprise Server. You can see more information about planned future support on the GitHub public roadmap.
環境変数について
GitHubは、ワークフローの実行におけるどのステップでも使用できる、デフォルトの環境変数を設定します。 環境変数では、大文字小文字は区別されます。 アクションあるいはステップ内で実行されるコマンドは、環境変数を作成、読み取り、変更することができます。
カスタムの環境変数を設定するには、ワークフローファイル中でその変数を指定しなければなりません。 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
set-env
ワークフローコマンド を使用して、ワークフローの次の手順で使用できる環境変数を設定することもできます。 set-env
コマンド は、アクションによって直接使用することも、run
キーワードを使用してワークフローファイルのシェルコマンドとして使用することもできます。 詳しい情報については「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 | ワークフローをトリガーしたwebhookイベントの名前。 |
GITHUB_EVENT_PATH | 完了したwebhookイベントペイロードのファイルのパス。 /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 | ワークフローをトリガーしたブランチまたはタグref。 たとえば、refs/heads/feature-branch-1 です。 イベントタイプのブランチもタグも利用できない場合、変数は存在しません。 |
GITHUB_HEAD_REF | Only set for pull request events. The name of the head branch. |
GITHUB_BASE_REF | Only set for pull request events. The name of the base branch. |
GITHUB_SERVER_URL | GitHub Enterprise Server サーバーの URL を返します。 For example: https://github.com . |
GITHUB_API_URL | API URL を返します。 For example: https://api.github.com . |
GITHUB_GRAPHQL_URL | グラフ QL API の URL を返します。 For example: https://api.github.com/graphql . |
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 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.
環境変数の命名規則
Note: GitHub reserves the GITHUB_
environment variable prefix for internal use by GitHub. GITHUB_
プレフィックスを使用して環境変数またはシークレットを設定すると、エラーになります。
ファイルシステム上の場所を指すように設定した新しい環境変数がある場合は、_PATH
サフィックスを指定する必要があります。 デフォルトの変数HOME
とGITHUB_WORKSPACE
は、「home」および「workspace」という言葉で最初から場所であることがわかっているため、この規則の例外です。