Create Multiple GitHub Actions in a Single Repository
Kimberly Myers
Posted on September 16, 2024
You can create and package multiple GitHub Actions in a single repository that are written using TypeScript/JavaScript, allowing each action to run separately. Below is how to accomplish this using vercel/ncc, which allows compiling modules into a single file with its dependencies eliminating the need to include the node_modules folder in your repository:
Create and Configure Actions:
- Create a separate folder for each action and configure the action.yml file within each folder
š test-action
-ā action.yml
š test-action-2
-ā action.yml
Add Source Code:
- Place your source code for each action in the src folder
š src
-š test-action.ts
-š test-action-2.ts
Create Distribution Folder:
Create a folder named dist to store the packaged source code
š distCreate separate folder for each action under dist folder
š dist
-š test-action
-š test-action-2
Update package.json:
- Add a separate script to "scripts" in the package.json file for each action that calls npx ncc build command passing the TypeScript source code file and the output distribution folder for the action. Each script will be used to package your TypeScript source file as JavaScript in a generated file called index.js.
"scripts": {
"testAction": "npx ncc build src/test-action.ts -o dist/test-action --source-map --license licenses.txt",
"testAction2": "npx ncc build src/test-action-2.ts -o dist/test-action-2 --source-map --license licenses.txt"
}
- Add a script to bundle all packages and copy the generated JavaScript source to the distribution folder
"scripts": {
"bundle": "npm run format:write && npm run testAction && npm run testAction2 && copy src/*.js dist"
}
Run the bundle script:
npm run bundle
Update the action.yml to call package source in distribution folder for each action:
runs:
using: node20
main: '../dist/test-action/index.js'
runs:
using: node20
main: '../dist/test-action-2/index.js'
Create a GitHub workflow to test
on:
workflow_dispatch:
jobs:
action_test_job:
runs-on: ubuntu-latest
name: Test multiple actions
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Test Action
id: testaction
uses: ./test-action
- name: Test Action2
id: testaction2
uses: ./test-action2
References
Posted on September 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.