Skip to content

Instantly share code, notes, and snippets.

@leaguecodeuk
Last active June 17, 2024 03:53
Show Gist options
  • Save leaguecodeuk/0a6a25283eca246ee78a8eeb22ab2744 to your computer and use it in GitHub Desktop.
Save leaguecodeuk/0a6a25283eca246ee78a8eeb22ab2744 to your computer and use it in GitHub Desktop.
Laravel / PHP / Vue.Js / React.Js : Github Actions for Shared Hosting [ IONOS, HostGator ]
name: HostGatorDeploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}
- name: Use PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Copy .env
run: cp .env.example .env
- name: Install composer Dependencies
run: composer install --optimize-autoloader --no-dev -q --no-ansi --no-progress
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
cache: 'npm'
- name: Install node dependencies
run: npm install
- name: Setup Project
run: npm run build
- name: Deploy PHP to Server
if: ${{ success() }}
uses: fifsky/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
user: ${{ secrets.SSH_USERNAME }}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
command: |
cd github/sample-site
php artisan down
git pull
composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --optimize-autoloader --no-dev
php artisan migrate --force
php artisan optimize:clear
php artisan optimize
php artisan up
- name: Install SSH key
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa_auto_gh_actions
chmod 600 ~/.ssh/id_rsa_auto_gh_actions
ssh-keyscan -t rsa ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
- name: Copy Build Files to Target Server
run: |
rsync -avz -e "ssh -i ~/.ssh/id_rsa_auto_gh_actions -o StrictHostKeyChecking=no -p ${{ secrets.SSH_PORT }}" ${{ github.workspace }}/public/build/ ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:~/github/sample-site/public/build
name: IonosDeploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Copy .env
run: cp .env.example .env
- name: Install composer Dependencies
run: composer install --optimize-autoloader --no-dev -q --no-ansi --no-progress
- name: Install node dependencies
run: pnpm install
- name: Setup Project
run: |
pnpm run build
- name: Deploy PHP to Server
if: ${{ success() }}
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script_stop: true
script: |
cd github/sample-site
/usr/bin/php8.2-cli artisan down
git pull
/usr/bin/php8.2-cli ~/bin/composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist --optimize-autoloader --no-dev
/usr/bin/php8.2-cli artisan migrate --force
/usr/bin/php8.2-cli artisan optimize:clear
/usr/bin/php8.2-cli artisan optimize
/usr/bin/php8.2-cli artisan up
- name: Install SSH key
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa_auto_gh_actions
chmod 600 ~/.ssh/id_rsa_auto_gh_actions
ssh-keyscan -t rsa ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
- name: Copy Build Files to Target Server
run: |
rsync -r -e "ssh -i ~/.ssh/id_rsa_auto_gh_actions" ${{ github.workspace }}/public/build/ ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOST }}:~/github/sample-site/public/build

Laravel / PHP / Vue.Js / React.Js : Github Actions for Shared Hosting


Deploy Github actions.

  1. Test Ionos Shared Hosting (Using appleboy/ssh-action@master) Try this First
  2. HostGator Shared Hosting (Using fifsky/ssh-action@master )

Setting up SSH for GitHub Actions on Laravel Shared Hosting

This guide helps you configure SSH for deploying your Laravel application using GitHub Actions on a shared hosting environment.

Connect to your Hosting Server via SSH

Connect to your hosting server via SSH using the terminal or command prompt:

ssh username@host.com

Navigate to the .ssh folder on your server:

cd ~/.ssh

Generate SSH keys using ssh-keygen. You can optionally provide a name (e.g., github-actions) and a passphrase:

ssh-keygen -t rsa -b 4096

Or simply run:

ssh-keygen

This creates two files: github-actions (private key) and github-actions.pub (public key).

Add the public key (github-actions.pub) to the authorized_keys file to allow machines using the private key to access the server:

cat github-actions.pub >> ~/.ssh/authorized_keys

Add SSH Key to GitHub

  1. Copy the content of github-actions.pub.
  2. Go to your GitHub account settings.
  3. Navigate to Settings > SSH and GPG keys > New SSH key.
  4. Provide a title (any) and paste the copied key.
  5. Save the key.

Configure Repository Secrets in GitHub

  1. Go to your repository settings.
  2. Navigate to Settings > Secrets > Actions.
  3. Add the following repository secrets:
    • SSH_HOST: Your host address (e.g., host.com or IP address).
    • SSH_USERNAME: Your SSH username.
    • SSH_KEY: The private key name (github-actions).
    • SSH_PORT: your ssh port (optioinal, default is port 22)

For using github-actions, run:

cat ~/.ssh/github-actions

Copy the output and save it to SSH_KEY.

Additional Resources

For further guidance on setting up GitHub Actions for Laravel, you can refer to the following resources:


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