laravelgithubdeploymentftp

Is that possible to deploy Laravel web application to shared hosting using GitHub Action & GitHub FTP Deploy?


Is that possible to deploy the Laravel web application to shared hosting using GitHub Action & GitHub FTP Deploy? If possible how should I change the.github\workflows\master.yml?

on: 
  push:
    branches:
      - master
name: 🚀 Deploy website on push
jobs:
  web-deploy:
    name: 🎉 Deploy
    runs-on: ubuntu-latest
    steps:
    - name: 🚚 Get latest code
      uses: actions/checkout@v2
    
    - name: 📂 Sync files
      uses: SamKirkland/FTP-Deploy-Action@4.2.0
      with:
        server: ${{ secrets.ftp_server }}
        username: ${{ secrets.ftp_username }}
        password: ${{ secrets.ftp_password }}
        server-dir: /

Solution

  • Looks like you're very close but are missing 2 important steps: set up a temporary PHP environment, and use that environment to install your dependencies (Composer).


    GitHub Actions Setup

    This guide assumes you have a working Laravel installation, a GitHub account, and a shared hosting account that you can access via FTP using a username/password.

    I found this video https://www.youtube.com/watch?v=UNWIXYSZfZY helpful to get a basic understanding of how to deploy a simple application. To make this answer helpful to a wider range of people, I'll give a quick outline of my setup. There really aren't any Laravel-specific steps.

    Workflow directory set up

    Create the directories .github\workflows at the root of your project. In the workflows directory, create a yml file named after the branch you want to push to your shared hosting account. Ex. master.yml, staging.yml, development.yml etc. If you only have a single branch then just create one file. The name is important and should match the name of the branch.

    Screen shot of workflow file

    Design your workflow

    This is very dependent on your project but assuming you have a basic Laravel application without the need for additional components such as Node, then this is a basic GitHub Action that works for me on a variety of projects.

    A basic action file consists of 2 sections, the workflow, and the jobs. A workflow triggers the jobs.

    Workflow

    Lines 1-4 say this will run each time we push to the master branch.

    on:
      push:
        branches:
          - master
    

    Line 5 is the name of this workflow and will show up on your Actions page. Set this to something descriptive.

    name: 🚀 Deploy website on push (Master)
    

    Setting up jobs

    In this section, there are 5 jobs. Some take parameters, others don't. I'm not going to explain all the details here but have linked to the corresponding repositories if you need details.

    1. Checkout your code so the workflow has access to it, https://github.com/actions/checkout
    name: 🚚 Get latest code
    uses: actions/checkout@v2
    
    1. Sets up a temporary PHP environment so you can run things like Composer, https://github.com/shivammathur/setup-php. Make sure to set your PHP version here otherwise you could run into issues when installing Composer packages with an unexpected PHP version.
    name: Setup PHP
    uses: shivammathur/setup-php@v2
    with:
      php-version: 7.2
    
    1. Caches your dependencies for faster deploys, https://github.com/actions/cache
    name: Cache Composer packages
    id: composer-cache
    uses: actions/cache@v2
    with:
      path: vendor
      key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
      restore-keys: |
        ${{ runner.os }}-php-
    
    1. Install your dependencies from composer.json and composer.lock files.
    name: Install dependencies
    run: composer install --prefer-dist --no-progress
    
    1. Deploys your code to your remote shared hosting site, https://github.com/SamKirkland/FTP-Deploy-Action. Note the use of ${{ secrets.ftp_username }} and ${{ secrets.ftp_password }}. These are set up in your repository's secrets section. See https://docs.github.com/en/actions/security-guides/encrypted-secrets
    name: 📂 Sync files
    uses: SamKirkland/FTP-Deploy-Action@4.0.0
    with:
      server: name_of_server.com
      username: ${{ secrets.ftp_username }}
      password: ${{ secrets.ftp_password }}
      server-dir: public_html/
    

    Final file

    on:
      push:
        branches:
          - master
    name: 🚀 Deploy website on push (Master)
    jobs:
      web-deploy:
        name: 🎉 Deploy
        runs-on: ubuntu-latest
        steps:
          - name: 🚚 Get latest code
            uses: actions/checkout@v2
    
          - name: Setup PHP
            uses: shivammathur/setup-php@v2
            with:
              php-version: 7.2
    
          - name: Cache Composer packages
            id: composer-cache
            uses: actions/cache@v2
            with:
              path: vendor
              key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
              restore-keys: |
                ${{ runner.os }}-php-
    
          - name: Install dependencies
            run: composer install --prefer-dist --no-progress
    
          - name: 📂 Sync files
            uses: SamKirkland/FTP-Deploy-Action@4.0.0
            with:
              server: name_of_server.com
              username: ${{ secrets.ftp_username }}
              password: ${{ secrets.ftp_password }}
              server-dir: public_html/
    

    Running the workflow

    1. Check-in .github\workflows\master.yml, and others if appropriate, into your GitHub repository. Without these files checked in nothing will happen when you push a change to the branch.

    2. Go to your Actions tab and ensure the workflow shows up there. Action present

    3. Push a change to your branch and watch the Actions tab. Click into the running action to see details about the run. Action
details

    4. Fix any errors that show up in the console.

    Finally, you mentioned in a comment something about NPM. If you have Node as a component in your project you can simply run two extra steps that will bundle your assets and will get deployed along with the rest of the code.

    Good luck! Node run