2

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: /
  • CI/CD is a hard to configure, and you should test how the pipeline works after each step. No one will do this work for you. But in general, I would tell that Laravel is not working best on shared hostings, mainly because most of them do not provide SSH access, and you will probably end up having a lot of troubles while resolving issues related to path-sensitive settings. – PunyFlash Feb 05 '22 at 10:21
  • Hey. Thank you for the helpful guide. I try to do that but I understand it will waste my time. Thanks again. – Madushan Serasinghe Feb 05 '22 at 10:34
  • Did you try it before asking? – Wahyu Kristianto Feb 05 '22 at 11:27
  • Yes. I I tried to a lot. But could not success. I only could done deploy simple html page using github. But in the GitHub FTP Deploy package has option such as npm run, instal like wise. That's why I thought laravel application also may can deploy using that. Any way now I am going to buy laravel forge and vps host. It will easy for me. – Madushan Serasinghe Feb 05 '22 at 11:47
  • Yes, not only is it possible it is pretty easy to get set up. Laravel is fine on shared hosting. I have been using shared hosting with Laravel for years. But, your question doesn't have enough details for us to help you. For example, what isn't working? Errors, nothing uploaded, ...? Also, normally you don't check in your /vendor directory into GitHub so I would expect your GH Action file to have a section to install the dependencies. Please edit your question with what is and isn't working and we can help you get set up. – waterloomatt Feb 10 '22 at 20:00
  • Not sure if it'll be helpful but here is my master.yml, https://gist.github.com/waterloomatt/fff4fe14f3a3e28b7620a2b1dc602a43 – waterloomatt Feb 10 '22 at 20:02
  • Hi waterloomatt, If you could tell me how set up Laravel application to shared hosting using Github, it will so help full. I tried to find it on youtube and websites but could not find it. final I bought Laravel forge and cloud hosting because I thought there are no way to deploy Laravel applications using Github. Do you have any guide links please send me. I just want to know how to deploy the Laravel web application using Github. – Madushan Serasinghe Feb 11 '22 at 21:26

1 Answers1

7

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 action, 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

waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • Hello Waterloomatt, First of all, Thank you very much for this. I think I am doing something wrong because I tried this and successfully deploy it to shared hosting but when go to the domain it is not working. It shows 403 Forbidden error because of Laravel public folder (image is here: https://imgur.com/hfNZYJW ). this is my GitHub repo https://github.com/madushancs/laravel_github_action . – Madushan Serasinghe Feb 12 '22 at 03:30
  • I tried to deploy it to my dev subdomain. do I need to edit the .htaccess file? When I normally do zip public folder and other folders separately and put public folder files into domain folder and other files put out of the public folder and change index.php __DIR__ path into that such as ..apps/../vendor/autoload.php – Madushan Serasinghe Feb 12 '22 at 03:30
  • Change your document root to point to the public dir. – waterloomatt Feb 12 '22 at 12:19
  • 1
    Hey waterloomatt, It is working now. Thanks a lot for the guide me. – Madushan Serasinghe Feb 12 '22 at 13:26
  • What about running migrations (php artisan migrate)? – Leslie Joe Aug 08 '22 at 18:27
  • @LeslieJoe, see the post-deploy step, https://gist.github.com/waterloomatt/fff4fe14f3a3e28b7620a2b1dc602a43#file-master-yml-L38 – waterloomatt Aug 09 '22 at 15:02
  • Okay it may be possible. What about the configuration for database connection in .env file? Or should I manually add and edit the env file on cPanel for it to migrate tables? I do know that creating a new env file can be done on github action but when making and pushing changes to the repository, it will reset the database configuration to default so running migrations may fail. – Leslie Joe Aug 16 '22 at 15:12
  • Don't check your .env files in to your source control. They should remain separate. – waterloomatt Dec 05 '22 at 21:17