14
4

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 で3項演算子を実現する

Last updated at Posted at 2020-01-19

はじめに

演算子には今のところ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の場合の値) || ''
14
4
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
14
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?