How to Deploy Laravel Project Using CI/CD Pipeline, Step by Step Guide
In this article I will guide you step by step, how to deploy your Laravel application on shared hosting by using CI/CD pipeline with GitHub actions. Keep reading to find out how to receive the gift that is offered in this article.
Login to Cpanel
I assume that you have gotten perfect hosting for deployment of your project.
First you need to login to your Cpanel. Then you can see bunch of tools on the Cpanel dashboard. Next step is create a database for your project.
Create a Database for Your Project
There is a separate section for Databases.
Then create a new user and create a new database for your project. Keep your credential safely with you, we need them when we configure the environment file. Give all the permission for that project on that user. Then you can import the database of your project by simply using import option on your database on phpmyadmin.
Next step is create a separate GitHub repository for our project.
Create a GitHub Repository For Your Project.
In this CI/CD pipeline deployment process we have used GitHub actions. To use GitHub actions we need to have a separate GitHub repository for our Laravel project.
If you have no idea about how to create a GitHub repository you can refer this article - Push to your project on GitHub
Get Access to Your Server via FTP
Find File Manager icon on Cpanel. Then gain access to upload files to server using FTP(File Transfer Protocol) by creating a new FTP account. If you already have a FTP access you can use it.
Please keep in mind to keep your necessary username, password in a notepad or somewhere secure.
Add FTP Credentials to Repository's Github Secrets.
I think that you already have your FTP credentials with you. Store them as repository secrets. Read more Secrets of Repository on GitHub.
Create New workflow file
Then you need to go to actions tab on the top navigation of your repository then you need to create new workflow
You can be able to see main.yml file. paste below code on it.
on:
push:
branches: [ 'deployment' ]
name: 🚀 Deployment
jobs:
web-deploy:
name: 🎉 Deploy
runs-on: ubuntu-latest
steps:
- name: 🚚 Get latest code
uses: actions/checkout@v2
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Config Clearing
run: php artisan config:clear
- name: Cache Clearing
run: php artisan cache:clear
- name: Generate App Key
run: php artisan key:generate
- name: Generate storage link
run: php artisan storage:link
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install Npm
run: npm install
- name: Compile Css and Js Assets
run: npm run build
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
# - name: Create Database
# run: |
# mkdir -p database
# touch database/database.sqlite
# - name: Create Database
# run: |
# mkdir -p database
# touch database/foodntrack.mysql
- name: 📂 Sync files
uses: SamKirkland/[email protected]
with:
server: ${{ secrets.FTP_SERVER }}
username: ${{ secrets.FTP_USERNAME }}
password: ${{ secrets.FTP_PASSWORD }}
server-dir: /
Deployment
You are all set to go now CI/CD pipeline deployment. You just need to click on the run button if it not started it self. If you have done deployment process successfully, it will show green check button before your workflow name
Setup Environment Keys on .env file
A .env file has been created while the deployment process. So you just need to set up environment keys on .env file.
You can make changes as follows:
You are almost there! Now you can view your application by just searching your domain name on a browser. Ummm, looks alright but there is something missing. As you see the images are not showing on the server even though it's perfectly working on the local environment. So following step will be the last step that you need to follow alone.
Create Symlink to storage folder
So we have done everything perfectly except that image not showing error on production. So that occurs as we stored our images in Laravel storage. If you have already had a symlink to storage first remove it. Then create a new Symlink to the Storage from public path. To create a symlink first you need to open terminal on server, then goto project location then execute following command.
php artisan storage:link
Congratulations 🎉️ Your Laravel project has been hosted CI/CD pipeline using GitHub actions successfully. If you have anything need to have any guideline while you doing this tutorial, feel free to ask them on the comment section. I will answers them to instantly. Hope you learned something new from this article. Next meet on another article.Here I have a little gift for you. If you are looking for a perfect domain or host I think NameCheap is perfect for you. Buying a domain and host cost a lots of money. So I will give you coupon for get a discount up to 50%. Good Luck 🤟️.
WELCOME50