Deploy Laravel with GitHub Actions on Shared Hosting
Tags: cicd, laravel
Published: 2023-01-09 04:24:39

---
title: Deploy Laravel with GitHub Actions on Shared Hosting
published: true
date: 2023-01-09 03:45:59 UTC
tags: CICD,Laravel
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vde09gswtcgvvjb7vfm0.png
canonical_url: https://anlisha.com.np/blog/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
```

{% embed https://gist.github.com/anlisha-maharjan/bd244b5f20920702ed0c0ee105058347 %}

**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
```

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.

{% embed https://gist.github.com/anlisha-maharjan/0b1d7cd246e49ced8daace31491e3fe0 %}

Let me explain what each section does.

```
name: Deploy on push master
```

Just specifying a name for the workflow.

```
on:
  push:
    branches:
      - master
```

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

```
jobs:
  web-deploy:
    name: Deploy
    runs-on: ubuntu-latest
```

_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
```

_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"
```

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 }}
```

![](https://anlisha.com.np/blog/wp-content/uploads/2023/01/Actions-secrets-1024x663.png)

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.

![](https://anlisha.com.np/blog/wp-content/uploads/2023/01/actions-1-1024x444.png)

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

The post [Deploy Laravel with GitHub Actions on Shared Hosting](https://anlisha.com.np/blog/deploy-laravel-with-github-actions-on-shared-hosting/) first appeared on [Anlisha Maharjan](https://anlisha.com.np/blog).