GitHub Actions & GitLab CI/CD Advanced
Reusable workflow, composite action, matrix, service container, DAG, include, dan optimasi pipeline lanjutan.
Setelah pipeline dasar berjalan, tantangannya adalah skalabilitas, kecepatan, dan pemeliharaan. Modul ini membahas fitur lanjutan di GitHub Actions dan GitLab CI.
GitHub Actions: reusable workflow
Workflow yang dapat dipanggil workflow lain:
.github/workflows/test.yml:
on:
workflow_call:
inputs:
node-version:
type: string
default: "22"
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: npm
- run: npm ci && npm testPemanggil:
jobs:
test:
uses: ./.github/workflows/test.yml
with:
node-version: "22"GitHub Actions: composite action
Bungkus langkah berulang:
.github/actions/setup/action.yml:
name: Setup project
runs:
using: composite
steps:
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
shell: bashPakai:
- uses: actions/checkout@v4
- uses: ./.github/actions/setupService container
Butuh database untuk integration test:
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: test
POSTGRES_DB: app
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 5s
--health-timeout 5s
--health-retries 10
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
env:
DATABASE_URL: postgres://postgres:test@localhost:5432/appConcurrency
Batalkan run lama saat ada push baru:
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: trueIni menghemat menit runner dan menghindari deployment ganda.
GitLab: include dan extends
include:
- local: .gitlab/ci/test.yml
- template: Jobs/SAST.gitlab-ci.yml
.node:
image: node:22-alpine
cache:
key:
files: [package-lock.json]
paths: [node_modules/]
unit:
extends: .node
script:
- npm ci
- npm testextends mewarisi konfigurasi; include memecah pipeline.
GitLab: needs (DAG)
build:
stage: build
script: npm ci && npm run build
artifacts:
paths: [dist/]
test:
stage: test
needs: [build]
script: npm testneeds memungkinkan job berjalan tanpa menunggu seluruh stage sebelumnya.
GitLab: parallel matrix
test:
parallel:
matrix:
- NODE_VERSION: ["20", "22"]
image: node:${NODE_VERSION}-alpine
script:
- npm ci
- npm testGitLab: child pipeline
Untuk monorepo atau pipeline besar:
trigger-backend:
trigger:
include: backend/.gitlab-ci.yml
strategy: dependCache yang efektif
| Bahasa | Path cache umum |
|---|---|
| Node.js | ~/.npm, node_modules |
| Python | ~/.cache/pip |
| Go | ~/go/pkg/mod |
| Docker | BuildKit cache |
Kunci cache harus memasukkan lockfile agar invalid saat dependensi berubah.
Runner dan resource
- Gunakan runner dengan CPU/RAM sesuai build.
- Batasi concurrency agar tidak saling berebut resource.
- Pisahkan runner untuk job berat (Docker build, E2E).
- Gunakan disk yang cukup untuk cache dan image.
Keamanan pipeline lanjutan
- Pin action/image ke versi atau SHA.
- Batasi
GITHUB_TOKEN/CI_JOB_TOKEN. - Jangan jalankan pipeline dari fork dengan secret produksi.
- Gunakan protected environment untuk deploy.
- Scan dependency dan container.
- Audit perubahan pada file pipeline.
Pipeline adalah kode produksi
Siapa pun yang dapat mengubah pipeline dapat mengubah cara deploy dan mencuri secret. Lindungi file CI dengan code review dan branch protection.
Praktik: memecah pipeline monorepo
- Deteksi perubahan path (
paths/rules:changes). - Jalankan hanya job untuk service yang berubah.
- Gunakan reusable workflow/template.
- Cache dependensi per service.
- Deploy hanya service yang berubah.
Ringkasan
- Reusable workflow, composite action, include, dan extends mengurangi duplikasi.
- Service container memungkinkan integration test dengan database nyata.
needs/DAG mempercepat pipeline.- Cache, concurrency, dan runner yang tepat menjaga biaya dan kecepatan.
Selanjutnya
Pahami runner lebih dalam di GitLab Runner: Shell vs Docker Executor.