DEV Community

Cover image for How to Set Up a Basic CI/CD Pipeline with GitHub Actions
10000coders
10000coders

Posted on • Edited on

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

How to Set Up a Basic CI/CD Pipeline with GitHub Actions
GitHub Actions provides a powerful and flexible way to automate your software development workflows. In this guide, we'll walk through setting up a basic CI/CD pipeline for a typical web application.

Understanding GitHub Actions
GitHub Actions uses YAML files (workflows) to define your CI/CD pipeline. These workflows are stored in the .github/workflows directory of your repository.

Key Concepts
Workflows: Automated processes defined in YAML files
Events: Triggers that start workflows (push, pull request, etc.)
Jobs: Sets of steps that run on the same runner
Steps: Individual tasks that can run commands or actions
Actions: Reusable units of code that can be shared across workflows
Setting Up Your First Workflow
Let's create a basic workflow for a Node.js application:

name: CI/CD Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '18'

    - name: Install Dependencies
      run: npm install

    - name: Run Tests
      run: npm test

    - name: Build
      run: npm run build
Enter fullscreen mode Exit fullscreen mode

Understanding the Workflow
Trigger Events

The workflow runs on push to main branch
Also runs on pull requests to main branch
Job Configuration

Uses Ubuntu latest as the runner
Sets up Node.js environment
Installs dependencies
Runs tests
Builds the application


Adding Deployment Steps
For a complete CI/CD pipeline, let's add deployment:

# ... previous configuration ...

jobs:
  build-and-test:
    # ... previous steps ...

  deploy:
    needs: build-and-test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'

    steps:
    - uses: actions/checkout@v3

    - name: Deploy to Production
      uses: some-deployment-action@v1
      with:
        credentials: ${{ secrets.DEPLOY_KEY }}
Enter fullscreen mode Exit fullscreen mode

Best Practices
Security

Use secrets for sensitive data
Implement branch protection rules
Review third-party actions
Performance

Cache dependencies
Use matrix builds for multiple environments
Optimize workflow steps
Maintenance

Keep workflows DRY
Document complex steps
Regular updates of actions

Common Use Cases

  1. Automated Testing
- name: Run Unit Tests
  run: npm run test:unit

- name: Run Integration Tests
  run: npm run test:integration

- name: Run E2E Tests
  run: npm run test:e2e
Enter fullscreen mode Exit fullscreen mode
  1. Code Quality
- name: Lint
  run: npm run lint

- name: Type Check
  run: npm run type-check

- name: Security Scan
  uses: snyk/actions/node@master
Enter fullscreen mode Exit fullscreen mode
  1. Deployment Strategies Staging Deployment
- name: Deploy to Staging
  if: github.ref == 'refs/heads/develop'
  run: npm run deploy:staging
Enter fullscreen mode Exit fullscreen mode

Production Deployment

- name: Deploy to Production
  if: github.ref == 'refs/heads/main'
  run: npm run deploy:prod

Enter fullscreen mode Exit fullscreen mode

Advanced Features
Matrix Builds

Test across multiple Node.js versions
Build for different platforms
Run tests in parallel
Caching

Cache npm dependencies
Cache build artifacts
Optimize workflow speed
Environment Variables

Use different variables per environment
Secure sensitive data
Configure deployment targets
Troubleshooting
Common issues and solutions:

Workflow Not Triggering

Check branch names
Verify file paths
Review permissions
Failed Steps

Check logs for errors
Verify dependencies
Test locally first
Performance Issues

Optimize caching
Review job dependencies
Use appropriate runners
Conclusion
GitHub Actions provides a powerful platform for implementing CI/CD pipelines. Start with basic workflows and gradually add complexity as your needs grow. Remember to follow security best practices and regularly review and update your workflows.

By implementing these practices, you'll have a robust CI/CD pipeline that automates your development process, improves code quality, and enables faster deployments.
๐Ÿš€ Ready to kickstart your tech career?
๐Ÿ‘‰ [Apply to 10000Coders]
๐ŸŽ“ [Learn Web Development for Free]
๐ŸŒŸ [See how we helped 2500+ students get jobs]

Top comments (0)