DEV Community

Cover image for Deploy Laravel with GitHub Actions on Shared Hosting
Anlisha Maharjan
Anlisha Maharjan

Posted on • Originally published at anlisha.com.np on

Deploy Laravel with GitHub Actions on Shared Hosting

Let’s see the process to deploy Laravel app on shared hosting with SSH GitHub Action.

Prerequisites:

  • Set up SSH keys in the server.

  • Set up Laravel project in GitHub.

Let’s start!

Create Deployment Script:

Run the command below in the root of the project directory. A deploy.sh file is created inside the scripts folder. Paste the code snippet below into the deploy.sh file.

mkdir .scripts/
touch .scripts/deploy.sh
Enter fullscreen mode Exit fullscreen mode

Setup GitHub Actions:

Option 1:

Run the command below in the root of the project directory. A ci.yml file is created inside the workflows folder, the file can have any name but it should end with a .yml extension. Write the configuration code snippet below into the ci.yml file.

mkdir .github/
mkdir .github/workflows/
touch .github/workflows/ci.yml
Enter fullscreen mode Exit fullscreen mode

Option 2:

In the GitHub repository, click on Actions > set up a workflow yourself and write the configuration code snippet below into the ci.yml file. The file can have any name but it should end with a .yml extension.

Let me explain what each section does.

name: Deploy on push master
Enter fullscreen mode Exit fullscreen mode

Just specifying a name for the workflow.

on:
  push:
    branches:
      - master
Enter fullscreen mode Exit fullscreen mode

The above snippet triggers the workflow when one pushes to the master branch.

jobs:
  web-deploy:
    name: Deploy
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

jobs – Group all the jobs that run in the workflow. Specifying and setting up a web-deploy job.

runs-on: ubuntu-latest – Configures to run the workflow using the latest version of Ubuntu.

steps:
    - name: 🚚 Get latest code
        uses: actions/checkout@v2
Enter fullscreen mode Exit fullscreen mode

steps – Group all the steps that run in the web-deploy job.

uses: actions/checkout@v2 – Check-out repository so the workflow can access it.

- name: 📂 Deploy to server via ssh
        uses: appleboy/ssh-action@v0.1.7
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          port: ${{ secrets.PORT }}
          script: "cd /var/www/html && sh ./.scripts/deploy.sh"
Enter fullscreen mode Exit fullscreen mode

Using appleboy/ssh-action; any ssh commands can be remotely executed to the shared hosting server with SSH username/password provided.

Add GitHub Secrets:

Goto Settings tab on the GitHub repository, click on Secrets > Actions > New Repository Secret to add the server host, ssh username, password, and port.

For example: For Server host: HOST as Name and your server IP address as Value. For SSH port: PORT as Name and your ssh port as Value. 22 is the default ssh port. For example: For SSH username: USERNAME as Name and run whoami on your server and use the result as Value.

To access variables in the pipeline use the format below:

${{ secrets.HOST }}
${{ secrets.USERNAME }}
${{ secrets.PASSWORD }}
${{ secrets.PORT }}
Enter fullscreen mode Exit fullscreen mode

Now anytime one pushes to the master branch, the pipeline starts running a web-deploy job that deploys the Laravel app.

Goto Actions tab to monitor whether it’s running, successfully deployed, or failed.

This concludes a CI/CD pipeline for Laravel on GitHub. Thanks for reading!

The post Deploy Laravel with GitHub Actions on Shared Hosting first appeared on Anlisha Maharjan.

Oldest comments (0)