If condition not executing in github actions

Viewed 17

I want to run npm command based on environment. but it always execute if true section only.

workflow_dispatch:
    inputs:
      typeOfTesting:
        type: choice
        description: Select Type of Test
        default: "stage-test-Uptime"
        required: true
        options:
          - stage-test-Uptime
          - prod-test-Uptime
  schedule:
    - cron: "30 3 * * *"
    - cron: "30 13 * * *"

 steps:
      - name: Selection of Testing type
        run: |
          echo "Branch Name: ${{ github.event.inputs.typeOfTesting }}"
      - name: Install
        run: npm install
      - name: Test
        run: |

          if [ ${{ github.event.inputs.typeOfTesting == '' || github.event.inputs.typeOfTesting == 'stage-test-Uptime' }} ]; then
            echo "action_state=https://hooks.slack.com/services/XXX/XXX/XXX" >> $GITHUB_ENV
          else 
            echo "action_state=https://hooks.slack.com/services/YYY/YYY/YYY" >> $GITHUB_ENV
          fi
            npm run ${{ github.event.inputs.typeOfTesting == '' && github.event.inputs.typeOfTesting || 'stage-test-Uptime' }}

here it always execute true part of if condition.

1 Answers

try to resolve only github variables instead of the whole conditions, like:

      if [ ${{ github.event.inputs.typeOfTesting }} == '' || ${{ github.event.inputs.typeOfTesting}} == 'stage-test-Uptime' ]; then
        echo "action_state=https://hooks.slack.com/services/XXX/XXX/XXX" >> $GITHUB_ENV
      else 
        echo "action_state=https://hooks.slack.com/services/YYY/YYY/YYY" >> $GITHUB_ENV
      fi
Related