Auto Deploy Laravel via GitHub Actions (Tanpa Docker) β
Ini adalah rangkuman lengkap tentang cara melakukan auto deploy Laravel menggunakan GitHub Actions tanpa Docker. Saya membuat ini dengan bantuan chatGPT, termasuk rangkuman yang akan kamu di bawah ini.
π― Tujuan Akhir: β
Laravel app ter-deploy otomatis setiap push ke branch
prodServer ringan (RAM 2GB), tidak menggunakan Docker
User
deploynon-root, tanpa passwordAman dan terpisah antara:
- SSH key untuk GitHub β Server
- SSH key untuk Server β GitHub
Support multiple repository dengan key & remote berbeda
π§± 1. Membuat User deploy di Server β
sudo adduser --disabled-password --gecos "" deployIni membuat user
deploytanpa password login dan tanpa sudo secara default.
π 2. Setup SSH Key untuk GitHub Actions β Server (akses SSH) β
Di lokal: β
ssh-keygen -t ed25519 -f ~/.ssh/github_deploy_key -C "github-deploy"Upload private key ke GitHub Repo:
Settings β Secrets β Actions β SSH_PRIVATE_KEY- Value: isi dari
github_deploy_key(private)
Tempatkan public key di server (user
deploy):
sudo mkdir -p /home/deploy/.ssh
sudo nano /home/deploy/.ssh/authorized_keys
# Paste isi github_deploy_key.pub
chmod 600 /home/deploy/.ssh/authorized_keys
chown -R deploy:deploy /home/deploy/.sshποΈ 3. Setup SSH Key untuk Server β GitHub (akses git pull) β
Di server (user deploy): β
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_repo_x -C "server-github"
chmod 600 ~/.ssh/id_ed25519_repo_xTambahkan public key
id_ed25519_repo_x.pubke GitHub repo:Repo β Settings β Deploy Keys β Add Deploy Key β Centang Allow write access jika perlu
βοΈ 4. Konfigurasi SSH Client (~/.ssh/config) untuk Multi Repo β
# Repo 1
Host repo.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_repo
IdentitiesOnly yesBisa ditambahkan lagi untuk repo lain:
# Repo Kasir
Host kasir.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_kasir
IdentitiesOnly yesπ 5. Ubah Git Remote ke Alias yang Sesuai β
Contoh: β
cd /var/www/your-repo
git remote set-url origin git@your-repo:username/repo.gityour-repo adalah nama repomu bisa akan dibuat alias untuk remote ssh.
Harus sesuai dengan
Hostdi SSH config
Verifikasi:
git remote -vπ 6. Tambahkan GitHub ke known_hosts di Server β
ssh-keyscan github.com >> ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hostsDiperlukan agar
git pulltidak gagal karena fingerprint unknown.
π 7. Atur Akses Folder Laravel untuk User deploy β
sudo chown -R deploy:www-data /var/www/your-laravel-project
sudo chmod -R ug+rw /var/www/your-laravel-project
sudo find /var/www/your-laravel-project -type d -exec chmod 775 {} \;π 8. Izinkan deploy Restart Queue via supervisorctl (Tanpa Password) β
sudo visudoTambahkan:
deploy ALL=(ALL) NOPASSWD: /usr/bin/supervisorctl restart queue-allπ 9. Tambahkan Secrets di GitHub Repository β
| Secret Name | Value |
|---|---|
SSH_PRIVATE_KEY | Private key dari github_deploy_key |
DEPLOY_SERVER_USER | deploy |
DEPLOY_SERVER_IP | IP server kamu |
DEPLOY_APP_PATH | /var/www/your-laravel-project |
π 10. GitHub Actions Workflow: .github/workflows/deploy-prod.yml β
name: Deploy Laravel to Production
on:
push:
branches:
- prod
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: π₯ Checkout Repo
uses: actions/checkout@v3
- name: π Setup SSH Key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: π‘ Add known hosts
run: |
ssh-keyscan -H ${{ secrets.DEPLOY_SERVER_IP }} >> ~/.ssh/known_hosts
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
- name: π Deploy to Server
run: |
ssh ${{ secrets.DEPLOY_SERVER_USER }}@${{ secrets.DEPLOY_SERVER_IP }} << 'EOF'
ssh-keyscan github.com >> ~/.ssh/known_hosts
cd ${{ secrets.DEPLOY_APP_PATH }}
git pull origin prod
composer install --no-dev --optimize-autoloader
pnpm install
pnpm build
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
sudo /usr/bin/supervisorctl restart queue-all
echo "β
Deployment complete!"
EOFπ§ͺ 11. Test β
- Jalankan:
ssh -T github-repo-x- Jalankan:
git pull origin prod- Push ke branch
proddi GitHub β β GitHub Actions auto-deploy Laravel app ke server
π¦ Hasil Akhir: β
| Item | Status |
|---|---|
| Laravel deployed otomatis | β |
| Tidak pakai Docker | β |
| Multi repository support | β |
| Aman dengan SSH key terpisah | β |
Non-root user deploy | β |
| Supervisor queue restart | β |