Skip to content

Hands-On with GitHub Actions: Setting Up Continuous Integration


Created Sep 19, 2023 – Last Updated Sep 19, 2023

GitHub

#Introduction πŸš€

When working on CI, I realized the importance of Continuous Integration (CI) for ensuring code quality. Using GitHub Actions, I set up an automated CI pipeline for the project. Let’s walk through the process together.

#1. Initializing the Workflow in YourAwesomeProject πŸ“

In the root of YourAwesomeProject, create a new directory structure for GitHub Actions:

bash
mkdir -p .github/workflows

Inside the workflows directory, create a new file named node-ci.yml.

#2. Configuring the Workflow πŸ”§

Here’s the content of node-ci.yml, detailing every step of the CI process:

yml
name: Node.js CI for YourAwesomeProject
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [12, 14, 16]
steps:
- name: Checkout YourAwesomeProject's code
uses: actions/checkout@v2
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- name: Install dependencies
run: npm ci
- name: Lint the code
run: npm run lint
- name: Execute tests
run: npm test

#3. Demystifying the Workflow πŸ”

Here’s a step-by-step breakdown:

  • name: This is the name of our CI process. It appears this way under the Actions tab in our repo.

  • on: Defines when the CI should be triggered. Our setup runs on pushes and pull requests to the main branch.

  • jobs: Jobs describe the set of actions the CI will perform. Here, we only have one job: test.

  • runs-on: We’re using the latest version of Ubuntu to run our CI tasks.

  • strategy: We’re testing our project on multiple Node.js versions (12, 14, and 16) to ensure compatibility.

  • steps: A detailed set of tasks:

    1. Checkout code: This gets the latest code from YourAwesomeProject.
    2. Set up Node.js: Prepares the Node.js environment.
    3. Install dependencies: Instead of npm install, npm ci is used for faster and more reliable dependency installation.
    4. Lint the code: Assuming you have a lint script set up in your package.json.
    5. Run tests: Executes the test scripts specified in your project.

#4. Push and Watch it in Action πŸš€

After saving the node-ci.yml file, commit and push the changes to your repository:

bash
git add .github/workflows/node-ci.yml
git commit -m "Set up CI with GitHub Actions"
git push origin main

Navigate to the Actions tab in your YourAwesomeProject GitHub repository. You’ll see the CI process running or queued up. Click on it to see details and logs.

#5. Adapting and Expanding πŸ› 

This is a foundational setup. Depending on YourAwesomeProject’s requirements, you might want to:

  • Add a build step if your project needs compiling or transpiling.
  • Cache dependencies for faster subsequent runs.
  • Deploy your project automatically upon successful tests and merges.

#Wrapping Up 🌟

Setting up Continuous Integration for YourAwesomeProject with GitHub Actions has brought peace of mind, ensuring that every change is automatically tested. It’s not just about automation; it’s about enhancing code quality, spotting issues early, and streamlining the development process. Give it a try, and watch your development practices transform! πŸš€