#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:
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:
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:
- Checkout code: This gets the latest code from
YourAwesomeProject
. - Set up Node.js: Prepares the Node.js environment.
- Install dependencies: Instead of
npm install
,npm ci
is used for faster and more reliable dependency installation. - Lint the code: Assuming you have a lint script set up in your
package.json
. - Run tests: Executes the test scripts specified in your project.
- Checkout code: This gets the latest code from
#4. Push and Watch it in Action π
After saving the node-ci.yml
file, commit and push the changes to your repository:
git add .github/workflows/node-ci.ymlgit 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! π