Phần 6 — Reusable Workflows và Composite Actions
Ý kiến
0
Chưa có ý kiến nào. Hãy là người đầu tiên chia sẻ!
Chưa có ý kiến nào. Hãy là người đầu tiên chia sẻ!
Pipeline production Next.js → Docker → K8s với cache image, manual approval. Tổng kết best practices và kỹ thuật debug (CI Lint, visualizer, CI_DEBUG_TRACE).
Khai báo environment để GitLab theo dõi deployments, có lịch sử và nút rollback. Review Apps — tính năng signature: mỗi MR tự deploy lên môi trường tạm.
Tái sử dụng pipeline giữa nhiều dự án với include (local, project, template, remote), kế thừa job với extends (hidden job), và YAML anchors.
Series GitHub Actions Toàn Tập — 8 phần:
Phần 1 — Tổng quan: CI/CD, GitHub Actions và các khái niệm cốt lõi
Phần 5 — Self-hosted Runner: cài đặt, security, auto-scaling
Phần 6 — Reusable Workflows và Composite Actions ← bạn đang đọc
Khi có 10–20 repo cùng pattern pipeline (test → build → deploy), copy-paste YAML là một thảm hoạ bảo trì. Phần này so sánh hai cơ chế tái sử dụng pipeline trong GitHub Actions: reusable workflow (toàn bộ workflow gọi qua workflow_call) và composite action (nhóm steps nhúng vào job hiện tại). Hai cơ chế bù trừ nhau — biết khi nào dùng cái nào là chìa khoá để pipeline org-wide gọn nhẹ và dễ thay đổi.
Khi có 10–20 repo cùng một pipeline, bạn không muốn copy-paste YAML — DRY giúp dễ bảo trì.
File .github/workflows/build-and-push.yml ở repo org/ci-templates:
name: Reusable Build & Push
on:
workflow_call:
inputs:
image-name:
required: true
type: string
secrets:
DOCKER_PASSWORD:
required: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- uses: docker/build-push-action@v6
with:
push: true
tags: ${{ inputs.image-name }}:${{ github.sha }}
Gọi từ repo bất kỳ:
jobs:
call-build:
uses: org/ci-templates/.github/workflows/build-and-push.yml@main
with:
image-name: ghcr.io/org/myapp
secrets:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
Khác với reusable workflow, composite action là một tập steps dùng chung — nhúng vào job hiện tại. Tạo file action.yml trong repo org/setup-toolchain:
name: 'Setup Toolchain'
description: 'Cài Node, pnpm, restore cache'
inputs:
node-version:
default: '20'
runs:
using: 'composite'
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- uses: pnpm/action-setup@v4
with:
version: 9
- run: pnpm install --frozen-lockfile
shell: bash
Dùng:
steps:
- uses: actions/checkout@v4
- uses: org/setup-toolchain@v1
with:
node-version: '22'
- run: pnpm test
Trong phần này bạn đã nắm:
Reusable workflow: file YAML đầy đủ trong repo template, expose qua on: workflow_call; gọi từ repo khác bằng uses: org/templates/.github/workflows/foo.yml@v1.
Composite action: file action.yml với runs: using: composite; nhúng nhiều bước vào 1 step của job hiện tại.
Khi nào dùng cái nào: reusable workflow cho flow lớn (toàn bộ job/multi-job); composite action cho tiện ích nhỏ dùng trong 1 step (setup toolchain, login registry…).
Versioning: pin bằng @v1 hoặc commit SHA — không bao giờ @main (supply chain attack vector).
Trong Phần 7, ta dựng một pipeline production hoàn chỉnh cho Next.js: test → build Docker image → push GHCR → deploy lên VPS, tận dụng GHA cache cho Docker layer và environment protection.