Boolean based on workflow conditions

How would you go about creating a boolean variable, which switches its value based on certain conditions ?

The variable is used inside a script in the workflow.

I’ve came up with this solution through Powershell:

      - name: Determine if signing should be applied to AppxBundle
        id: determine_signing
        shell: powershell
        run: |
          [bool]$PARAM = $false
          if ( ($env:GITHUB_EVENT_NAME -ceq 'push') -and ($env:GITHUB_REF -ceq 'refs/heads/master') -and ($env:BUILD_CONFIGURATION -ceq 'Release') ) {
            $PARAM = $true
          }
          echo "::set-output name=parameter::$(echo $PARAM)"
        env:
          $GITHUB_EVENT_NAME: ${{ github.event_name }}
          $GITHUB_REF: ${{ github.ref }}
          $BUILD_CONFIGURATION: ${{ matrix.configuration }}

And of course it works, but I was thinking about a solution that is YAML only. The thing is, I can’t think of any :smiley:

Any ideas ?

You mean like this?

- name: Run on push to master if build config is Release
  if: github.event_name == 'push' && github.ref == 'refs/heads/master' && env.BUILD_CONFIGURATION == 'Release'
  run: |
    echo "The conditions were met"

github context: Context and expression syntax for GitHub Actions - GitHub Docs
env context: Context and expression syntax for GitHub Actions - GitHub Docs

env.BUILD_CONFIGURATION will probably only be available if you set it via env: ... in the workflow itself, not if you set it in a run: ... block like export BUILD_CONFIGURATION="Release". You can set an env key from within a run block with a workflow command if necessary, however, e.g. like echo "BUILD_CONFIGURATION=Release" >> $GITHUB_ENV using bash shell (also see Workflow commands for GitHub Actions - GitHub Docs).

It was quite some time ago, but I think I meant grouping conditions inside a single variable.

env:
  condition: github.event_name == 'push' && github.ref == 'refs/heads/master' && env.BUILD_CONFIGURATION == 'Release'

Several months later, I don’t think that’s possible in YAML, but correct me if I am wrong.

1 Like

Well, you could evaluate an expression in place, like this:

env:
  condition: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && env.BUILD_CONFIGURATION == 'Release' }}

But the env context is only available in steps, so the third condition wouldn’t be possible like this.

Or do you mean to defer evaluation? As in have github.event_name == 'push' && github.ref == 'refs/heads/master' && env.BUILD_CONFIGURATION == 'Release' as string and then evaluate that string in certain places of the workflow, something along ${{ eval(env.condition) }}? That is definitely not supported, and I would guess it’s very unlikely to ever get supported because of security concerns.

1 Like

That’s it!

Thank you!

1 Like