Cơ Bản Về Workflow
Workflow Tối Giản
# .github/workflows/ci.yml name: CI on: push jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: echo "Hello from CI"
Khái Niệm Chính
WorkflowFile YAML trong .github/workflows/ định nghĩa tự động hóa
EventTrigger khởi động workflow (push, PR, schedule, v.v.)
JobTập hợp các bước chạy trên cùng một runner
StepTác vụ riêng lẻ — chạy lệnh hoặc dùng action
RunnerVM thực thi jobs (ubuntu-latest, macos-latest, windows-latest)
ActionĐơn vị code tái sử dụng được tham chiếu bằng uses:
Triggers
Sự Kiện Phổ Biến
on: push: branches: [main] pull_request: branches: [main] schedule: - cron: "0 6 * * 1" # every Monday 6 AM UTC workflow_dispatch: # manual trigger
Bộ Lọc Sự Kiện
branches:Trigger chỉ cho branch cụ thể
paths:Trigger chỉ khi file khớp thay đổi
tags:Trigger khi push tag (v*)
types: [opened, synchronize]Lọc loại hoạt động PR
branches-ignore:Loại trừ branch cụ thể
paths-ignore:Loại trừ đường dẫn file cụ thể
Jobs & Steps
Cấu Hình Job
jobs: test: runs-on: ubuntu-latest needs: build # depends on build job if: github.ref == 'refs/heads/main' timeout-minutes: 10 steps: - uses: actions/checkout@v4 - run: npm test
Loại Step
run:Thực thi lệnh shell
uses:Dùng action đã publish
with:Truyền input cho action
name:Tên hiển thị trong UI
id:Tham chiếu output step qua steps..outputs
if:Thực thi có điều kiện
continue-on-error: trueKhông fail job nếu step thất bại
Actions
Sử Dụng Actions
steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 20 - uses: ./.github/actions/my-action # local action
Actions Phổ Biến
actions/checkout@v4Checkout code repository
actions/setup-node@v4Cài đặt Node.js
actions/setup-python@v5Cài đặt Python
actions/upload-artifact@v4Tải lên build artifacts
actions/download-artifact@v4Tải xuống artifacts từ job khác
actions/cache@v4Cache dependencies giữa các lần chạy
actions/github-script@v7Chạy JS với client GitHub API
Biến Môi Trường
Thiết Lập Biến
env: # workflow-level NODE_ENV: production jobs: build: env: # job-level CI: true steps: - run: echo "$MY_VAR" env: # step-level MY_VAR: hello
Biến Mặc Định
github.shaSHA commit kích hoạt workflow
github.refRef branch hoặc tag (refs/heads/main)
github.repositoryTên owner/repo
github.actorNgười dùng kích hoạt workflow
github.event_nameSự kiện kích hoạt workflow
runner.osOS của runner (Linux, macOS, Windows)
Secrets
Sử Dụng Secrets
steps: - run: deploy --token "$TOKEN" env: TOKEN: ${{ secrets.DEPLOY_TOKEN }} - uses: some/action@v1 with: api-key: ${{ secrets.API_KEY }}
Quy Tắc Secret
secrets.GITHUB_TOKENToken tự động tạo có phạm vi repo
Settings → SecretsThêm secrets trong cài đặt repo hoặc org
MaskingGiá trị secret tự động che trong log
Environment secretsGiới hạn trong môi trường deployment
Org secretsChia sẻ giữa các repo trong tổ chức
Chiến Lược Matrix
Matrix Builds
jobs: test: strategy: matrix: os: [ubuntu-latest, macos-latest] node: [18, 20] runs-on: ${{ matrix.os }} steps: - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }}
Tùy Chọn Matrix
matrix:Định nghĩa các tổ hợp biến để mở rộng
include:Thêm tổ hợp ngoài vào matrix
exclude:Loại bỏ tổ hợp cụ thể
fail-fast: falseTiếp tục job khác nếu một job thất bại
max-parallel: 2Giới hạn số matrix job chạy đồng thời
Caching
Cache Dependencies
- uses: actions/cache@v4 with: path: ~/.npm key: npm-${{ hashFiles('package-lock.json') }} restore-keys: npm-
Cache Tích Hợp Sẵn
- uses: actions/setup-node@v4 with: node-version: 20 cache: npm # auto-cache for npm/yarn/pnpm - uses: actions/setup-python@v5 with: python-version: "3.12" cache: pip # auto-cache for pip
Artifacts
Tải Lên & Tải Xuống
- uses: actions/upload-artifact@v4 with: name: build-output path: dist/ retention-days: 7 - uses: actions/download-artifact@v4 with: name: build-output
Lưu Ý Về Artifacts
retention-daysTự động xóa sau N ngày (mặc định 90)
pathFile hoặc thư mục cần tải lên (hỗ trợ glob)
Cross-jobTải lên ở một job, tải xuống ở job khác với needs:
compression-level0 (không nén) đến 9 (nén tối đa), mặc định 6
Mẫu Phổ Biến
Triển Khai Có Điều Kiện
- name: Deploy to production if: github.ref == 'refs/heads/main' run: ./deploy.sh - name: Post PR comment if: github.event_name == 'pull_request' run: gh pr comment $PR --body "Build passed"
Biểu Thức Hữu Ích
success()True nếu tất cả bước trước thành công
failure()True nếu bất kỳ bước nào thất bại
always()Chạy bất kể trạng thái (dọn dẹp)
cancelled()True nếu workflow bị hủy
contains(github.ref, 'release')Kiểm tra chuỗi có chứa
startsWith(github.ref, 'refs/tags')Kiểm tra tiền tố chuỗi
hashFiles('**/lock*')SHA-256 của file (cho cache key)