How to Set Up a CI/CD Pipeline with Husky and GitHub Actions

 

Setting up a CI/CD pipeline with Husky and GitHub Actions involves integrating automated testing (CI) and continuous deployment (CD) processes into your GitHub repository. Here’s a step-by-step guide to get you started:

1. Setting Up Husky

Husky is a tool that helps you run scripts (such as linting or tests) at specific Git hooks. Follow these steps to set it up:

Install Husky: If you haven’t already, install Husky and lint-staged by running:

css
npm install husky lint-staged --save-dev

Configure Husky: Add Husky configuration to your package.json:

json

"husky": {
  "hooks": {
    "pre-commit": "lint-staged"
  }
},
"lint-staged": {
  "*.js": [
    "eslint --fix",
    "git add"
  ]
}

This configuration runs ESLint and fixes linting errors before each commit.

2. Setting Up GitHub Actions

GitHub Actions allows you to automate tasks directly within your GitHub repository. Here’s how to set up a basic CI/CD workflow using GitHub Actions:

Create a Workflow File: Inside your repository, create a directory named .github/workflows. In this directory, create a YAML file (e.g., ci-cd.yaml) for your workflow.

Define Workflow: Here’s an example YAML file for CI/CD with Node.js, running tests on push and deploying on merge to the main branch:

yaml

name: CI/CD Pipeline
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Install dependencies
        run: npm install
      - name: Run tests
        run: npm test
  deploy:
    runs-on: ubuntu-latest
    needs: build
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    steps:
      - name: Deploy to production
        run: |
          echo Deploying...
          # Add your deployment steps here

3. Workflow Explanation:

Trigger: The workflow triggers on push to the main branch and on pull_request to main.
Jobs:
  •     build: Checks out the code, sets up Node.js, installs dependencies, and runs tests.
  •     deploy: Deploys the application when changes are pushed to the main branch.

4. Commit and Push Changes:

  • Commit your changes (including .huskyrc and .github/workflows/ci-cd.yaml).
  • Push your changes to GitHub.

5. Testing the Setup:

  • Make a code change and commit it. Husky should run ESLint before allowing the commit.
  • Push the change to GitHub. GitHub Actions should automatically run tests defined in your workflow.

Additional Considerations:

  • Environment Variables: If your deployment requires secrets or environment variables, use GitHub Secrets and pass them as environment variables in your workflow.
  • Notifications: Configure notifications (email, Slack, etc.) to get notified of workflow run status.
  • Error Handling: Handle errors gracefully in your workflows to avoid failed deployments.

By following these steps, you should have a basic CI/CD pipeline set up with Husky and GitHub Actions, ensuring automated testing and deployment for your project.

Post a Comment

0 Comments