GitHub Actions CI/CD Pipeline Tutorial for Node.js
TL;DR: To set up a CI/CD pipeline for Node.js, create a workflow file in the “.github/workflows” directory that triggers on pushes to the main branch. This workflow installs dependencies, runs linting and tests, and finally builds and deploys your application upon successful test completion.
Step 1: Create the Workflow File
Begin by navigating to your repository’s root directory. Create a new folder named “.github”, and inside it, create another folder called “workflows”. Within the “workflows” folder, add a new file named “ci-cd.yml”. This specific path is required for GitHub Actions to recognize and execute your automation scripts. Ensure the file extension is exactly “.yml” to maintain proper YAML formatting.
If you want to dig deeper, check out our guide on 7 Shopify Dropshipping Niches That Are Still Profitable.
Step 2: Define the Trigger
At the top of your “ci-cd.yml” file, define the “on” key to specify when the workflow should run. For a standard CI/CD setup, trigger the pipeline on pushes to the “main” branch. You can also add “pull_request” to run checks before merging changes. This ensures that code quality is maintained before any code reaches the production environment. Keep the trigger simple initially to avoid unnecessary resource consumption during development.
Step 3: Configure the Job Environment
Under the “jobs” key, define a job named “build-and-deploy”. Specify the “runs-on” parameter as “ubuntu-latest” to use a fresh Linux virtual machine for each run. This isolation prevents cross-contamination between different branches or runs. Next, add a “steps” section. The first step should use “actions/checkout@v4” to clone your repository code into the runner. This is a prerequisite for any subsequent actions that need access to your source code.
Step 4: Setup Node.js and Install Dependencies
Add a step using “actions/setup-node@v4” to install the desired Node.js version. Specify your required version in the “node-version” input, such as “18” or “20”. Following this, use “run: npm ci” to install dependencies. Unlike “npm install”, “npm ci” performs a clean installation based strictly on your “package-lock.json” file, ensuring consistent and reproducible builds across different environments. This step is critical for avoiding “works on my machine” issues.
Step 5: Run Linting and Tests
Insert a step to run your linter, typically “npm run lint”, to catch syntax errors and style inconsistencies early. Follow this with a test step using “npm test”. If any test fails, the workflow will immediately stop, preventing the deployment of broken code. You can add specific environment variables or secrets here if your tests require external API keys or database connections. Use the “env” keyword to map these securely.
Step 6: Build and Deploy
Finally, add a build step using “npm run build” to compile your application for production. After the build succeeds, add a deployment step. For simple static sites, you might use GitHub Pages. For dynamic apps, you would use a provider-specific action like “aws-actions/configure-aws-credentials” or a custom script. Always ensure that deployment steps are conditional on the success of previous steps by relying on the sequential nature of the “steps” array.
Pro Tips
Use caching for “node_modules” using “actions/cache” to significantly speed up your pipeline. Regularly update your action versions to security patches. Keep your workflow file concise and readable. Monitor the run duration and optimize slow steps, such as heavy test suites, by running them in parallel if possible. Always test your pipeline in a development branch before merging to main.
FAQ
Q: Why is my build failing on GitHub Actions but not locally?
A: This usually happens due to version mismatches or uncommitted lock files
Leave a Reply