Deploy Laravel app to server using SSH only and github actions


name: Deploy Laravel to CentOS 8 Server as Root

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 30 # Adjust the timeout as needed

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: SSH Deploy as Root
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: root  # Change to 'root' to run as root
          password: ${{ secrets.SSH_PASSWORD }}
          script: |
            # Navigate to your deployment directory
            cd /var/www/project

            # Pull the latest code from your repository
            git pull origin main

            # Install Composer dependencies
            composer install --no-interaction --prefer-dist --optimize-autoloader

            # Run Laravel migrations and clear the cache
            php artisan migrate --force
            php artisan optimize:clear

            # Install Node.js (if not already installed)
            # This assumes Node.js is available in your server's repositories
            # If not, you'll need to install it manually.

            # run build
            npm install
            npm run build

            # Log the deployment date and time
            echo "Deployment completed: $(date)" >> deployment.log

No comments:

Post a Comment