Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolean env var in Github actions

I have a boolean env var TAG_EVENT and I update it in one of the steps to false (I also print it and I see it false) but for some reason, the last step is not executed although TAG_EVENT is false. I appreciate help with that,

on:
 workflow_dispatch:

 env:
 TAG_EVENT: ${{ true }}

 jobs:
   push_images:
   name: Push images
   runs-on: ubuntu-latest
   if: ${{ github.event_name != 'pull_request' }}
   steps:
   - id: version
     name: Infer version
     run: |
        version="${GITHUB_REF#refs/tags/v}"
        echo $version
        if  [[ $version == refs/* ]] ;
         then
           echo 'TAG_EVENT=false' >> $GITHUB_ENV
           branch="${GITHUB_REF#refs/heads/}"
           version=$branch

       fi
       echo ::set-output name=version::$version
   - name: Publish latest image tag for release
     if: github.event_name != 'pull_request' && TAG_EVENT == false
     run: |
       echo "printme!!!"
like image 726
erez Avatar asked Jul 17 '26 07:07

erez


2 Answers

Thanks to @zswqa answer this worked for me

env:    
  RUN_TESTS: ${{ true }}

Then in steps

- name: Gradle Test
  if: ${{ env.RUN_TESTS == 'true' }}
  run: gradle test

if I set RUN_TESTS to false then the test step doesn't run.

like image 73
rupweb Avatar answered Jul 21 '26 17:07

rupweb


As you make use of a text-based shell like bash, you will have to deal with strings. Therefore you wont be able to do a check for false, but need to do it for 'false'. But I would suggest marshaling the boolean by using GitHub's built-in toJSON and fromJSON functions:

...
steps:
  - name: I produce a boolean output
    id: output_producer
    shell: bash
    run: |
      if [[ $RANDOM > 100 ]]; then i=true; else i=false; fi
      echo "::set-output name=boolean_output::${{ toJSON($i) }}"
      echo "::set-output name=integer_output::${{ toJSON($i) }}"
  - name: I run conditionally on that boolean
    if: fromJSON(steps.output_producer.outputs.boolean_output) 
    shell: bash
    run: |
      echo "Ran successfully!"
  - name: I run always
    if: always()
    shell: bash
    run: |
      echo ${{ fromJSON(steps.output_producer.outputs.integer_output) }}
like image 25
flipcc Avatar answered Jul 21 '26 15:07

flipcc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!