> ## Documentation Index
> Fetch the complete documentation index at: https://docs.skanesgalab.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# GitHub Actions (Workflows)

> Panduan mendalam GitHub Actions: konsep dasar, trigger event, secrets, contoh pipeline Node.js/Docker, hingga teknik tingkat lanjut (Matrix Build & OIDC).

## Apa itu GitHub Actions?

**GitHub Actions** adalah platform otomatisasi alur kerja (*workflow*) bawaan yang terintegrasi langsung di dalam setiap repositori GitHub. Kamu bisa menggunakannya untuk menjalankan pengujian otomatis, mem-build Docker Image, menerbitkan paket npm, hingga melakukan deployment ke AWS/VPS.

***

## Konsep Inti GitHub Actions

Berkas alur kerja disimpan di dalam direktori `.github/workflows/` dengan ekstensi `.yml` atau `.yaml`.

1. **Workflow:** Prosedur otomatis menyeluruh yang didefinisikan dalam berkas YAML.
2. **Events (Triggers):** Aktivitas yang memicu workflow berjalan (contoh: `push`, `pull_request`, `schedule` cron, atau `workflow_dispatch` manual).
3. **Jobs:** Kumpulan langkah (*steps*) yang dieksekusi pada mesin virtual (runner) yang sama. Secara default, jobs berjalan paralel.
4. **Steps:** Tugas individual di dalam job (dapat berupa perintah shell langsung atau menjalankan aksi siap pakai).
5. **Actions:** Unit kode mandiri yang dapat digunakan kembali dari komunitas (*GitHub Marketplace*).
6. **Runners:** Server virtual (Ubuntu, Windows, macOS) yang disediakan GitHub untuk mengeksekusi workflow.

***

## Contoh Alur Kerja Dasar: Test & Build

Buat file baru di `.github/workflows/ci.yml`:

```yaml theme={null}
name: CI Pipeline

on:
  push:
    branches: [ "main", "staging" ]
  pull_request:
    branches: [ "main" ]

jobs:
  test-and-lint:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Kode Repositori
        uses: actions/checkout@v4

      - name: Setup Node.js Runtime
        uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: 'npm'

      - name: Install Dependensi
        run: npm ci

      - name: Menjalankan Linter
        run: npm run lint

      - name: Menjalankan Pengujian Otomatis
        run: npm test
```

***

## Manajemen Rahasia (GitHub Secrets)

Jangan pernah menyimpan kredensial (seperti token Docker Hub, SSH Private Key, atau API Key AWS) langsung di dalam file YAML repositori publik!

1. Buka halaman repositori di GitHub $\to$ **Settings** $\to$ **Secrets and variables** $\to$ **Actions**.
2. Klik tombol **New repository secret**.
3. Buat nama variabel (misal: `DOCKERHUB_USERNAME` dan `DOCKERHUB_TOKEN`).
4. Panggil di dalam berkas workflow menggunakan sintaks `${{ secrets.NAMA_SECRET }}`:

```yaml theme={null}
      - name: Login ke Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
```

***

## Teknik Tingkat Lanjut (Advanced GitHub Actions)

### 1. Matrix Strategy (Pengujian Lintas Versi & OS)

Menguji aplikasi di berbagai versi runtime dan sistem operasi secara bersamaan tanpa menulis ulang kode job:

```yaml theme={null}
jobs:
  test-matrix:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        node-version: [16.x, 18.x, 20.x]

    steps:
      - uses: actions/checkout@v4
      - name: Gunakan Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm test
```

### 2. Job Dependencies (`needs`)

Secara default job berjalan paralel. Gunakan `needs` untuk mengatur agar job deploy baru berjalan HANYA jika job testing berhasil:

```yaml theme={null}
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: npm test

  build-and-deploy:
    needs: test # Menunggu job 'test' selesai dan sukses
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying to production server..."
```

### 3. Caching Dependensi untuk Akselerasi Pipeline

Mempercepat durasi workflow dari hitungan menit menjadi hitungan detik dengan memanfaatkan `actions/cache`:

```yaml theme={null}
      - name: Cache Node Modules
        uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
```

### 4. Deployment Otomatis via SSH ke VPS Server

```yaml theme={null}
      - name: Deploy ke Server VPS
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: ${{ secrets.VPS_HOST }}
          username: ${{ secrets.VPS_USER }}
          key: ${{ secrets.VPS_SSH_KEY }}
          script: |
            cd /opt/myapp
            git pull origin main
            docker compose down
            docker compose up -d --build
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.