Ivo Valchev
Ivo Valchev

Reputation: 265

GitHub Actions defaults setting

Official github actions documentation says I can set the defaults to specfify defaults settings for all jobs (https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#defaults). I want to set that up to specify

defaults:
  runs-on: ubuntu-latest
  strategy:
    fail-fast: false # do not cancel 7.2 if 7.3 fails
    matrix:
      php: ['7.2', '7.3', '7.4']
      node-version: ['12.5']
jobs:
    ...

But this fails with

The workflow is not valid. .github/workflows/code_checks.yaml (Line: 7, Col: 3): Unexpected value 'runs-on',.github/workflows/code_checks.yaml (Line: 8, Col: 3): Unexpected value 'strategy'

I want to specify the same runs-on and strategy for all my jobs. Why isn't the defaults working?

Upvotes: 13

Views: 8416

Answers (2)

Max Cascone
Max Cascone

Reputation: 833

I have the same gripe with Actions. I am working around it for now by setting repeated job configurations as inputs that are not required and have defaults. That way they are defaulted but can also be user-overridden if desired. I suppose they could also be workflow-level env vars but that would make them not user-overridable, if that's a concern for you.

So I do:

on:
  workflow_call:
    inputs:
      runs-on:
        required: false
        type: string
        description: The self-hosted runner to use
        default: 'linux'

      timeout-minutes:
        required: false
        type: number
        description: The maximum time in minutes that the job can run.
        default: 90

# note that you *can* set this default
defaults:
  run:
    shell: bash


# use the workflow defaults here
jobs:
  init:
    name: Initialize
    runs-on: ${{ inputs.runs-on }}
    timeout-minutes: ${{ inputs.timeout-minutes }}

Upvotes: 0

zzarbi
zzarbi

Reputation: 1852

This is not possible, with defaults you can only set the shell and working-directory.

You're kinda looking for default-strategy which doesn't exist. One thing to keep in mind with Github Actions is that each job is spawned on a different machine which doesn't share any information with the previous job.

What suits your needs better is to create one job with one set of strategies and multiple steps.

Upvotes: 9

Related Questions