- Language: ๐ฎ๐ฉ Bahasa Indonesia | ๐ฌ๐ง English
-
Install Laravel
composer create-project laravel/laravel example-app
-
Install Deployer
composer require --dev deployer/deployer
-
Inisialisasi Deployer Jalankan perintah berikut dan pilih PHP saat diminta.
vendor/bin/dep init
-
Ubah file
deploy.phpSesuaikan filedeploy.phpdengan konfigurasi server Anda.<?php namespace Deployer; require 'recipe/laravel.php'; require 'contrib/npm.php'; // HARUS DIGANTI SESUAI KEBUTUHAN ANDA set('application', 'Nama Aplikasi Anda'); set('repository', 'git@github.com:user/repo.git'); // URL SSH untuk clone Git set('bin/php', '/usr/bin/php8.2'); // Sesuaikan path PHP di server Anda // HARUS DIGANTI SESUAI KEBUTUHAN ANDA set('keep_releases', 5); add('shared_files', ['.env']); add('shared_dirs', ['storage']); add('writable_dirs', ['bootstrap/cache', 'storage']); // ----- Hosts ----- // HARUS DIGANTI SESUAI KEBUTUHAN ANDA host('NAMA_REMOTE_ANDA') // Nama alias untuk server Anda (cth: production) ->setHostname('IP_SERVER_ANDA') // Hostname atau IP server ->set('remote_user', 'USER_SSH_ANDA') // User SSH di server ->set('port', 22) // Port SSH (default: 22) ->set('branch', 'master') // Branch Git yang akan di-deploy ->set('deploy_path', '~/public_html/nama-proyek'); // Path deploy di server // HARUS DIGANTI SESUAI KEBUTUHAN ANDA // ----- Tasks ----- task('deploy:secrets', function () { file_put_contents(__DIR__ . '/.env', getenv('DOT_ENV')); upload('.env', get('deploy_path') . '/shared'); }); task('deploy', [ 'deploy:prepare', 'deploy:secrets', 'deploy:vendors', 'deploy:shared', 'artisan:storage:link', 'deploy:publish', ]); after('deploy:failed', 'deploy:unlock'); // before('deploy:symlink', 'artisan:migrate');
-
Buat File GitHub Workflow
mkdir -p .github/workflows touch .github/workflows/main.yml
-
Isi file
main.ymlname: Deploy to Production on: push: branches: - master # Atau branch utama Anda jobs: deploy: name: Deploy to Server runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' - name: Install Composer Dependencies run: composer install --prefer-dist --no-progress --no-dev - name: Setup Deployer uses: atymic/deployer-php-action@master with: ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} ssh-known-hosts: ${{ secrets.SSH_KNOWN_HOSTS }} - name: Deploy env: DOT_ENV: ${{ secrets.DOT_ENV_PRODUCTION }} run: php vendor/bin/dep deploy NAMA_REMOTE_ANDA # Sesuaikan dengan nama host di deploy.php
-
Tambahkan Credentials ke GitHub Secrets Akses
Settings > Secrets and variables > Actionsdi repositori Anda dan tambahkan 3 secrets berikut:SSH_PRIVATE_KEY: Kunci privat SSH untuk mengakses server Anda.SSH_KNOWN_HOSTS: Sidik jari SSH dari server Anda.DOT_ENV_PRODUCTION: Seluruh isi file.envproduksi Anda.
-
Cara Mendapatkan
SSH_PRIVATE_KEYDi terminal server Anda, jalankan:# Buat kunci baru jika belum ada ssh-keygen -t rsa -b 4096 -C "email-anda@example.com" # Tampilkan kunci privat untuk disalin cat ~/.ssh/id_rsa
Salin seluruh output (termasuk
-----BEGIN...dan-----END...) ke dalam secretSSH_PRIVATE_KEY. -
Cara Mendapatkan
SSH_KNOWN_HOSTSDi terminal lokal Anda, jalankan:ssh-keyscan -p [PORT_SSH_ANDA] [IP_SERVER_ANDA]
Salin outputnya ke dalam secret
SSH_KNOWN_HOSTS. -
Selesai! Sekarang, setiap kali Anda melakukan
pushke branchmaster, GitHub Actions akan otomatis men-deploy proyek Anda.
-
Install Laravel
composer create-project laravel/laravel example-app
-
Install Deployer
composer require --dev deployer/deployer
-
Initialize Deployer Run the following command and select PHP when prompted.
vendor/bin/dep init
-
Modify the
deploy.phpfile Adjust thedeploy.phpfile with your server's configuration.<?php namespace Deployer; require 'recipe/laravel.php'; require 'contrib/npm.php'; // MUST BE REPLACED WITH YOUR DETAILS set('application', 'Your Application Name'); set('repository', 'git@github.com:user/repo.git'); // SSH URL to clone your Git repo set('bin/php', '/usr/bin/php8.2'); // Adjust the PHP path on your server // MUST BE REPLACED WITH YOUR DETAILS set('keep_releases', 5); add('shared_files', ['.env']); add('shared_dirs', ['storage']); add('writable_dirs', ['bootstrap/cache', 'storage']); // ----- Hosts ----- // MUST BE REPLACED WITH YOUR DETAILS host('YOUR_REMOTE_NAME') // An alias for your server (e.g., production) ->setHostname('YOUR_SERVER_IP') // Server hostname or IP address ->set('remote_user', 'YOUR_SSH_USER') // SSH user on the server ->set('port', 22) // SSH port (default: 22) ->set('branch', 'master') // Git branch to deploy ->set('deploy_path', '~/public_html/project-name'); // Deployment path on the server // MUST BE REPLACED WITH YOUR DETAILS // ----- Tasks ----- task('deploy:secrets', function () { file_put_contents(__DIR__ . '/.env', getenv('DOT_ENV')); upload('.env', get('deploy_path') . '/shared'); }); task('deploy', [ 'deploy:prepare', 'deploy:secrets', 'deploy:vendors', 'deploy:shared', 'artisan:storage:link', 'deploy:publish', ]); after('deploy:failed', 'deploy:unlock'); // before('deploy:symlink', 'artisan:migrate');
-
Create the GitHub Workflow File
mkdir -p .github/workflows touch .github/workflows/main.yml
-
Fill the
main.ymlfilename: Deploy to Production on: push: branches: - master # Or your main branch jobs: deploy: name: Deploy to Server runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' - name: Install Composer Dependencies run: composer install --prefer-dist --no-progress --no-dev - name: Setup Deployer uses: atymic/deployer-php-action@master with: ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} ssh-known-hosts: ${{ secrets.SSH_KNOWN_HOSTS }} - name: Deploy env: DOT_ENV: ${{ secrets.DOT_ENV_PRODUCTION }} run: php vendor/bin/dep deploy YOUR_REMOTE_NAME # Must match the host name in deploy.php
-
Add Credentials to GitHub Secrets Go to
Settings > Secrets and variables > Actionsin your repository and add the following 3 secrets:SSH_PRIVATE_KEY: The private SSH key to access your server.SSH_KNOWN_HOSTS: The SSH fingerprint of your server.DOT_ENV_PRODUCTION: The entire content of your production.envfile.
-
How to Get
SSH_PRIVATE_KEYIn your server's terminal, run:# Create a new key if you don't have one ssh-keygen -t rsa -b 4096 -C "your-email@example.com" # Display the private key to copy it cat ~/.ssh/id_rsa
Copy the entire output (including
-----BEGIN...and-----END...) into theSSH_PRIVATE_KEYsecret. -
How to Get
SSH_KNOWN_HOSTSIn your local terminal, run:ssh-keyscan -p [YOUR_SSH_PORT] [YOUR_SERVER_IP]
Copy the output into the
SSH_KNOWN_HOSTSsecret. -
Done! Now, every time you
pushto themasterbranch, GitHub Actions will automatically deploy your project.
