GitHub Actions Fundamentals
Memahami workflow, event, job, step, runner, action, secret, matrix, cache, dan artifact di GitHub Actions.
GitHub Actions adalah platform CI/CD terintegrasi GitHub. Pipeline didefinisikan dalam YAML di
.github/workflows/ dan berjalan pada runner GitHub atau self-hosted.
Konsep utama
| Konsep | Arti |
|---|---|
| Workflow | Satu file YAML berisi otomasi |
| Event | Pemicu workflow (push, pull_request, schedule) |
| Job | Kumpulan step yang berjalan di satu runner |
| Step | Satu tugas: uses action atau run perintah |
| Runner | Mesin yang menjalankan job |
| Action | Komponen reusable dari Marketplace |
| Secret | Nilai rahasia terenkripsi |
| Artifact | File hasil job yang disimpan |
| Cache | Data yang dipakai ulang antar-run |
Workflow pertama
.github/workflows/ci.yml:
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm testEvent pemicu
on:
push:
branches: [main]
tags: ['v*']
pull_request:
workflow_dispatch:
schedule:
- cron: '0 2 * * *'| Event | Kegunaan |
|---|---|
push | Build/deploy saat ada commit |
pull_request | Validasi PR |
workflow_dispatch | Trigger manual |
schedule | Tugas berkala |
release | Saat release dibuat |
Job dan dependency
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run lint
test:
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
build:
runs-on: ubuntu-latest
needs: [lint, test]
steps:
- run: echo "Build setelah lint dan test lolos"needs membuat job berjalan berurutan. Job tanpa needs berjalan paralel.
Matrix build
Menguji beberapa versi sekaligus:
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node: [20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci && npm testCache
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-Cache mempercepat instalasi dependensi. Kunci harus berubah saat lockfile berubah.
Artifact
- name: Build
run: npm run build
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: dist
path: dist/Artifact berguna untuk meneruskan hasil build ke job lain atau menyimpan laporan test.
Secret dan environment
Simpan rahasia di Settings → Secrets and variables → Actions.
steps:
- name: Login registry
run: echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdinJangan echo secret
Secret disamarkan di log, tetapi tetap berisiko jika ditulis ke file atau dikirim ke layanan lain. Gunakan hanya di langkah yang membutuhkan.
Environment protection
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- run: ./deploy.shEnvironment dapat mewajibkan approval manual dan membatasi secret.
Permission minimum
permissions:
contents: read
packages: writeTetapkan permission eksplisit agar token tidak memiliki akses berlebih.
Conditional step
- name: Deploy
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: ./deploy.shAction dari Marketplace
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}Pin action ke versi major atau commit SHA untuk stabilitas dan keamanan.
Praktik: pipeline CI lengkap
name: CI
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
packages: write
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npm test -- --coverage
- uses: actions/upload-artifact@v4
if: always()
with:
name: coverage
path: coverage/Ringkasan
- Workflow GitHub Actions berada di
.github/workflows/. - Event memicu job; step menjalankan action atau perintah.
- Matrix, cache, dan artifact mempercepat serta mengorganisasi pipeline.
- Secret, environment, dan permission harus dibatasi.
Selanjutnya
Bandingkan dengan GitLab CI/CD Fundamentals.