GitHub actions conditional config

Viewed 1267

(GH newb attempting to cargo cult my way through some configuration ...)

I am using the GitHub actions YAML file here to test an R package. By default this workflow tests on four separate platforms, but this is overkill for my day-to-day needs. (This action does more limited testing, but I think? it also misses some stuff I wanted from the fuller workflow ...)

I would like to have the tests done on all four platforms only if the commit message includes the string "full check", otherwise testing just one platform. I know I can in principle achieve something like this if I include a conditional:

if: "contains(github.event.head_commit.message, '[full ci]')"

but I'm not sure of the precise syntax for including it in the workflow (and I'm afraid of spending many hours on trial-and-error attempts)

The chunk of the workflow that defines the set of platforms current looks like this, with three of the platforms commented out:

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
##          - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
##          - {os: windows-latest, r: 'release'}
##          - {os: macOS-latest, r: 'release'}

I'm assuming that just sticking the if: clause below the first row won't work (because it would break up the list). I suppose something like this might work?

    matrix:
      config:
        if: "! contains(github.event.head_commit.message, '[full ci]')"]
        config:
           - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
        if: "contains(...)"
        config: 
            [list with all four configs]

(Does this syntax have an if/else construct?)

I'm looking for answers that help me achieve the narrow goal, and hopefully understand the YAML syntax a little better (I have tried to read the FM ...)

1 Answers

I believe it is not possible to add conditions to the matrix construct itself. You could generate the matrix data beforehand in another job and then use it in your job.

There is a simpler solution: Use two jobs, because if can be used at the job level. Copy the current job that you have and then add if-conditions to each job. Keep one job as it is for the full CI and in the other job hardcode your environment for the ordinary commits.

Something like this:

jobs:
  R-CMD-check-ordinary:
    if: ${{ ! contains(github.event.head_commit.message, '[full ci]') }}
    # Hardcode os
    runs-on: ubuntu-20.04
    # Hardcode name
    name: Test only on Ubuntu

    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      RSPM: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

    steps:
      # replace any occurences of matrix.config. with hardcoded values
      ...
      
  # And the original job
  R-CMD-check-full:
    if: ${{ contains(github.event.head_commit.message, '[full ci]') }}
    runs-on: ${{ matrix.config.os }}

    name: ${{ matrix.config.os }} (${{ matrix.config.r }})

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
          - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
          - {os: windows-latest, r: 'release'}
          - {os: macOS-latest, r: 'release'}

    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      RSPM: ${{ matrix.config.rspm }}
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

    steps:
      ...

Regarding YAML syntax:

YAML is a data format. Don't think of YAML as a script or program. A YAML file is similar to a list of command line arguments for a program. It only defines input data for another program.

So there is no if in YAML. For YAML if: condition is like as any other key: value pair, there is no logic attached.

Related