Skip to content

Instantly share code, notes, and snippets.

@hellomustaq
Last active September 30, 2023 07:11
Show Gist options
  • Save hellomustaq/6f8a6f9cf0dcb16ae14c7dd77647cbf1 to your computer and use it in GitHub Desktop.
Save hellomustaq/6f8a6f9cf0dcb16ae14c7dd77647cbf1 to your computer and use it in GitHub Desktop.
Laravel App Auto Deploy with Github Actions
name: Build & Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy Laravel app
uses: appleboy/[email protected]
env:
COMPOSER_ALLOW_SUPERUSER: 1
GITHUB_USERNAME: ${{ secrets.GH_USERNAME }}
GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
with:
host: ${{secrets.IP}} # IP address of the server you wish to ssh into
key: ${{secrets.SSHPRIVATESERVER}} # Private or public key of the servers
username: ${{ secrets.USER }} # User of the server you want to ssh intoo
envs: COMPOSER_ALLOW_SUPERUSER,GITHUB_USERNAME,GITHUB_TOKEN
script: |
cd /var/www/web-pos/
git pull origin main
composer install --ignore-platform-reqs
php artisan migrate --force
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
echo 'Deployment successful to digital ocean'

Automatic Deployment with GitHub Actions

Supporting Doc | YML file sample | Possible issue

1. Create an AWS or DigitalOcean Droplet: Set up your AWS or DigitalOcean droplet that will serve as your deployment server.

2. Access Droplet via Terminal: Use SSH to access your droplet through the terminal.

3. Generate SSH Key Pair: On the droplet, generate an SSH key pair by running the following command:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

4 Copy the Public Key: View the public key by running:

cat ~/.ssh/id_rsa.pub

Copy the displayed public key.

5. Add Public Key to GitHub: Add the copied public key to your GitHub account. Go to GitHub Settings > SSH and GPG keys, then add it in the SSH section. This step allows your repository to be cloned from GitHub to the server.

6. Edit Authorized Keys: Back on the server terminal, open the authorized_keys file with a text editor:

nano ~/.ssh/authorized_keys

Paste the copied public SSH key then press Control+X and accept changes.

7. Local Machine SSH Key: If your local machine doesn't already have a 4096-bit SSH key, create one by running:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

8. Copy Local Machine Secret Key: Copy the secret key from your local machine by running:

cat ~/.ssh/id_rsa

9. Add Secret Key to Repository: In your GitHub repository's Settings, go to Secrets and Variables > Secrets > Add Secret. Name it "SSHKEY" or a preferred name and paste the secret key.

Add 2 more secret one is "IP" another is "USER". Ip is the server public ip address and user for server user.

10. Add More Secrets: In the same section, add two more secrets: one for the droplet's IP address and another for the droplet access user.

11. Copy Local Machine Public Key: Run the following command on your local machine:

cat ~/.ssh/id_rsa.pub

Copy the displayed public key.

12. Edit Server's Authorized Keys: On the server, open the authorized_keys file for editing:

nano ~/.ssh/authorized_keys

Paste the copied local machine's public SSH key into this file.

13. Create deploy.yml File: Create a deploy.yml file in the .github/workflows/ directory of your GitHub repository with the following content:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment