Magento2 Continuous Integration and Continuous Deployment using Jenkins

Updated 20 June 2017

In Continuous Integration and Continuous Deployment environment, there are many steps involved while deploying codes to the production server. Some of these steps are as follows-:

  • Developers push their latest codes to the repository
  • Code is fetched from the repository and built is made
  • The built code is deployed to the staging server
  • In staging server, some test cases are run
  • After passing the test cases, code is deployed to the production server

Jenkins is a great tool which can be used  to setup continuous integration and continuous deployment environment for Magento2.

In this tutorial, I will show you how to setup a continuous integration and deployment environment for magento2 using magedeploy2. If you want to know more about the magedeployer2 visit this link https://github.com/mwr/magedeploy2-base.git

I am using 4 servers in this setup

  1. Git server
  2. Jenkins server
  3. Staging server
  4. Production server

Below is the architecture of my continuous integration and deployment environment

  • When code is pushed to the git repository job trigger is generated at Jenkins through web hooks.
  • At first step Jenkins will setup build environment for magento2.
  • In second step it will fetch latest code from the git repository and build the code.
  • In third step the built code is deployed to the staging server.
  • In last step the built code is deployed to the production server.

Setting up your production server

Install apache and php dependencies


In production server, user magento is running the apache service. So I need to change the user www-data to magento in the /etc/apache2/envvars file


Virtual Host configuration

Create a new file /etc/apache2/sites-enabled/magento.conf and add the following codes


Download n98-magerun2


Install composer


Restart apache service


Install mysql server


create database and user for magento

Login to your root mysql user and create user and database

Setting up your staging server

Follow the same procedure as in the production server. Just make some changes in the virtual host configuration and database setup. In staging server user test is running apache service. Open /etc/apache2/envvars file and change user www-data to test


Virtual Host configuration

create a file /etc/apache2/sites-enabled/magento.conf and add the following content


create database and user for magento

Login to your root mysql user and create user and database

Setting up your Jenkins server

create database and user for magento


Download and setup composer as given in the production server setup.

create and login as non-root user


Download magedeploy2-base


create .config/composer/auth.json file and add your magento2 credential for repo.magento.com


create your default magedeploy2.php, staging.php and production.php


change the content of the files according to your need. Every time you run the Jenkins job the defaults php files inside site folder will be used for the build and deployment tasks.

magedeploy2.php


I am using https://username:password@mygitrepo.com/repo1/magedeploy.git in git url because I do not want prompt for username and password while cloning from git. We are providing the username and password in the url. So it will be easy for the automation process.

Here, https://mygitrepo.com/repo1/magedeploy.git is your actual git url for your repo.

staging.php


production.php


config


The files inside /home/testuser/site/Task are

  • BuildTasks.php
  • CleanupTasks.php
  • DeployTasks.php
  • MagentoTasks.php
  • SystemTasks.php
  • TaskAbstract.php

Some of the tasks required sudo access. So it will be difficult for us to automate the process because it will ask for password when sudo access is needed. We will remove sudo related tasks in these files.

In BuildTasks.php comment these lines


In CleanupTasks.php remove sudo


In SystemTasks.php  comment these lines


If your  user in staging and production server has sudo access you do not need to edit these files.

Create password less authentication from Jenkins server to staging and production server

Go to home directory of user jenkins


Now, you can execute command on remote server without password

Create scripts for running deploy tasks

I have created 4 scripts for deploying Magento

  1. setup.sh
  2. build.sh
  3. stage.sh
  4. prod.sh

setup.sh


build.sh


stage.sh


prod.sh


Now we need to execute these shell scripts through Jenkins pipeline.

Create pipeline jobs in Jenkins

Login to your admin panel in Jenkins.

Go to New Item -> Pipeline

Create a name for your pipeline and your job will be created.

Then got to Build Triggers and select Trigger builds remotely. Give your Authentication Token. The Authentication Token will be used in git webhook.

In Pipeline script copy the following codes.


In this pipeline script we are defining 4 stages. If one of the stage is failed, the Jenkins job will be stopped.

Now, save your Jenkins pipeline job.

Setting up your git repository

Create a repository for magento2. We will upload our files in this repository.

Now go to Settings of this repo and select Web hooks. Write the url you want to trigger when someone pushes the code into git.

When someone pushes the code http://example.com/hook.php will be called. In hook.php I am executing a curl command which will trigger the Jenkins job we have previously defined.

hook.php


In this code user and pass is the username and password of Jenkins admin user. The job name is deploy_magento and sgfed35e3d2 is the Authentication Token we defined at the creation of the Jenkins Job.

Now, we have completed setting up our server. For installing magento2 upload magento2 setup files in the git repository. This will trigger the job defined in Jenkins. Job will start automatically and start executing the scripts. When the job is completed you can visit your staging server and production server in the browser. If you want to install modules in magento2, just push your module codes to the git repository and it will be automatically tested and deployed to the production server.

If your job failed for some reason, you can check the build logs. This is all about setting up Continuous Integration and Continuous Deployment environment for magento2. If you have any query regarding the setup procedure, you can ask me in the comment.

 

IN CASE OF ANY QUERY, CONTACT US

 

 

 

 

 

Category(s) magento
author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home