Published on

Deploy Laravel Project via SSH

Create a workflow

  • A Github action workflow is a set of instructions that consists different jobs and steps that can be triggered on events we mentioned above.

  • Workflows for a repository are stored inside .github/workflows in the root directory of your applications.

  • create a file called deploy.yml inside .github/workflows folder with the following content.

name: deploy
on:
  push:
    branches:
      - main
      - master
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Install SSH Key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          known_hosts: unnecessary

      - name: Adding Known Hosts
        run: ssh-keyscan -p ${{ secrets.SSH_PORT}} -H ${{ secrets.SSH_HOST }}  >> ~/.ssh/known_hosts

      - name: Start Deploying
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: ${{ secrets.SSH_PORT }}
          passphrase: ${{ secrets.PASSPHRASE }}
          script: |
            ls
            cd domain.com/public_html/
            php82 artisan down || true
            git pull
            composer install 
            php82 artisan migrate
            php82 artisan optimize:clear
            php82 artisan up