Sub topic 6
Bundling dependencies so the action runs
This chapter assumes you can write a JavaScript action with an action.yml file and use @actions/core and @actions/github. It also assumes you know npm basics and Git commits. Here you will learn why an action's dependencies must be available at run time, and how to bundle them so they are.
Why dependencies are a problem
When a workflow uses your action, the runner downloads your repository at the referenced tag or commit. It then runs the file named in runs.main. The runner does not run npm install. If index.js calls require('@actions/core') and there is no node_modules directory in the downloaded code, the step fails with an error like this:
Error: Cannot find module '@actions/core'Your action works on your machine because node_modules exists there. The .gitignore file in most Node projects excludes node_modules, so the folder never reaches the repository. The runner only sees what you committed.
There are two ways to fix this:
| Approach | What you commit | Trade-off |
|---|---|---|
| Commit `node_modules` | The whole dependency tree, thousands of files | Large repository, noisy diffs, easy to forget a package |
| Bundle into one file | A single generated file, usually `dist/index.js` | Needs a build step, and the generated file must stay in sync with the source |
Bundling is the standard choice. A bundler reads your entry file, follows every require or import, and writes one self-contained file. The runner then needs only that file.
How the pieces fit together
The diagram shows where the build step sits between your source and the runner.
The important point is that the bundle is a build artifact you commit. The runner never builds anything.
Setting up a bundled action
Start with a project that has a source file in src/. Install the toolkit packages as regular dependencies, and the bundler as a development dependency.
npm init -y
npm install @actions/core @actions/github
npm install --save-dev @vercel/ncc@vercel/ncc is a compiler that packages a Node.js program and its dependencies into a single file. Other bundlers such as esbuild or Rollup also work. This chapter uses ncc because it needs almost no configuration.
Put the action code in src/index.js:
const core = require('@actions/core');
const github = require('@actions/github');
try {
const nameToGreet = core.getInput('who-to-greet');
console.log(`Hello ${nameToGreet}!`);
core.setOutput('time', new Date().toTimeString());
console.log(`Event name: ${github.context.eventName}`);
} catch (error) {
core.setFailed(error.message);
}Add a build script to package.json:
{
"name": "hello-action",
"version": "1.0.0",
"main": "src/index.js",
"scripts": {
"build": "ncc build src/index.js -o dist"
},
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0"
},
"devDependencies": {
"@vercel/ncc": "^0.38.1"
}
}Run the build:
npm run buildThis creates dist/index.js. The file contains your code and all the code it needs from the toolkit packages. It is typically a few hundred kilobytes to a few megabytes.
Finally, point action.yml at the bundle, not at the source:
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
default: 'World'
outputs:
time:
description: 'The time we greeted you'
runs:
using: 'node20'
main: 'dist/index.js'The only change from the earlier version is main: 'dist/index.js'. Pointing at src/index.js is the most common mistake, and it fails on the runner for the reason given at the start of this chapter.
What to commit and what to ignore
Your .gitignore should exclude node_modules but keep dist:
node_modules/Do not add dist/ to .gitignore. Commit the bundle every time the source changes. A typical commit includes src/index.js, package.json, package-lock.json, and dist/index.js.
Commit package-lock.json as well. It makes npm ci produce the same dependency versions each time, so the bundle you build today matches the one a teammate builds tomorrow.
Keeping the bundle in sync
The most common failure after you adopt bundling is a stale dist/index.js. You edit src/index.js, test locally with node src/index.js, commit, and forget to rebuild. The workflow then runs the old bundle, and your change appears to have no effect.
Add a check to your own repository's CI that rebuilds the bundle and fails if it differs from what was committed:
name: Check dist
on:
pull_request:
push:
branches: [main]
jobs:
check-dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Rebuild the dist directory
run: npm run build
- name: Fail if dist changed
run: |
if [ -n "$(git status --porcelain dist)" ]; then
echo "dist/ is out of date. Run 'npm run build' and commit the result."
git diff --stat dist
exit 1
fiThe step git status --porcelain dist prints nothing when the rebuilt files match the committed ones. If it prints anything, the committed bundle was stale and the job fails with a message that tells the contributor what to do.
A local alternative is a Git pre-commit hook that runs npm run build and stages dist/. The CI check is still worth keeping, because hooks are easy to skip.
Choosing the Node runtime
The using field in action.yml selects the Node version that runs your bundle. The example uses node20. Your bundle should target the same major version. Locally, run node --version and confirm that it matches, so a feature that works for you does not fail on the runner.
If the runner's supported versions change, update using and rebuild. Old runtime values are eventually retired by GitHub, and workflows that use an action with a retired runtime start to show warnings, then fail.
Edge cases and gotchas
- Native modules. A package that compiles C++ code during install produces a binary for one operating system and CPU. A bundler cannot fold that binary into
dist/index.js. Prefer pure JavaScript packages. If you need a native module, the action may run on only one platform. - Files read at run time. If your code reads a file with
fs.readFileSyncfrom a path relative to the source, such as a template, the file is not part of the bundle. ncc handles some static asset references, but not dynamic paths. Test the built bundle, not just the source. - Dev dependencies in the bundle. The bundler includes only what your entry file actually imports. Test libraries in
devDependenciesdo not end up indist/unless you import them. - Source maps.
ncc build --source-mapwritesdist/index.js.map, so stack traces point to your original lines. If you use it, commit the map file as well and add--license licenses.txtto keep third-party license text with the bundle. - Minification.
ncc build --minifyshrinks the file but makes diffs and security review harder. Most action authors leave it off. - Line endings and diffs. Generated files can produce very large diffs. You can mark
dist/*as generated in.gitattributesso that GitHub collapses it in pull requests:
dist/** -diff linguist-generated=trueTesting the bundle before you release
Always run the bundle itself at least once before tagging a release. You can do this locally by setting the environment variable that core.getInput reads. Input names are uppercased, and spaces become underscores, with an INPUT_ prefix:
INPUT_WHO-TO-GREET="Mona" node dist/index.jsSome shells reject environment variable names that contain a hyphen. If yours does, use env:
env "INPUT_WHO-TO-GREET=Mona" node dist/index.jsThen run the action in a real workflow with uses: ./ as shown in an earlier chapter. This confirms that the runner finds dist/index.js and that nothing was left out.
Exercise
A teammate edits src/index.js, runs node src/index.js locally and sees the right output, then pushes. In the workflow, the action behaves as it did before the change. What is the most likely cause, and which two things would you set up to prevent it?
Show answer
The committed dist/index.js is stale, because action.yml points at dist/index.js and the bundle was not rebuilt and committed. To prevent it, add a CI job that runs npm ci and npm run build and fails if git status --porcelain dist shows changes, and optionally add a pre-commit hook that builds and stages dist/.
Exercise
Your action works in a workflow run from your repository, but when another repository uses it, the step fails with Cannot find module '@actions/core'. action.yml has main: 'src/index.js'. Name two valid fixes and the trade-off of each.
Show answer
Fix one: bundle with ncc into dist/index.js, commit it, and change main to dist/index.js. This keeps the repository small but needs a build step and a sync check. Fix two: commit node_modules so require can resolve packages. This needs no build step but adds thousands of files and noisy diffs, and it is easy to commit an incomplete tree.