0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHub Actions: Expression Syntax でトリガーイベントの情報を取得する (branch 名、tag 名、PR 番号)

Last updated at Posted at 2022-02-04

GitHub Actions 実行時には Expression Syntax を使ってビルド情報にアクセスできます。

Expression Syntax ${{ }}

GitHub Actions では、特定の場所で Expression Syntax を使用できます。

Expression Syntax は値を記述する箇所ではほとんどの場所で使えると思われます。

if:

steps:
  - uses: actions/...
    if: ${{ <expression> }}

env:

env:
  MY_ENV: ${{ <expression> }}

その他 (例: run:)

この例ではシェルが解釈する前に expression の結果が文字列として置換されることになります。

steps:
  - run: |
    echo ${{ <expression> }}

Contexts

Expression の中では GitHub Actions の環境情報として Contexts へアクセスできます。

Context 名 説明
github Workflow やトリガー情報
env 環境変数
job 実行中の Job 情報
steps 実行中の step 情報、step の結果へのアクセスなど
runner 実行中の runner (実行ホストマシン) 情報
secrets Github Actions Secrets
matrix Workflow の Matrix 情報
needs 実行中の Job が依存する他の Job の情報
inputs Reusable Workflow の実行パラメーター

トリガー情報は github コンテキストに含まれています。

github.event

github.event にはその Workflow のトリガー情報が含まれます。

トリガーの種類によって github.event の payload は異なります。github.event の種類は以下のドキュメントを参照してください。

PR 番号の取得

pull_request トリガーのときに取得できます。

#...
on: pull_request
#...
steps:
  - run: |
    echo ${{ github.event.number }}

pull_request のときの、github.context payload はこちらです。

branch 名または tag 名の取得

push トリガーのときに取得できます。

#...
on:
  push:
    branches:
      - main
#    tags:
#      - release/**
#...
steps:
  - run: |
    echo ${{ github.ref_name }}

ref_name の説明は github context のドキュメントを参照してください。

その他

よく使うもの

項目 説明
github.sha Workflow のコミットハッシュ。PR トリガーのときは PR 先とマージされたコミット
github.event.pull_request.head.ref PR トリガーのとき、PR 先とマージされる前のコミットハッシュ
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?