github-complete-guide/en/docs/19-github-actions.md at main · wangzi5151/github-complete-guide · GitHub
Skip to content

Latest commit

 

History

History
174 lines (126 loc) · 2.92 KB

19-github-actions.md

File metadata and controls

174 lines (126 loc) · 2.92 KB

GitHub Actions Automation

What is GitHub Actions?

GitHub Actions is GitHub's CI/CD platform, can automate build, test and deployment processes.

Basic Concepts

Concept Description
Workflow Automated workflow, defined by YAML
Event Event that triggers workflow
Job A group of steps
Step Single task
Action Reusable work unit
Runner Server that executes workflow

Create Workflow

File Structure

.github/
└── workflows/
    └── ci.yml

Basic Template

name: CI

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

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'
        
    - name: Install dependencies
      run: npm install
      
    - name: Run tests
      run: npm test

Common Workflows

Node.js Project

name: Node.js CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        node-version: [18, 20, 22]
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        
    - run: npm ci
    - run: npm run build
    - run: npm test

Deploy to GitHub Pages

name: Deploy to GitHub Pages

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    permissions:
      contents: read
      pages: write
      id-token: write
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Setup Pages
      uses: actions/configure-pages@v4
      
    - name: Build
      run: npm run build
      
    - name: Upload artifact
      uses: actions/upload-pages-artifact@v3
      with:
        path: './dist'
        
    - name: Deploy to GitHub Pages
      uses: actions/deploy-pages@v4

Secrets Management

Add in repository Settings → Secrets and variables → Actions:

- name: Deploy
  env:
    API_KEY: ${{ secrets.API_KEY }}
  run: ./deploy.sh

Cache Optimization

- name: Cache dependencies
  uses: actions/cache@v4
  with:
    path: ~/.npm
    key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}

Workflow Syntax

Trigger Events

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  schedule:
    - cron: '0 0 * * *'  # Execute daily
  workflow_dispatch:       # Manual trigger

Conditional Execution

- name: Deploy
  if: github.ref == 'refs/heads/main'
  run: ./deploy.sh

Actions Marketplace

Visit github.com/marketplace?type=actions to discover commonly used Actions.

Next Step

GitHub Pages Static Website →