github-actionsbuilding-github-actions

How to share a node script and run it from a shared github workflow


I'm writting a shared github workflow meant to be reused (ex: my-username/my-repo/.github/workflows/my-workflow.yml).

And in another repository:

jobs:
  my-job:
    uses: my-username/my-repo/.github/workflows/my-workflow

I'd like this workflow file to run a node script

Ex: my-script.js:

const myModule = require('my-module');
myPackage.doSomething();

How do I share my-script.js and make it available in the workflow?

My current workflow file is:

# my-username/my-repo/.github/workflows/my-workflow.yml
on:
  workflow_call:

jobs:
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v4
      - uses: actions/setup-node@v4
      - run: npm install my-module
      # - run: node ??/my-script.js

Solution

  • I used a composite action:

    My shared workflow file became:

    # my-username/my-repo/.github/workflows/my-workflow.yml
    on:
      workflow_call:
    
    jobs:
      my-job:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout Repo
            uses: actions/checkout@v4
          - uses: my-username/my-repo/actions/my-action
    

    And I created my-username/my-repo/actions/my-action/action.yml:

    # my-username/my-repo/actions/my-action/action.yml
    runs:
      using: "composite"
      steps:
        - uses: actions/setup-node@v4
        - run: npm install my-module
          shell: bash
          working-directory: ${{ github.action_path }}
        - run: node ${{ github.action_path }}/my-script.js
          shell: bash
    

    and I moved my-script.js in my-username/my-repo/actions/my-action/my-script.js.

    Some tricks were to to