Simple CI/CD implementation through Github to AWS Beanstalk for Laravel

Gerry Sabar
5 min readApr 20, 2020

Before you following this article, first I assume you’ve set up a very basic working Laravel app repository in your own github, second you have deployed the Laravel app from your repo to AWS Bean stalk. Please follow this article if you haven’t set yet since these two things are the requirements before we can implementing CI/CD through Github & AWS beanstalk.

Also in case after you deployed your Laravel application to AWS beanstalk and got 403 forbidden error, make sure at configuration, you document root to public as example below:

also don’t forget to add APP_KEY to environment properties. Environment properties behave exactly like .env in your local Laravel project like example below to avoid error 500:

So I assume at least your AWS showing a very basic Laravel application from fresh Laravel installation as follow:

I try to explain this as simple as possible. Therefore, I’ll skip any complicated part such as connecting to the database since it’s not our main concern for now. But a working unit test from default Laravel configuration is required for advanced topic regarding to CI/CD implementation later.

To put it simple, CI/CD workflow can be described as follow:

I assume we already have github repo for basic Laravel app & running AWS Beanstalk for your basic Laravel app. Now, let’s proceed to create a pipeline. Let’s go to AWS CodePipeline and click Create Pipeline button. Next I insert value as follow then click Next button:

Since we try to understand the basic workflow we don’t need to add any setting in Advanced setting. In source provider select Github and then click Connect to GitHub button.

Followed by inserting correct repository & branch you’re going to add to pipeline, like example below and click Next button:

For now we’ll skip build stage, it’s for advanced user

Now we need to connect our pipeline to our Beanstalk target for deployment. Make sure you insert correct Beanstalk application & environment you want to connect, as an example below:

Last you only need to click Create pipeline button or maybe you want to review it first to make sure your setting is correct. Now your pipeline looks something like this:

You can do test, by editing welcome.blade.php file for example and then push your code to the Github repository and you’ll see this page is executed & deployed to targeted Beanstalk.

But hey, we’re missing the unit testing part aren’t we? Yep, now we’re going to last & interesting part. We’ll create additional command that’ll be executed when AWS pipeline is running. We’ll implement the unit testing. First in your root project directory create a folder named .ebextensions this is the default folder to add additional command / configuration for CI/CD implementation using AWS pipeline. Then create a file named 01.unit_test.config . each file inside .ebextensions folder is executed alphabetically so it’s good if you put numerical order suffix on each file, and let’s have a code as follow:

let’s push this commit to our Github repo and you’ll see right after you push the code, AWS pipeline is running again & deployed your code to AWS beanstalk & also execute Unit Test before deployed. So far maybe you’re asking to yourself, ok it’s working but how can I make sure this workflow is correct to prevent deploy if unit test is failed. From root Laravel project directory let’s open /Test/Unit/ExampleTest.php and replace the code inside:

public function testBasicTest()

from

$this->assertTrue(true);

to

$this->fail('Test force failure');

when you execute

$ ./vendor/bin/phpunit

obviously you’ll get error like this:

now you can try to push your changes to repo, and in AWS pipeline you’ll get result like this:

If you click the details at failed deploy section you can see the error detail that pointed to your unit test doesn’t pass. Congratulation, now you’re successfully implement basic CI/CD workflow!

Conclusion

This tutorial only covers a solid basic introduction to implement CI/CD from Github to AWS Pipeline. In real case, sometimes the pipeline isn’t as simple as this example. Even it may covers more than 2 stages because of the complexity the development of your project. But as long as you understand the concept you can grasp it fast. Also GitHub is not the only way to implement CI/CD with AWS Beanstalk. Maybe you’re wondering how to implement it with Gitlab? or Bitbucket? or other sources.

Gitlab for instance is suitable for advanced level. One among many solutions for Gitlab is you’re using Gitlab pipeline, zip the code from Gitlab pipeline then send to S3. Then you can create an AWS pipeline to trigger the process each time the pipeline find any update from uploaded file from Gitlab to S3 then do deploy to Beanstalk. This project repository can be accessed here.

--

--