[GITHUB] CI CD Backend Laravel using ssh action - fourslickz/notes GitHub Wiki

FIRST THING TODO:

  • SERVER PRODUCTION
    1. git clone -b {development-branch} {github-repository}
    2. composer install
    3. cp .env.production .env
    4. php artisan key:generate
    5. chmod -R 777 storage bootstrap/cache

  1. github account setting add ssh-key from server production / development

  2. repo setting

    • {SSH_PRIVATE_KEY_PRODUCTION}
    • {SSH_USERNAME_PRODUCTION}
    • {SSH_HOST_PRODUCTION}
  3. setup workflow

name: Laravel CI/CD

on:
  push:
    branches: [ "main", "development" ]
  pull_request:
    branches: [ "main", "development" ]

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    steps:
    - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
      with:
        php-version: '7.4'
    - uses: actions/checkout@v3
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install --ignore-platform-req=ext-grpc
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      run: vendor/bin/phpunit

  deploy-production:
    name: Deploy Project to Production Server
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
    - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
      with:
        php-version: '7.4'
    - uses: actions/checkout@v3
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install --ignore-platform-req=ext-grpc
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      run: vendor/bin/phpunit
    - name: Deploy using ssh
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.SSH_HOST_PRODUCTION }}
        username: ${{ secrets.SSH_USERNAME_PRODUCTION }}
        key: ${{ secrets.SSH_PRIVATE_KEY_PRODUCTION }}
        port: 22
        script_stop: true
        script: |
          cd ~/apps/project-directory
          git pull origin main
          git status
          composer install
          cp .env.production .env
          php artisan key:generate