'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!!!"
Solution 1:[1]
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) }}
Solution 2:[2]
Try to use in second if-statement ${{ env.TAG_EVENT == false }}
Solution 3:[3]
There is some inconsistency between the input context and the way booleans are treated in GitHub Actions. I have a short write-up on this. Hope you find this helpful
GitHub Actions: Passing Boolean input variables to reusable workflow_call
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | flipcc |
| Solution 2 | zswqa |
| Solution 3 | Sohailra |
