Skip to content

Instantly share code, notes, and snippets.

@sterlingwes
Last active August 16, 2022 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sterlingwes/207553165b73afc8104ceb3215a24fbe to your computer and use it in GitHub Desktop.
Save sterlingwes/207553165b73afc8104ceb3215a24fbe to your computer and use it in GitHub Desktop.
Reusable Github Action Steps (Local Composite Action)

Crafting a Reusable Github Action

The documentation for this common use case is sorely lacking...

Why this was needed:

  • I had a bunch of CI jobs (lint, test, type checking) that I wanted to run as separate jobs but share the same common steps for running checkout & dependency install

File structure

ie: my-github-user/my-repo with main as the default branch

.github
  /workflows
    my-workflow.yml
  /actions
    /yarn-install
      action.yml

my-workflow.yml

name: CI
on: push

jobs:
  test:
    steps:
      - uses: my-github-user/my-repo/.github/actions/yarn-install@main

      - name: Run Jest
        run: yarn test
        
  lint:
    steps:
      - uses: my-github-user/my-repo/.github/actions/yarn-install@main

      - name: Run Lint
        run: yarn lint
        

action.yml

name: yarn-install
description: cached yarn install with checkout

runs:
  using: 'composite'
  steps:
    - uses: actions/checkout@v2

    - name: Get yarn cache path
      id: yarn-cache
      run: echo "::set-output name=dir::$(yarn cache dir)"
      shell: bash

    - name: Cache node modules
      uses: actions/cache@v2
      with:
        path: ${{ steps.yarn-cache.outputs.dir }}
        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-yarn-

    - name: Install modules
      run: yarn
      shell: bash

Notes on development

Adding this workflow on a branch before the action.yml file exists on your main branch requires changing the @main commit ref in the uses in your workflow job definition to point to the commit you want to test on your branch. When you're ready to merge, change in back to your main branch name (expect CI to fail), and merge.

The documentation makes it seem like you can locally reference an action using the non-repo notation of: ./.github/actions/yarn-install, but I can say this doesn't work and I tried a few different versions of this.

Docs I referenced:

@gersona
Copy link

gersona commented Aug 16, 2022

I just want to comment that as of august 2022, the non-repo notations are now supported. thanks

@sterlingwes
Copy link
Author

I just want to comment that as of august 2022, the non-repo notations are now supported. thanks

@gersona thanks for sharing, do you have a link for reference?

@gersona
Copy link

gersona commented Aug 16, 2022

I don't actually. I just stumbled on your gist and tried it and it worked. As you said it seems the documentation is quite lacking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment