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:
Section titled βπ― Tujuan Akhir:β-
Laravel app ter-deploy otomatis setiap push ke branch
prod -
Server ringan (RAM 2GB), tidak menggunakan Docker
-
User
deploynon-root, tanpa password -
Aman 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
Section titled βπ§± 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)
Section titled βπ 2. Setup SSH Key untuk GitHub Actions β Server (akses SSH)βDi lokal:
Section titled β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/.sshsudo nano /home/deploy/.ssh/authorized_keys# Paste isi github_deploy_key.pubchmod 600 /home/deploy/.ssh/authorized_keyschown -R deploy:deploy /home/deploy/.sshποΈ 3. Setup SSH Key untuk Server β GitHub (akses git pull)
Section titled βποΈ 3. Setup SSH Key untuk Server β GitHub (akses git pull)βDi server (user deploy):
Section titled βDi server (user deploy):βssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_repo_x -C "server-github"chmod 600 ~/.ssh/id_ed25519_repo_x-
Tambahkan 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
Section titled ββοΈ 4. Konfigurasi SSH Client (~/.ssh/config) untuk Multi Repoβ# Repo 1Host repo.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_repo IdentitiesOnly yesBisa ditambahkan lagi untuk repo lain:
# Repo KasirHost kasir.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_kasir IdentitiesOnly yesπ 5. Ubah Git Remote ke Alias yang Sesuai
Section titled βπ 5. Ubah Git Remote ke Alias yang SesuaiβContoh:
Section titled βContoh:βcd /var/www/your-repogit 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
Section titled βπ 6. Tambahkan GitHub ke known_hosts di Serverβssh-keyscan github.com >> ~/.ssh/known_hostschmod 644 ~/.ssh/known_hostsDiperlukan agar
git pulltidak gagal karena fingerprint unknown.
π 7. Atur Akses Folder Laravel untuk User deploy
Section titled βπ 7. Atur Akses Folder Laravel untuk User deployβsudo chown -R deploy:www-data /var/www/your-laravel-projectsudo chmod -R ug+rw /var/www/your-laravel-projectsudo find /var/www/your-laravel-project -type d -exec chmod 775 {} \;π 8. Izinkan deploy Restart Queue via supervisorctl (Tanpa Password)
Section titled βπ 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
Section titled βπ 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
Section titled βπ 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 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
Section titled βπ§ͺ 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:
Section titled βπ¦ 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 | β |