GitHub Actions ${{expression}} 評価


GitHub Actionsの式の評価がドキュメントからいまいち分からなかったので検証しました。

# true、falseの評価

- 環境変数に対するtrue, falseは文字列型として扱われる

```yaml
  - name: bool test
        env:
          false-value1: false # falseの代わりに${{ false }} としても も同様の結果になる
        run: |
          # falseはbool
          echo 12 ${{ (false && 'result: true') || 'result: false' }} # result: false
          
          # 'false'は文字列
          echo 13 ${{ ('false' && 'result: true') || 'result: false' }} # result: true

          # env.false-value1 == 'false'
          echo 14 ${{ env.false-value1 && format('result: true {0}', env.false-value1) }}# result: true false
          echo 21 ${{ env.false-value1 == false }} # false
          echo 22 ${{ env.false-value1 == 'false' }} # true
```

- 0, 1と true, falseは互換性がある

```yaml
  - name: bool test-2
        env:
          one: 1
          zero: 0
        run: |
          echo 7 ${{ 1 == true }} # true
          echo 8 ${{ env.one == true }} # true
          echo 9 ${{ 1 == 'true' }} # false
          echo 10 ${{ env.one == 1 }} # true
          echo 11 ${{ env.one == '1' }} # true

          echo 16 ${{ 0 == false }} # true
          echo 17 ${{ 0 == 'false' }} # false
          echo 18 ${{ env.zero == false }} # true
          echo 19 ${{ env.zero == 0 }} # true
          echo 20 ${{ env.zero == '0' }} # true

          echo 24 ${{ 0 && 'result: true' }} # 0 (falseではなく0が返る)
          echo 25 ${{ '0' && 'result: true' }} # result: true
          echo 26 ${{ env.zero && 'result: true' }} # result: true
```
 
以下は検証した内容

```yaml
  - name: bool test-2
        env:
          true-value1: true
          true-value2: ${{ true }}
          false-value1: false
          false-value2: ${{ false }}
          one: 1
          zero: 0
        run: |
          # trueに対してtest
          echo 1 ${{ (true && 'result: true') || 'result: false' }} # result: true
          echo 2 ${{ env.true-value1 && format('result: true {0}', env.true-value1) }} # result: true true
          echo 3 ${{ env.true-value1 == true }} # false
          echo 4 ${{ env.true-value1 == 'true' }} # true
          echo 5 ${{ env.true-value1 == env.true-value2 }} # true
          echo 6 ${{ env.true-value2 && format('result: true {0}', env.true-value1) }} # result: true true
         
          echo 7 ${{ 1 == true }} # true
          echo 8 ${{ env.one == true }} # true
          echo 9 ${{ 1 == 'true' }} # false
          echo 10 ${{ env.one == 1 }} # true
          echo 11 ${{ env.one == '1' }} # true
          
          echo 11-2 ${{ 1 && 'result: true' }} # result: true
          # falseに対してtest   
          echo 12 ${{ (false && 'result: true') || 'result: false' }} # result: false
          echo 13 ${{ ('false' && 'result: true') || 'result: false' }} # result: true
          echo 14 ${{ env.false-value1 && format('result: true {0}', env.false-value1) }}# result: true false
          echo 15 ${{ env.false-value2 && format('result: true {0}', env.false-value1) }}# result: true false
          echo 16 ${{ 0 == false }} # true
          echo 17 ${{ 0 == 'false' }} # false
          echo 18 ${{ env.zero == false }} # true
          echo 19 ${{ env.zero == 0 }} # true
          echo 20 ${{ env.zero == '0' }} # true
          echo 21 ${{ env.false-value1 == false }} # false
          echo 22 ${{ env.false-value1 == 'false' }} # true
          echo 23 ${{ env.false-value1 == env.false-value2 }} # true
          echo 24 ${{ 0 && 'result: true' }} # 0
          echo 25 ${{ '0' && 'result: true' }} # result: true
          echo 26 ${{ env.zero && 'result: true' }} # result: true
```

# expression中の文字列連結

[format](https://docs.github.com/ja/actions/reference/context-and-expression-syntax-for-github-actions#functions)を利用する

```yaml
${{ format('result: {0} {1}', env.true-value1, 'test') }} # result true, test
```

# 参考

https://docs.github.com/ja/actions/reference/context-and-expression-syntax-for-github-actions