githubgithub-actionsgithub-actions-self-hosted-runners

Is there a way to determine in a GitHub Actions workflow step if the current runner is self-hosted?


I have set up a self-hosted runner for GitHub Actions. It works fine but I have a step that uses setup-xcode-version and this needs sudo access. So when it gets to this step the terminal running the GitHub Actions runner script prompts me for a password, which holds up the workflow until I enter it.

I've seen there are ways to make sudo passwordless but I don't actually need this step on my self-hosted runner because I control what version of Xcode is available. So for me a simpler and more secure option would be to just check the current runner is self-hosted or not and skip the step accordingly.

e.g. something that looks like this:

- name: Xcode
  id: setup_xcode
  if: contains(runner.labels, 'self-hosted')
  uses: maxim-lobanov/setup-xcode@v1
    with:
      xcode-version: 14.3.1

Unfortunately it looks like the runner context does not include any labels. Is there some way I can achieve this check?

My runner is Apple Silicon whereas right now GitHub Actions macOS runners will presumably always be Intel, but that's not reliable enough. I suppose I could ensure my self-hosted runner names all contain a "self-hosted" keyword and check for that, but that's a bit of a hack. For now this is what I'm using and it seems to work but it relies on people remembering to include the right keyword in the runner name:

${{ !contains(runner.name, 'MyKeyword') }}

I also asked this on GitHub.


Solution

  • According the github context reference, there are no labels property in the runner object.

    But you can use the environment property like this:

      - name: Cache NuGet packages
        if: runner.environment != 'self-hosted'
        uses: actions/cache@v4
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
          restore-keys: |
            ${{ runner.os }}-nuget-
    

    https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#runner-context

    The environment of the runner executing the job. Possible values are: github-hosted for GitHub-hosted runners provided by GitHub, and self-hosted for self-hosted runners configured by the repository owner.