DEV Community

Daniel Mabadeje
Daniel Mabadeje

Posted on • Edited on

Automating Laravel CI/CD with GitHub Actions on Namecheap

Who is this article for?

Have you had some struggles hosting your Laravel application on namecheap? Or you have been looking for a way to automate your deployments to your namecheap server? This article is for you.

Lets talk about Github actions a bitπŸ™‚

GitHub Actions is a powerful tool for automating various workflows and tasks within the GitHub ecosystem. Here are some of the key benefits of using GitHub Actions:

  1. Continuous Integration and Deployment (CI/CD): GitHub Actions enables you to set up automated CI/CD pipelines, allowing you to build, test, and deploy your applications with ease. You can define custom workflows that trigger actions based on events like pushes, pull requests, or scheduled intervals. (This is what we will be focusing in this article)

  2. Flexibility and Customization: GitHub Actions provides a flexible and customizable platform where you can define workflows using YAML syntax. You have granular control over the steps, dependencies, and conditions of your actions, allowing you to create tailored workflows that fit your specific needs.

  3. Seamless Integration with GitHub: Since GitHub Actions is tightly integrated with the GitHub platform, it offers seamless integration with your repositories, issues, pull requests, and other GitHub features. You can leverage this integration to create workflows that interact with your repository, update issues, trigger notifications, and more.

  4. Scalability: GitHub Actions is designed to scale with your needs. You can run workflows on different types of virtual machines and specify resource allocation for each job. Additionally, parallel and matrix strategies allow you to distribute workloads across multiple machines, enabling faster execution times.

  5. Cross-Platform Support: GitHub Actions supports a wide range of platforms and operating systems, including Linux, macOS, and Windows. You can test your applications across different environments, ensuring compatibility and reliability.

  6. Cost-Efficient: GitHub Actions provides a generous amount of free usage every month, allowing you to automate your workflows without incurring additional costs. If you require more resources or have high-demand workflows, you can choose from various pricing options that align with your needs.

  7. Vast Ecosystem and Community Support: GitHub Actions has a large ecosystem of pre-built actions that you can use to perform common tasks. These actions, created by both GitHub and the community, cover a wide range of functionalities, from building and testing applications to deploying to various platforms. You can also create and share your own actions with others.

With all these mentioned, It is safe to say that Github actions is the real IDAN (magic but colloquially means boss.)πŸ˜‚

So How Do I Setup this Github Actions?

So first of all lets head to our cpanel, you shouls see something like this.

Image description

This particular step here should be followed if you want to deploy to a sub domain. If you want to deploy to the main domain itself, you may skip this step

Click on Subdomains

Fill in the details for your sub domain and click on create.Β 
This would automatically create the subdomain and also create the document root.

Sidenote: I prefer to use the subdomain I created as the actual document root. So I can have my subdomain field as danielsub, my document root would be danielsub.myazabox.site.
After we have successfully created the sub domain, we create an FTP account

What is an FTP Account?πŸ€”
According to Techopedia, A file transfer protocol account (FTP account) is a type of user account that enables the transfer of files with a host computer by using FTP services.
So basically we are creating an account to facilitate file transfer to a server.

On your CPanel, Navigate to the files section as shown in the image

Image description

Click on FTP Accounts. At this point you should have something like this.

Image description

Put in your desired login details and ensure you stored those details properly in maybe your notes or somewhere safe (you would need them). Also select domain you want to assign the ftp account to. In this case it would be the subdomain that we created recently.

When you are done, you can click on the "Create FTP Account" button.

If you look below, you should see a new ftp account created as shown below

Image description

to view your ftp details like Username, server details and in some cases Port. click on "Configure FTP Client"
Now we are done with CPANEL, lets move over to github and perform this magic.

At this point I believe you already have your Laravel project pushed to Github. If this has not been done, ensure you have pushed your laravel project to your repository. (This deployment procedure works for both personal and organisation account).

So for the purpose of this article, I will use a laravel application that I have pushed long ago.
Navigate to the repository that houses your laravel project, and click on the actions tab.

Image description

Click on "setup new workflow for yourself -> "

Image description

This would create aΒ .yml file for you to work with (mostly a main.yml file)

Copy the code below and paste it in your main.yml



name: Deploy Laravel Project on push
on:
  push:
    branches:
      - main
jobs:
  web-deploy:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Get the latest code
        uses: actions/checkout@v2.3.2
      - uses: actions/setup-node@master
      - name: Copy .env
        run: php -r "file_exists('.env') || copy('.env.example', '.env');"
      - name: Install Dependencies
        run: composer update --ignore-platform-reqs
      - name: Generate 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: πŸ“‚ Sync files
        uses: SamKirkland/FTP-Deploy-Action@v4.3.4
        with:
          server: ${{ secrets.YOURSITE_FTP_SERVER}}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.YOUR_PASSWORD }}


Enter fullscreen mode Exit fullscreen mode

You can use the code above in your main.yml or you could go to Samkirkland to view other use cases to deploy your application with Github actions. Note that it is best practice to create secret keys rather than hard coding the values.

The code above basically checks if there is a new push to the branch you specified. (In this case the main branch). Runs a composer update, generates a key with the laravel commands and also links the storage.

After this is done, it uploads the files to the document directory in your CPanel. Do not forget to change the username, ftp and password field to their respective values which you saved earlier on. To take it a notch higher, you can store them as SECRETS on github.

It is Important to note that your first deployment may take a long time to sync its files as it copies the newly created vendor folder (which houses a lot of files).
One more thing, If you are using an Apache server, there may be need to create aΒ .htaccess in your root directory. After theΒ .htaccess file creation, add the following code.



# RewriteEngine on

# # serve existing files in the /public folder as if they were in /
# RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
# RewriteRule (.+) /public/$1 [L]

# # route everything else to /public/index.php
# RewriteRule ^ /public/index.php [L]

# DirectoryIndex index.php
<IfModule mod_rewrite.c>

    RewriteEngine On
    # RewriteBase /public_html/

    RewriteRule ^$ public/index.php [L]

    RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

</IfModule>


Enter fullscreen mode Exit fullscreen mode

And Voila!!!πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰
You have successfully deployed a laravel application to your cpanel through github actions.

Follow me on twitterΒ : https://twitter.com/mabadejedanphp

Top comments (2)

Collapse
 
stas_7702602173bf3ccef914 profile image
Stas

Good plan - create a publicly accessible YML file in the git repository, then add your FTP password\details in it... voila!

Seriously, I appreciate the simplicity of the solution as an example, but I am more than sure that someone will just ctrl-c\v this :) So instead of saying "to take it a notch higher" it is IMHO an absolute must in the first place to use Secrets or Environment Variables.

Collapse
 
danielmabadeje profile image
Daniel Mabadeje

I agree!!

I will update it as well