LoginSignup
3

More than 3 years have passed since last update.

posted at

updated at

GitHub Actions で3項演算子を実現する

はじめに

演算子には今のところ3項演算子はありませんが3項演算子のようなことは容易に実現可能です。

対象

GitHub Actionsを利用している開発者

実装

AND 演算子と OR 演算子を使用して以下のように指定します。

(条件 && 条件がtrueの場合の値) || 条件がfalseの場合の値

実装例

以下のような実装でイベントごとに異なる変数をアクションに渡すことができます。

.github/workflows/test.yml
on:
  pull_request:
  push:

name: Test
jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Test
        uses: ./
        with:
          test: ${{ (github.event_name == 'pull_request' && 'test1') || 'test2' }}
action.yml
name: Test
description: Test
author: technote-space
inputs:
  test:
    description: test
    required: true
runs:
  using: node12
  main: lib/main.js
lib/main.js
console.log('eventName: ', process.env.GITHUB_EVENT_NAME);
console.log('process.env.INPUT_TEST: ', process.env.INPUT_TEST);

動作

202001192227.png
202001192230.png

注意

条件がtrueの場合の値に空の文字列を渡したい場合、条件を逆にする必要があります。

(!条件 && 条件がfalseの場合の値) || ''

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
What you can do with signing up
3