This GitHub Actions deployment guide gives you a reusable checklist for deploying a Node.js application safely: install the intended runtime, restore dependencies, run tests, build the app, handle secrets, publish artifacts, and require approval before production changes.
Overview
A dependable Node.js CI/CD pipeline separates verification from deployment. Every pull request should be checked before it can merge, while deployment should normally happen only from a controlled branch, tag, or manually approved environment.
The exact final deployment step depends on your hosting platform. A platform may accept a container image, a build artifact, a Git push, an archive uploaded through an API, or a command executed over SSH. The parts before that step are broadly reusable.
Before writing a workflow, confirm four things:
- The Node.js version required by the application and hosting environment.
- The package manager and lockfile committed to the repository.
- The command that installs dependencies, such as
npm cior the equivalent for your package manager. - The build output and the provider-specific deployment method.
For package-manager decisions, see npm vs pnpm vs Yarn. Consistency matters more than switching tools: the workflow should use the same package manager and lockfile format used during local development.
Checklist by scenario
1. Pull request validation
Start with a workflow that validates changes without deploying them. Store it at .github/workflows/ci.yml:
name: CI
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: npm
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build application
run: npm run buildIf the project does not use .nvmrc, replace node-version-file with the version declared in the repository’s runtime configuration. Keep the version explicit so a local upgrade does not silently change the CI environment.
2. Deployment from a protected branch
Once CI passes, add a separate deployment job. This makes the deployment boundary visible and lets you attach an environment such as production:
deploy:
needs: test
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
environment: production
permissions:
contents: read
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: npm
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run build
- name: Deploy
run: ./scripts/deploy.sh
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}The deployment script is intentionally provider-neutral. Replace it with your hosting provider’s supported action, CLI command, container push, or artifact upload. Keep provider-specific credentials in the repository or environment’s encrypted secrets, not in the workflow file.
3. Deploying a built artifact
For larger applications or multi-stage pipelines, build once and deploy the same output. Upload the directory produced by your build command, then download it in a later job:
- name: Upload build output
uses: actions/upload-artifact@v4
with:
name: node-app-build
path: dist/
# In a later deployment job
- name: Download build output
uses: actions/download-artifact@v4
with:
name: node-app-build
path: dist/Adjust dist/ to match the application. A server-rendered Node.js app may need its source, production dependencies, compiled files, and package metadata rather than a frontend-only directory. Document exactly what the hosting platform expects.
4. Deploying with environment-specific configuration
Use separate GitHub environments for staging and production when configuration, approvals, or secrets differ. A staging job can deploy automatically after a successful build; production can use a required reviewer or a manual dispatch workflow. Do not place API keys, database passwords, signing secrets, or provider tokens in plain-text variables.
Remember that build-time and run-time variables are different. A frontend variable embedded during npm run build may become visible to visitors, while a server-side variable can remain private. Review the framework’s conventions before adding any value to a build command.
What to double-check
- Node.js alignment: Match
.nvmrc,package.jsonengines, local development, CI, and the hosting runtime where practical. - Lockfile integrity: Commit one intended lockfile and use the matching clean-install command. Avoid silently regenerating it in CI.
- Permissions: Start with minimal workflow permissions, such as
contents: read. Add access only when a specific deployment step needs it. - Secrets: Confirm names, scope, and environment assignment. A production secret may not be available to a staging job by design.
- Build output: Verify the output directory, start command, port configuration, and required production dependencies.
- Caching: Cache package-manager data, not sensitive files or generated output that could become stale. A cache miss should still produce a correct build.
- Approvals: Protect production with an environment rule when an accidental merge should not immediately release a change.
- Rollback: Know how to redeploy a previous commit, artifact, image, or release before the first production deployment.
After deployment, check logs, health endpoints, application configuration, and the public URL. If browser requests fail, the issue may be CORS rather than the pipeline; use the CORS troubleshooting guide. For DNS, HTTPS, and post-release checks, use the web deployment checklist.
Common mistakes
| Symptom | Likely cause | Practical check |
|---|---|---|
| Dependency installation fails | The lockfile and package manager do not match, or the Node.js version is wrong. | Run the same clean-install command locally with the workflow’s declared runtime. |
| Secret is empty or unavailable | The secret name is misspelled or belongs to another environment. | Check repository and environment settings without printing the secret value. |
| Build works locally but fails in Actions | The local machine has undeclared files, tools, or environment variables. | Use a clean checkout and list required variables and system dependencies. |
| Cache causes confusing behavior | Stale or overly broad cache keys are restoring unintended data. | Clear or narrow the cache configuration, then verify the build without relying on it. |
| Deployment succeeds but the app is unavailable | The start command, port, health check, or runtime configuration is incorrect. | Inspect hosting logs and test the expected process and endpoint. |
| Production deploys unexpectedly | The workflow triggers on more branches or events than intended. | Review the trigger, branch condition, environment protection, and approval rules. |
Do not treat a green workflow as proof that the application is healthy. CI confirms the checks you configured; it cannot automatically confirm correct DNS, third-party credentials, migrations, or user-facing behavior.
When to revisit
Review this workflow before seasonal planning cycles, before a major release, and whenever the repository or hosting platform changes. In particular, revisit it when you:
- Upgrade Node.js, the package manager, or the application framework.
- Change from npm to pnpm or Yarn, or replace the lockfile.
- Add a release branch, preview environment, staging environment, or production approval gate.
- Change build commands, output directories, container settings, or hosting providers.
- Add database migrations, scheduled jobs, private package registries, or new secrets.
- Notice slower builds, repeated cache failures, permission warnings, or unexplained deployment behavior.
- Need to update action references after reviewing your organization’s approved versions and security policy.
Keep a small deployment runbook beside the workflow: required variables, supported branches, rollback steps, health checks, and the owner of each hosting credential. After any change, test the pull-request path first, then staging, then production approval. That routine turns a one-time GitHub Actions setup into a maintainable Node.js deployment process.