1

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

1 Answer 1

1

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

  • use ${{ github.action_path }} to be able to access to the script (${{ github.action_path }}/my-script.js).
  • Set working-directory to ${{ github.action_path }} when install the dependencies with npm install.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.