DEV Community

WDSEGA
WDSEGA

Posted on

GitHub Actions CI/CD Complete Guide

Introduction

GitHub Actions is one of the most popular CI/CD tools today. This guide covers everything from basics to advanced techniques.

Basic Workflow Structure

name: CI/CD Pipeline

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '18'
      - run: npm ci
      - run: npm test
Enter fullscreen mode Exit fullscreen mode

Matrix Builds

Test across multiple environments:

strategy:
  matrix:
    node-version: [16, 18, 20]
    os: [ubuntu-latest, windows-latest]
Enter fullscreen mode Exit fullscreen mode

Caching

Speed up builds with caching:

- uses: actions/setup-node@v4
  with:
    node-version: '18'
    cache: 'npm'

- uses: actions/cache@v3
  with:
    path: ~/.cache/pip
    key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
Enter fullscreen mode Exit fullscreen mode

Reusable Workflows

Create reusable workflow:

# .github/workflows/reusable-test.yml
on:
  workflow_call:
    inputs:
      node-version:
        required: true
        type: string

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ inputs.node-version }}
      - run: npm ci
      - run: npm test
Enter fullscreen mode Exit fullscreen mode

Security Scanning

- name: Run Trivy vulnerability scanner
  uses: aquasecurity/trivy-action@master
  with:
    scan-type: 'fs'
    format: 'sarif'

- name: Initialize CodeQL
  uses: github/codeql-action/init@v2
Enter fullscreen mode Exit fullscreen mode

Summary

Key points for GitHub Actions:

  1. Design proper trigger conditions
  2. Use matrix builds for multi-environment testing
  3. Leverage caching for faster builds
  4. Separate jobs for parallel execution
  5. Use environment protection for production
  6. Integrate security scanning

💡 Tool Recommendation: For managing multiple GitHub repositories' Actions configurations, check out FeishuAgent Orchestrator - a multi-agent collaboration framework for intelligent task scheduling.


Originally published on WD Tech Blog

Top comments (0)