← Back to list

Continuous Integration with Laravel and GitHub Actions.

Test your Laravel code for code duplication, unused variables, clean code, code complexity via Github Actions and make your CI effortless.

Nehal Kapadiya in Geek Culture · 2021-08-18 06:15 · 212 claps · 6.3 min read
#laravel #github-actions #phpmd #phpcpd #continuous-integration
Open on Medium ↗
Wiki topics: 💻 · Programming ☁️ · DevOps & Cloud 🔓 · Open Source

Continuous Integration with Laravel and GitHub Actions.

Laravel Code Quality Testing via Github Actions

Laravel Code Quality Testing via Github Actions

In this article, we are going to implement the Continuous Integration part for Laravel Project with Github Actions. At the end of the article, you will have a working workflow where if you push the code to Github, it will test your code for Test Cases, Naming Conventions, Duplication, and Unusability. Let’s get this working then…

NOTE: It is being assumed that you are familiar with Laravel, Github and Composer.

Before starting, follow this tutorial to set up the laravel project with the git repo if you don’t have one already.

[embed]Connecting the Laravel Project on GitHub. So today we’ll go through how to connect our GitHub account to our project. In short, we’ll learn how to use git to push…medium.com

So I have created a laravel project and GitHub repo named laravel-github-actions.

For testing our code, we will use two packages that can be installed via composer in your project. From now on every command we run will be in our project directory, so please navigate to your project directory.

Now navigate to your git repository and open the actions tab.

URL: https://github.com/<user-name>/<repo-name>

Actions Tab

Actions Tab

Under the Actions tab, you will find the Continuous integration workflows title and in that list, you can find multiple ready to use available CI flows. For our application, we need to find Laravel Setup.

List of Continuous Integration Workflows

List of Continuous Integration Workflows

If you cannot find it in the list above, click on the More continuous integration workflows button at the end of the list, a new list will load and in that, you can search for Laravel and you will find the laravel workflow setup.

Laravel Setup for GithubActions

Laravel Setup for GithubActions

Click on the Set up this workflow button and a new page with editable laravel.yml file will open up like below:

laravel.yml file

laravel.yml file

For now, do not make any changes to the file, we will edit it later. Just click on the green-coloured Start Commit on the top right corner of the screen. Then a dropdown will open up and select Commit directly to the master option and click the commit button. After this go to your local machine, navigate to project dir in the terminal and run git pull to get the changes of laravel.yml in your local branch.

pull changes in your local

pull changes in your local

Now we are going to install **phpcpd **for checking code duplication. Run the following command to install the package:

composer require sebastian/phpcpd

After this open the laravel.yml file in your editor and copy-paste the following code at the bottom of the file.

- name: Check the duplicate code in app directory
  run: ./vendor/bin/phpcpd app/ --min-lines=5 --min-tokens=10
Only copy above part.
app/ => Directory in which we are checking the duplicate code. To add another directory copy paste the same command again with another directory.
--min-lines=5 => This will throw error when min 5 lines are duplicated. You can change this according to your requirement. 
-min-tokens=10 => This will throw error when min 10 tokens are duplicated. You can change this according to your requirement.

Now your laravel.yml file should look like this.

laravel.yml file after adding phpcpd command

laravel.yml file after adding phpcpd command

Here note that I have removed the pull request code from the file as for now we are only going to focus on the push event. Also, make sure that the indentation in your file is correct otherwise the code won’t work as this uses the yml. Now go ahead create a file in your app directory and add some duplicate functions. You can also use the below code to test:

/**
* @return bool
*/
public function function1(): bool
{
$use = 1;
do {
echo "The number is: $use <br>";
$use++;
} while ($use <= 5);
for ($use = 0; $use <= 100; $use += 10) {
echo "The number is: $use <br>";
}
return false;
}
/**
* @return bool
*/
public function function2(): bool
{
$use = 1;
do {
echo "The number is: $use <br>";
$use++;
} while ($use <= 5);
for ($use = 0; $use <= 100; $use += 10) {
echo "The number is: $use <br>";
}
return false;
}

Now go ahead, commit your code and push it to your branch. Then navigate to the Actions tab of your repository(see image Actions Tab). You can see that the build has failed and upon checking the report, you might see something like this:

