About contexts
警告: ワークフローやアクションを作る際には、攻撃者からの信頼できない入力をコードが実行するかもしれないことを、常に意識しなければなりません。 攻撃者が悪意あるコンテンツを挿入してくるかもしれないので、特定のコンテキストは信頼できない入力として扱うべきです。 詳しい情報については「スクリプトインジェクションのリスクを理解する」を参照してください。
Contexts are a way to access information about workflow runs, runner environments, jobs, and steps. Contexts use the expression syntax. For more information, see "Expressions."
${{ <context> }}
| Context name | Type | Description |
|---|---|---|
github | object | Information about the workflow run. For more information, see github context. |
env | object | Contains environment variables set in a workflow, job, or step. For more information, see env context. |
job | object | Information about the currently executing job. For more information, see job context. |
steps | object | Information about the steps that have been run in this job. For more information, see steps context. |
runner | object | Information about the runner that is running the current job. For more information, see runner context. |
secrets | object | Enables access to secrets. For more information about secrets, see "Creating and using encrypted secrets." |
strategy | object | Enables access to the configured strategy parameters and information about the current job. Strategy parameters include fail-fast, job-index, job-total, and max-parallel. |
matrix | object | Enables access to the matrix parameters you configured for the current job. For example, if you configure a matrix build with the os and node versions, the matrix context object includes the os and node versions of the current job. |
needs | object | Enables access to the outputs of all jobs that are defined as a dependency of the current job. For more information, see needs context. |
inputs | object | Enables access to the inputs of reusable workflow. For more information, see inputs context. |
As part of an expression, you may access context information using one of two syntaxes.
- Index syntax:
github['sha'] - Property dereference syntax:
github.sha
In order to use property dereference syntax, the property name must:
- start with
a-Zor_. - be followed by
a-Z0-9-or_.
Determining when to use 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をランナーから参照します。
github context
The github context contains information about the workflow run and the event that triggered the run. You can read most of the github context data in environment variables. For more information about environment variables, see "Using environment variables."
警告: githubコンテキスト全体を使う場合には、github.tokenのようなセンシティブな情報が含まれていることを忘れないようにしてください。 GitHubは、シークレットがコンソールに出力される際にはマスクしますが、コンテキストをエクスポートしたりプリントしたりするときには注意が必要です。
警告: ワークフローやアクションを作る際には、攻撃者からの信頼できない入力をコードが実行するかもしれないことを、常に意識しなければなりません。 攻撃者が悪意あるコンテンツを挿入してくるかもしれないので、特定のコンテキストは信頼できない入力として扱うべきです。 詳しい情報については「スクリプトインジェクションのリスクを理解する」を参照してください。
| Property name | Type | Description |
|---|---|---|
github | object | The top-level context available during any job or step in a workflow. |
github.action | string | The name of the action currently running. GitHub removes special characters or uses the name __run when the current step runs a script. If you use the same action more than once in the same job, the name will include a suffix with the sequence number with underscore before it. 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_path | string | The path where your action is located. You can use this path to easily access files located in the same repository as your action. This attribute is only supported in composite actions. |
github.actor | string | The login of the user that initiated the workflow run. |
github.base_ref | string | The base_ref or target branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target. |
github.event | object | The full event webhook payload. For more information, see "Events that trigger workflows." You can access individual properties of the event using this context. |
github.event_name | string | The name of the event that triggered the workflow run. |
github.event_path | string | The path to the full event webhook payload on the runner. |
github.head_ref | string | The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target. |
github.job | string | The job_id of the current job. |
github.ref | string | The branch or tag ref that triggered the workflow run. For branches this is the format refs/heads/<branch_name>, and for tags it is refs/tags/<tag_name>. |
github.ref_name | string | The branch or tag name that triggered the workflow run. |
github.ref_protected | string | true if branch protections are configured for the ref that triggered the workflow run. |
github.ref_type | string | The type of ref that triggered the workflow run. Valid values are branch or tag. |
github.repository | string | The owner and repository name. For example, Codertocat/Hello-World. |
github.repository_owner | string | The repository owner's name. For example, Codertocat. |
github.run_id | string | リポジトリ内でユニークな各実行に対する番号。 この番号は、ワークフローの実行をやり直しても変化しません、 |
github.run_number | string | リポジトリ内の特定のワークフローの各実行に対するユニークな番号。 この番号は、ワークフローの最初の実行時に1で始まり、新たな実行ごとにインクリメントされます。 この番号は、ワークフローの実行をやり直しても変化しません、 |
github.run_attempt | string | A 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. |
github.server_url | string | Returns the URL of the GitHub server. For example: https://github.com. |
github.sha | string | The commit SHA that triggered the workflow run. |
github.token | string | A token to authenticate on behalf of the GitHub App installed on your repository. This is functionally equivalent to the GITHUB_TOKEN secret. For more information, see "Authenticating with the GITHUB_TOKEN." |
github.workflow | string | The name of the workflow. If the workflow file doesn't specify a name, the value of this property is the full path of the workflow file in the repository. |
github.workspace | string | The default working directory for steps and the default location of your repository when using the checkout action. |
env context
The env context contains environment variables that have been set in a workflow, job, or step. For more information about setting environment variables in your workflow, see "Workflow syntax for GitHub Actions."
The env context syntax allows you to use the value of an environment variable in your workflow file. You can use the env context in the value of any key in a step except for the id and uses keys. For more information on the step syntax, see "Workflow syntax for GitHub Actions."
If you want to use the value of an environment variable inside a runner, use the runner operating system's normal method for reading environment variables.
| Property name | Type | Description |
|---|---|---|
env | object | This context changes for each step in a job. You can access this context from any step in a job. |
env.<env_name> | string | The value of a specific environment variable. |
job context
The job context contains information about the currently running job.
| Property name | Type | Description |
|---|---|---|
job | object | This context changes for each job in a workflow run. You can access this context from any step in a job. |
job.container | object | Information about the job's container. For more information about containers, see "Workflow syntax for GitHub Actions." |
job.container.id | string | The id of the container. |
job.container.network | string | The id of the container network. The runner creates the network used by all containers in a job. |
job.services | object | The service containers created for a job. For more information about service containers, see "Workflow syntax for GitHub Actions." |
job.services.<service id>.id | string | The id of the service container. |
job.services.<service id>.network | string | The id of the service container network. The runner creates the network used by all containers in a job. |
job.services.<service id>.ports | object | The exposed ports of the service container. |
job.status | string | The current status of the job. Possible values are success, failure, or cancelled. |
steps context
The steps context contains information about the steps in the current job that have already run.
| Property name | Type | Description |
|---|---|---|
steps | object | This context changes for each step in a job. You can access this context from any step in a job. |
steps.<step id>.outputs | object | The set of outputs defined for the step. For more information, see "Metadata syntax for GitHub Actions." |
steps.<step id>.conclusion | string | The result of a completed step after continue-on-error is applied. Possible values are success, failure, cancelled, or skipped. When a continue-on-error step fails, the outcome is failure, but the final conclusion is success. |
steps.<step id>.outcome | string | The result of a completed step before continue-on-error is applied. Possible values are success, failure, cancelled, or skipped. When a continue-on-error step fails, the outcome is failure, but the final conclusion is success. |
steps.<step id>.outputs.<output name> | string | The value of a specific output. |
runner context
The runner context contains information about the runner that is executing the current job.
| Property name | Type | Description |
|---|---|---|
runner.name | string | The name of the runner executing the job. |
runner.os | string | ジョブを実行しているランナーのオペレーティングシステム。 取り得る値はLinux、Windows、macOSのいずれか。 |
runner.arch | string | The architecture of the runner executing the job. Possible values are X86, X64, ARM, and ARM64. |
runner.temp | string | ランナー上の一時ディレクトリへのパス。 このディレクトリは、各ジョブの開始及び終了時点で空になります。 ランナーのユーザアカウントが削除する権限を持っていない場合、ファイルは削除されないことに注意してください。 |
runner.tool_cache | string | GitHubホストランナーにプレインストールされているツールを含むディレクトリへのパス。 詳しい情報については、「GitHub ホストランナーの仕様」を参照してください。 |
needs context
The needs context contains outputs from all jobs that are defined as a dependency of the current job. For more information on defining job dependencies, see "Workflow syntax for GitHub Actions."
| Property name | Type | Description |
|---|---|---|
needs.<job id> | object | A single job that the current job depends on. |
needs.<job id>.outputs | object | The set of outputs of a job that the current job depends on. |
needs.<job id>.outputs.<output name> | string | The value of a specific output for a job that the current job depends on. |
needs.<job id>.result | string | The result of a job that the current job depends on. Possible values are success, failure, cancelled, or skipped. |
inputs context
The inputs context contains information about the inputs of reusable workflow. The inputs are defined in workflow_call event configuration. These inputs are passed from jobs.<job_id>.with in an external workflow.
For more information, see "Reusing workflows".
| Property name | Type | Description |
|---|---|---|
inputs | object | This context is only available when it is a reusable workflow. |
inputs.<name> | string or number or boolean | Each input value passed from an external workflow. |
Example printing context information to the log file
To inspect the information that is accessible in each context, you can use this workflow file example.
警告: githubコンテキスト全体を使う場合には、github.tokenのようなセンシティブな情報が含まれていることを忘れないようにしてください。 GitHubは、シークレットがコンソールに出力される際にはマスクしますが、コンテキストをエクスポートしたりプリントしたりするときには注意が必要です。
.github/workflows/main.yml
on: push
jobs:
one:
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: echo "$GITHUB_CONTEXT"
- name: Dump job context
env:
JOB_CONTEXT: ${{ toJSON(job) }}
run: echo "$JOB_CONTEXT"
- name: Dump steps context
env:
STEPS_CONTEXT: ${{ toJSON(steps) }}
run: echo "$STEPS_CONTEXT"
- name: Dump runner context
env:
RUNNER_CONTEXT: ${{ toJSON(runner) }}
run: echo "$RUNNER_CONTEXT"
- name: Dump strategy context
env:
STRATEGY_CONTEXT: ${{ toJSON(strategy) }}
run: echo "$STRATEGY_CONTEXT"
- name: Dump matrix context
env:
MATRIX_CONTEXT: ${{ toJSON(matrix) }}
run: echo "$MATRIX_CONTEXT"
Context availability
Different contexts are available throughout a workflow run. For example, the secrets context may only be used at certain places within a job.
In addition, some functions may only be used in certain places. For example, the hashFiles function is not available everywhere.
The following table indicates where each context and special function can be used within a workflow. Unless listed below, a function can be used anywhere.
| Path | Context | Special functions |
|---|---|---|
concurrency | github, inputs | |
env | github, secrets, inputs | |
jobs.<job_id>.concurrency | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.container | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.container.credentials | github, needs, strategy, matrix, env, secrets, inputs | |
jobs.<job_id>.container.env.<env_id> | github, needs, strategy, matrix, job, runner, env, secrets, inputs | |
jobs.<job_id>.continue-on-error | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.defaults.run | github, needs, strategy, matrix, env, inputs | |
jobs.<job_id>.env | github, needs, strategy, matrix, secrets, inputs | |
jobs.<job_id>.environment | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.environment.url | github, needs, strategy, matrix, job, runner, env, steps, inputs | |
jobs.<job_id>.if | github, needs, inputs | always, cancelled, success, failure |
jobs.<job_id>.name | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.outputs.<output_id> | github, needs, strategy, matrix, job, runner, env, secrets, steps, inputs | |
jobs.<job_id>.runs-on | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.secrets.<secrets_id> | github, needs, secrets | |
jobs.<job_id>.services | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.services.<service_id>.credentials | github, needs, strategy, matrix, env, secrets, inputs | |
jobs.<job_id>.services.<service_id>.env.<env_id> | github, needs, strategy, matrix, job, runner, env, secrets, inputs | |
jobs.<job_id>.steps.continue-on-error | github, needs, strategy, matrix, job, runner, env, secrets, steps | hashFiles |
jobs.<job_id>.steps.env | github, needs, strategy, matrix, job, runner, env, secrets, steps, inputs | hashFiles |
jobs.<job_id>.steps.if | github, needs, strategy, matrix, job, runner, env, steps, inputs | always, cancelled, success, failure, hashFiles |
jobs.<job_id>.steps.name | github, needs, strategy, matrix, job, runner, env, secrets, steps, inputs | hashFiles |
jobs.<job_id>.steps.run | github, needs, strategy, matrix, job, runner, env, secrets, steps, inputs | hashFiles |
jobs.<job_id>.steps.timeout-minutes | github, needs, strategy, matrix, job, runner, env, secrets, steps | hashFiles |
jobs.<job_id>.steps.with | github, needs, strategy, matrix, job, runner, env, secrets, steps, inputs | hashFiles |
jobs.<job_id>.steps.working-directory | github, needs, strategy, matrix, job, runner, env, secrets, steps, inputs | hashFiles |
jobs.<job_id>.strategy | github, needs, inputs | |
jobs.<job_id>.timeout-minutes | github, needs, strategy, matrix, inputs | |
jobs.<job_id>.with.<with_id> | github, needs | |
on.workflow_call.inputs.<inputs_id>.default | github | |
on.workflow_call.outputs.<output_id>.value | github, jobs, inputs |