Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Github Workflow Presets

Sketch supports presets for Github workflows, as well as individual components in a workflow, such as a job or a step.

Example

We use this configuration:

github:
  workflow_presets:
    # Common settings for all workflows
    base:
      defaults:
        run:
          shell: bash
      env:
        my_env: somevalue
        another_env: anothervalue

    extended:
      extends_presets:
        - base
      on:
        push:
          branches:
            - main
      jobs:
        # Adding a job by using its preset ID
        check_main_branch: check_main_branch
        do_something:
          extends_presets:
            - base
          if: needs.check_branch.outputs.is_on_main == 'true'
          name: Do something while on main branch
          steps:
            - run: echo "Done something from main branch!"

  workflow_job_presets:
    # Common settings for all jobs
    base:
      runs-on: ubuntu-latest
      timeout-minutes: 25
      continue-on-error: false
      env:
        my_env: somevalue
        another_env: anothervalue

    check_main_branch:
      extends_presets:
        - base
      env:
        another_other_value: yetanothervalue
      outputs:
        is_on_main: ${{ steps.branch_check.outputs.is_on_main }}
      steps:
        # Using a step preset
        - check_main

  steps_presets:
    # Reusable step preset
    check_main:
      name: Check if a tag is on the main branch
      id: branch_check
      run: |
        if git branch -r --contains ${{ github.ref }} | grep -q 'origin/main'; then
        echo "On main branch. Proceeding with the workflow..."
        echo "is_on_main=true" >> "$GITHUB_OUTPUT"
        else
        echo "Not on main branch. Skipping workflow..."
        fi

Run the command

sketch gh-workflow extended

And get this output:

on:
  push:
    branches:
    - main
env:
  another_env: anothervalue
  my_env: somevalue
defaults:
  run:
    shell: bash
jobs:
  check_main_branch:
    runs-on: ubuntu-latest
    outputs:
      is_on_main: ${{ steps.branch_check.outputs.is_on_main }}
    env:
      another_env: anothervalue
      another_other_value: yetanothervalue
      my_env: somevalue
    timeout-minutes: 25
    continue-on-error: false
    steps:
    - name: Check if a tag is on the main branch
      id: branch_check
      run: |
        if git branch -r --contains ${{ github.ref }} | grep -q 'origin/main'; then
        echo "On main branch. Proceeding with the workflow..."
        echo "is_on_main=true" >> "$GITHUB_OUTPUT"
        else
        echo "Not on main branch. Skipping workflow..."
        fi
  do_something:
    runs-on: ubuntu-latest
    name: Do something while on main branch
    if: needs.check_branch.outputs.is_on_main == 'true'
    env:
      another_env: anothervalue
      my_env: somevalue
    timeout-minutes: 25
    continue-on-error: false
    steps:
    - run: echo "Done something from main branch!"