[Devops] CI / CD with CircleCI and Deployer for Laravel project

Tram Ho

Hello everyone, after a long time of absence, I came back here, the last time was too busy, so I could not share much, only there was a series about Deploy, so I did 1 article about CI / CD using CircleCI and Deployer everyone.

Like ae Sun, there is SunCI and if you want to setup to be naughty, you have to be keymember or leader or higher to fetch the project to test SunCI so it is quite inadequate.

But come to CircelCI, people build comfortable, functions and interfaces in my opinion are even smoother than SunCI. Ok ramble that let’s start.

To be able to do this lesson, make sure everyone has done it or has done it before:

An extra option if you want to write Test for your project, you need to have experience with UnitTest and IntegrationTest

Finally, to be able to do well and understand CI / CD, you must have quite a lot of experience in Build Server, Auto deploy, Check convention JS, PHP, Config tested in laravel project, Know how to use docker, build dockerhub …

OK, preparing knowledge is enough, let’s develop =))

Setup config.yml file

To get started, everyone must have a project (laravel), 2 must be using Github or Bitbucket.

After you’ve met the two conditions above, in the root folder of your project, you create a folder named .circleci and inside there create another file config.yml. After creating, your folder structure will look like this:

OK its basic content will be as follows

Explain through some syntax mn

  • Including 2 main parts is docker and steps
    • First you can see in the docker you are using an image called framgia / laravel-workspace . For those of you who have known about Docker or not yet, Docker is simply an image like a box with some things installed to support us in running test projects such as php, composer, git and tool to check conventions. for php is phpcs and a few other tools. The specific content image you can see here .
    • Next, the steps section is the step we will run for our project, specifically:
      • checkout – Clone code from github – this is the default command of CircleCI.
      • run: [command] – Commands to execute.
      • restore_cache – Restore the folder containing the package (vendor /) based on the composer.lock file
      • save_cache – Save the vendor vendor folder if the composer.lock file has been changed or you deleted or added a new package.
      • The cache of the vendor folder helps speed up your build for later runs because instead of having to reinstall the packages, then you can reuse the vendor folder of the previous build if the composer.lock file is not change.

After configuring, you can push the code to the repo, guys

Init project

Everyone go to https://circleci.com/ then login with github or Bitbucket of everyone, I use github to get used to it, after login is complete it will redirect to the dashboard page is ok

Everyone choose to enter ADD PROJECT Then select the project that needs Setup, Everyone select your project, click the Set up Project button

At this screen, there will be a place to select Operating System, language, it is already default for me, my PHP code =)) Now choose Start Building is ok, select then it will switch to the build screen, and It will run the first build

Wait it build 1 bit ok

Success is right, cool, first floor is smooth like that because I have run the phpcs test first in the local already, and if mn and fail then click on detail to see what it failed to fix then push again

For example, if this fails eslint, I just need to see where it fails then fix and push it again

CI

OK, as above, everyone can build one by themselves, now check more, check ESLINT, TEST in laravel. Please update your config.yml file

To be able to run Test in laravel, you need to have a database to run the test, that’s when adding the image mysql: 5.7

Now everyone configures some environment variables to be able to connect, You need to make sure that your .env.testing.example file also has a database declaration with information about the host, name The database, username and password coincide with the item we just added above as follows:

After doing the above two things, we continue to add the following command in the file .circleci / config.yml as follows:

Each command trembles, adding two sub-commands, name and command, so that when it builds, it displays the name for it to be good instead of displaying the command. In this paragraph, I have to add check convention of js with Eslint.

This should ensure that projec has been setup in projec. Who has not found out, review here

To run the test, make sure the file env of mn must have the correct key, add it and run it ✌️

Followed by a standard check PHPCS Framgia . I think this standard is quite good, it only captures the most necessary conventions. but according to psr2’s full .. it catches a lot, which is writing doc for function for class … everything in the world.

Next is the test connect to the database to see if it is ok and run migrate

The framgia-ci is a tool of Framgia, it is available in the image framgia / laravel-workspace mn, it is open source so please take it and use it hihi

The last part is to run tests

OK, good, push it up and try to see how it runs, but remember to manipulate this, create a new branch and push to create pull compare with develop. After pushing it up, it is building a JS error, ok see the error and check under local and push it up

It’s green, right, look like it wants to merge =))

CD

The upper part is CI setup ok then right, after checking CI all ok then auto deploy only, =)) I find the CD part of this guy is quite good, in the config file I can customize a lot of things, such as CI pass, it must be approved to be deployed to the Development environment, then require to run in turn, Deploy finished this environment to deploy another environment, The interface so that I can imagine how the process of it is quite intuitive.

For example, a process like this

When people finish setting up the Circle build it will be very intuitive and each job will be a build

OK, in theory, that’s the rest, I can deploy it with at most 1-2 environments, like this article, I will only deploy automatically to the development environment.

To do this, everyone must have it

Anyone who has ever set up the server knows that one way to access the server is via ssh-key, this Circle too, if you want to deploy auto, you must allow this CircleCI ssh to access the server. Right, so do the following

OK after having the server, and the server has been gen ssh okay, everyone copy the private key in the ~ / .ssh / id_rsa directory and paste this into SSH Permission in the project’s settings section on CircleCI.

Insert the hostname and privakey of mn here, click ok and you’re done

Mine is private, so show it up for me to see where hehe, After I have added ok, I need to adjust the config.yml file

Add 1 part of deploy development to the job section, in this there will be 1 image framgiaciteam / deb-deploy: 7.3, this one I use by Sun, I can free to build the image make by me for the clams also, And finally, run the deployer command only

Ok that’s okay, and deployer config how to see it in the article I noted above.

Cicle, this guy has a function to run under the workflows, in this workflow, I want to run if he runs, or I can condition that this guy will pass the other guy quite well,

Through this section, the player immediately guess what to do right, that is we will run the job

run build_and_test => deploy_development (Requires build_and_test guy to pass all before running deploy, and in this deploy only run when the code is updated in the develop branch)

. OK, good product, push the code up, everyone put on the branch mn code, wait for it to pass, then merge into develop. When merged, it will have a build

This build is just to check the convention and run the Test

When the build is delicious, it will run job auto deploy

People who click on the other running place will see 2 jobs of mn already configured

It’s done. Waiting for the other build to pass is delicious, let’s have a cup of coffee to enjoy.

In this guy, I prefer the approved part, such as auto deploy to production or an environment where you need to think, for example, this function is quite reasonable.

We just need to add this code to the config file only

Then push up to feel offline: Go see the build it will look like this

Wait for the build test to finish, the pass will jump to the job, wait for my appprove, if I choose approved, I can go to the job deploy hehe

Image and feel offline

Notification

Part notification it supports slack but not chatwork so I am not config anymore, everyone try to see how. ✌️

Conclude

Ok this is my only post here, anyone who reads here is probably quite patient, because in order to be able to manipulate this article, to be able to setup well, your knowledge must also be in the form of pitiful =))

This time I’m doing series about CI / CD. I will do an autodeploy post with github action later, a tool of github has just been developed.

Share the news now

Source : Viblo