Bash condition not working

Hi,
I have a simple bash condition which works properly on my local terminal and another online terminal. However for some reason, it’s not working properly on github runner running on ubuntu-latest.

Here is the step:

    - name: Perform action
      shell: bash
      run: |  
          cluster_status=$(az aks list | jq -c 'map(select(any(.name; contains("dev-aks")))|.powerState)[].code')
          echo $cluster_status
          if [[ "$cluster_status" == "Stopped" && "${{ steps.set-action.outputs.action }}" == "stop" ]]; then
             echo 'cluster already stopped, exiting'
          elif [[ "$cluster_status" == "Started" && "${{ steps.set-action.outputs.action }}" == "start"  ]]; then 
              echo 'cluster already started, exiting'
          else
              az aks ${{ steps.set-action.outputs.action }} --name dev-aks --resource-group dev-rg
          fi

I echoed both variables in the condition:
${{ steps.set-action.outputs.action }} = stop
$cluster_status = Stopped

but I still getting the last command in the else executed. I even tried with

  if [[ "$cluster_status" == "Stopped" ]]; then

and it still doesn’t get in!

Bash version is 5.0.17(1)-release.

I’m not sure if that’s what’s causing the problem, but is there any particular reasons why you’re defining cluster_status as an array?

1 Like

I did define differently before it’s a copy paste of my last try lol. @airtower-luna .

I finally made it work. For anyone who gets the same problem here is the solution:
We need to create a step before using the value of cluster_status and set it in an evironnement variable.
Then, I don’t know why and I hope someone clarifies this for us, it works by using it in another step.

    - name: Get AKS status
      run: |
          echo "cluster_status=$(az aks list | jq -c 'map(select(any(.name; contains("dev-aks")))|.powerState)[].code')" >> $GITHUB_ENV         

    - name: Perform action
      run: |    
          if [[ ${{ env.cluster_status }} == "Stopped" && "${{ steps.set-action.outputs.action }}" == "stop" ]]; then
             echo 'cluster already stopped, exiting'
          elif [[ ${{ env.cluster_status }} == "Started" && "${{ steps.set-action.outputs.action }}" == "start"  ]]; then 
              echo 'cluster already stopped, exiting'
          else
              az aks ${{ steps.set-action.outputs.action }} --name dev-aks --resource-group dev-rg
          fi  
1 Like