Php code duplicate check.

Php code duplicate check.

If you remove the duplicate code and push the code again, you can see that build has passed and it will show the green tick mark. See below:

Github build passed successfully.

Github build passed successfully.

Now that we have checked the code duplication, we move on to checking the coding conventions. For that, we will install a new package, run the below command to install.

composer require phpmd/phpmd

After the installation of the package, we need to create an XML file that will contain the rules that our PHP code need to follow. So go ahead and create a phpmdrules.xml in your project directory and add the following code to it:

<?xml version="1.0"?>
<ruleset name="My first PHPMD rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
My custom rule set that checks my code...
</description>
<!--Rules to check the code-->
<rule ref="rulesets/codesize.xml" />
<rule ref="rulesets/cleancode.xml" />
<rule ref="rulesets/controversial.xml" />
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/naming.xml" />
<rule ref="rulesets/unusedcode.xml" />
<!--Files and folders which needs to be excluded from checking-->
<exclude-pattern>bootstrap/cache/*</exclude-pattern>
<exclude-pattern>bootstrap/autoload.php</exclude-pattern>
<exclude-pattern>*/migrations/*</exclude-pattern>
<exclude-pattern>*/seeds/*</exclude-pattern>
<exclude-pattern>*.blade.php</exclude-pattern>
<exclude-pattern>*.js</exclude-pattern>
<exclude-pattern>/app/Console/Kernel.php</exclude-pattern>
<exclude-pattern>/app/Exceptions/Handler.php</exclude-pattern><exclude-pattern>app/Providers/BroadcastServiceProvider.php</exclude-pattern>
<exclude-pattern>app/Providers/RouteServiceProvider.php</exclude-pattern>
<exclude-pattern>tests</exclude-pattern>
</ruleset>

The checks will be performed by the files passed as ref inside the rule tag. Inside the exclude-pattern tag, we have added some files and folders which not need to be checked for these rules. You can modify the rule and exclude-pattern as per your requirements. All the XML files of rules like codesize.xml, cleancode.xml, controversial.xml, design.xml, naming.xml,unusedcode.xml can be found under:

vendor/phpmd/phpmd/src/main/resources/rulesets/

You can get more information about the rules at the official documentation of phpmd. https://phpmd.org/documentation/

Now add the following code at the bottom of the laravel.yml file as we did during phpcpd.

- name: Perform phpmd script
  run: ./vendor/bin/phpmd app/ github phpmdrules.xml

If you remember, we created a file to test the duplicate code. You can use the same file and add some code which is not following the conventions like add variable and don’t use it, do not keep the function name in camelCase. Commit the code and push it to master and check the build under the Actions tab. It should fail and show the output like this:

phpmd code check fail

phpmd code check fail

Now correct the errors and push the code again, your code will pass.

For PHPUnit test cases, we don’t need to add any packages. Laravel comes with a pre-installed PHPUnit package. All you have to do is keep the command in the laravel.yml file and Github will execute it. So go ahead add this code at the end of the laravel.yml file, write some faulty test cases and check it.

- name: Execute tests (Unit and Feature tests) via PHPUnit
  env:
    DB_CONNECTION: sqlite
    DB_DATABASE: database/database.sqlite
  run: vendor/bin/phpunit

That’s it. All the checks have been added and now GitHub will check the code and inform you know about the errors. You can now play around with it. You can also disable the merge button when PR is created to merge the code in the master branch but for that, you will have to buy the Github subscription and configure the settings in your repository.

One last thing. You can define which branches need to be checked and which should not be checked for the code. For that you will have to add the following code under branches tag on push event:

  • name** => this will build for all the branches starting with name

I hope this article will help you to design the workflow for your application. Please let me know in the comments for mistakes/improvements/suggestions.

Happy Coding!


메타데이터
post_id
b30b11fbd995
slug
continuous-integration-with-laravel-and-github-actions-b30b11fbd995
url
https://medium.com/geekculture/continuous-integration-with-laravel-and-github-actions-b30b11fbd995
canonical_url
https://medium.com/geekculture/continuous-integration-with-laravel-and-github-actions-b30b11fbd995
author_url
https://medium.com/@nehalkapadiya
status
ok
fetched_at
2026-07-28 01:42:33