3

We have two runners, one for running production jobs and another for running non production jobs, but I am unable to do that using a workflow level environment variable.

Below is what I have:

name: Workflow file

on:
  workflow-dispatch

env:
 RUNNER_NAME: ${{ contains(github.ref, 'main') && 'Prod Runner' || 'non-Prod Runner' }}

jobs:
  job-run:
    runs-on: [${{ env.RUNNER_NAME }}]
    needs: ...
    steps:
      ..........

I get the following error message:

Invalid workflow file

You have an error in your yaml syntax on line ###

How do I do this? I don't want to have separate workflow files for prod and non-prod workflows.

Keith C
  • 51
  • 2
  • 3
  • How does this expression work exactly: `${{ contains(github.ref, 'main') && 'Prod Runner' || 'non-Prod Runner' }}`? What are the expected outputs? – GuiFalourd Apr 22 '22 at 16:38
  • I figured this out afterwards using ```runs-on: ${{ contains(github.ref, 'main') && 'Prod Runner' || 'Cert Runner' }}``` as it picks the Production Runner if I'm on the main branch and the Cert Runner in all other cases. – Keith C Apr 22 '22 at 17:17
  • Ok, I've made some tests and shared my answer below – GuiFalourd Apr 22 '22 at 18:33

1 Answers1

6

For what you can check on this Github Actions ISSUE, it seems it's not possible to use natively env variable on the runs-on job field (yet?).


However, there is a workaround if you configure a variable as output in a previous job, so you would be able to use it afterwards.

Example: runs-on: ${{ needs.setup.outputs.runner }}

In your case, the workflow would look like this:

on:
  workflow_dispatch:

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      runner: ${{ steps.step1.outputs.runner }}
    steps:
      - name: Check branch
        id: step1
        run: |
          if [ ${{ github.ref }} == 'refs/heads/main' ]; then
            echo "runner=ubuntu-latest" >> $GITHUB_OUTPUT
          else
            echo "runner=macos-latest" >> $GITHUB_OUTPUT
          fi

  job1:
    needs: [setup]
    runs-on: ${{ needs.setup.outputs.runner }}
    steps:
      - run: echo "My runner is ${{ needs.setup.outputs.runner }}" #ubuntu-latest if main branch

I've made a test here if you want to have a look:

GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
  • 1
    This trick does not work if you want to use multiple labels. – evilfish Mar 06 '23 at 09:11
  • This workaround won't work after 31.05.2023 https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ – maximkrouk Apr 05 '23 at 14:40
  • 2
    I've updated the answer to use the new syntax to set outputs. Thanks for noticing @maximkrouk – GuiFalourd Apr 05 '23 at 16:09
  • As I think @evilfish is already trying to say this (unfortunately) does not seem to work with a strategy-matrix for the runners (as somewhat stated elsewhere by @GuiFalourd). Or if I'm wrong, I would love to see a version of that. I've tried something like 'strategy: matrix: fromJson(needs.setup.outputs.runners)'. – AudioDroid Apr 25 '23 at 10:33
  • How did you generate the output in that case @AudioDroid? (If you want to open another question with that scenario, we can perform some tests and evaluate how to make it work!) – GuiFalourd Apr 25 '23 at 18:25