3

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/[email protected]
      with:
        server: ${{ secrets.ftp_server }}
        username: ${{ secrets.ftp_username }}
        password: ${{ secrets.ftp_password }}
        server-dir: /
7
  • 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
    Commented Feb 5, 2022 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. Commented Feb 5, 2022 at 10:34
  • Did you try it before asking? Commented Feb 5, 2022 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. Commented Feb 5, 2022 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. Commented Feb 10, 2022 at 20:00

2 Answers 2

10

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/[email protected]
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/[email protected]
        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

8
  • 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: imgur.com/hfNZYJW ). this is my GitHub repo github.com/madushancs/laravel_github_action . Commented Feb 12, 2022 at 3: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 Commented Feb 12, 2022 at 3:30
  • Change your document root to point to the public dir. Commented Feb 12, 2022 at 12:19
  • 1
    Hey waterloomatt, It is working now. Thanks a lot for the guide me. Commented Feb 12, 2022 at 13:26
  • What about running migrations (php artisan migrate)?
    – Leslie Joe
    Commented Aug 8, 2022 at 18:27
0

First : We need to create FTP account on our Cpanel , Open the cpanel then go to FTP accounts then add username , choose the domain and add the password (and make sure there is no special characters and I will tell you why later😂)

Second : Go to your github repo and then go to Actions sections and choose set up a workflow yourself . Now we will start writing our own workflow on a YML file which a language to write human-readable data serialization language that is often used for writing configuration files.

we will write this insturctions you will find it on this gist on github

on: 
  push:
    branches:
        # Here you can choose the branch you want to update from
      - master
name: 🚀 Deploy website on push

jobs:
  web-deploy:
    name: 🎉 Deploy website
    runs-on: ubuntu-latest
    steps:
    # Checkout the repository
    - name: 🚚 Get latest code
      uses: actions/checkout@v4
    # sync files via FTP
    
    - name: 📂 Sync files via FTP
      uses: SamKirkland/[email protected]
        with:
          server: "Server Host"
          username: "FTP UserName"
          password: "FTP password"
          protocol: ftps

these instructions contains three main parts

First Part : which is about when to do the deployment (Every Push) ,

and what branch to get the code from and we choosed (master)

Second part : to checkout the repo to git the latest updates.

Third part : to sync the code via ftp account .

now commit then wait until the workflow finishes loading .

Congratsssss 💃 🚀 with every push your server will be updated with the latest code base.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.