DevOps part 2 – Integrating CI / CD into a simple Laravel project

Tram Ho

What is CI? What is CD?

CI – Continuous Integration – Continuous Integration Continuous integration (CI) is a software development method that requires team members to integrate work regularly. Every day, the members have to follow up and develop their work at least once. This will be automatically checked by another team, which will conduct retrieval testing to detect errors as quickly as possible. The team found that this approach alleviates more integration problems and allows for faster cohesive software development.

CD – Continuous Delivery Continuous Delivery is a continuous delivery, a set of techniques for implementing an integrated souce code on a staging environment (an environment very similar to production).

Config CI (phpcs, phpunit) for a simple Laravel project

1. Create the Laravel API

1. Create a laravel project

We just need to go to the terminal and run the following command

Next, the init project just created on git

2. Config database

In this article SQLite will be used for testing and our main database. Usually, the main database is a more complex DBMS like MySQL or MSSQL but for simplicity’s sake. We will keep the configuration for the main database in the .env file, and then create a .env.testing file to keep the database configuration for testing.

In the nv.testing file we will configure the following:

To be able to point to our SQLite database file as we have configured in the .env.testing file, inside config/database.php must also replace the SQLite configuration in the connections array with the config below:

Then run the migrate command to check that our config was correct

Install token-based authentication with Passport

To authenticate authen I will use the laravel / passport package of Laravel

After the installation is complete, run the migrate command again to configure the laravel / passport related tables:

Passport requires encryption keys to generate tokens, which need to be generated and stored in a database. To generate these keys, we run the command

After running the command successfully, you will see your Client secret

The next step is to add the LaravelPassportHasApiToken trait to the AppUser model. We will use the helper methods from package laravel/passport to the app to help check user’s token and scopes. Open the file app/User.php and replace its content with the code below:

Next, we need to call the Passport::routes method in the booth function of the app/Providers/AuthServiceProvider.php

The final configuration that Passport requires setting is the driver option in the API authentication guard inside config / auth.php.

4. Create API endpoints

We will build a simple API for managing suser. User will have the following functions:

Sign up for a new account

Log in to their account using the user’s credentials

Fetch the user’s information

Sign out of the application

  • First we need to declare the routes/api.php file:

  • Next, create Controller

In AuthController we code the signup , login , logout , and user

  • Next, create a Unit test to test the API we just created

In our UserTest file will look like this:

Then we run the test

2. Automate the test with CircleCI

It’s time to introduce the power of CI / CD into our Laravel API. I will create a pipeline to ensure that when we push new code, our tests will automatically run and we get the CI / CD successful or failed pipeline status. (go)


About CircleCI a bit:

CircleCI is a tool to help us realize CI. There are many other well-known CI tools (Travis CI, Jenkins …), the reason I chose Circle CI is just because it is quite simple and fully functional that I need.

CircleCI uses docker, in Circle CI configuration we will specify the docker image to use and job , in job there are step , in step are specific command . There is also a filter configuration that allows us to flexibly adjust so that only jobs are run when there is a merge / push into certain branches and so on.

Description of the process of running 1 job on Circle CI:

Developers just need to push or merge into a branch, Circle CI automatically knows that event and boots up to the corresponding installed job. Initially the Circle CI pulls the docker image back and shakes it up in its cloud environment.

Next, it runs the installed steps in the docker container, usually the first step is always checkout i.e. git checkout gets the source (default saved in the ~ / project directory).

The next steps are run depending on your creativity, for example a job to build is usually npm install and then npm run abcxyz or a job to deploy, it may be aws s3 sync or serverless deploy …

After all the steps have been run, the job is finished. If the job’s exit code is error, by default we will receive a failed message again.

In short, after installing and configuring, you just need to build, run tests, deploy, and so on are completely automated and run instantly in the powerful free cloud environment of Circle CI.

1. Sign up for the plan

CircleCI provides users with 4 packages, here I will subscribe to the free package (because I have no skewers) you can see in the free package we have:

1500 minutes build

Unlimited repository and user

4x project build at the same time with public repository and 1x with private repository

After the installation is complete, it will move to the dashboard as follows:

2. Integration into the project

I will select the project created above demo-ci and then click setup project

Circleci supports us many languages ​​like PHP, Java, .NET, Go, IOS, Android, Python …. here I will use PHP

1. Integrate into my laravel project

Create a .circleci directory in the project In the .circleci directory Create a file called config.yml

After measuring we go to github click Start building to start building project. CircleCI will start running pipeline configuration. If you have followed the instructions correctly, you should have a successful build indicated by the screen below.

Perfect. We have been able to successfully power our Laravel API with a CI / CD pipeline that automatically runs tests using CircleCI.

We will add a test method fucntion login to the UserTest.php file and push the code to see how CircleCI automatically runs our newly added test.

Now save the file, commit and push to GitHub. CircleCI will once again run the pipeline with the tests, including the test cases we just added.


Reference source

Share the news now

Source : Viblo