Magento2 Continuous Integration and Continuous Deployment using Jenkins
Updated 20 June 2017
SaveTweetShare
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
Git server
Jenkins server
Staging server
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.
grant all privileges on production.*to'dbuserprod'@'%';
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
1
2
3
export APACHE_RUN_USER=test
export APACHE_RUN_GROUP=test
Virtual Host configuration
create a file /etc/apache2/sites-enabled/magento.conf and add the following content
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.
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.
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.
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 AuthenticationToken will be used in git webhook.
In Pipeline script copy the following codes.
1
2
3
4
5
6
7
8
9
10
11
node('master'){
stage('Set up')
sh'su - jenkins -c "bash setup.sh"'
stage('Build')
sh'su - jenkins -c "bash build.sh"'
stage('Deploy staging')
sh'su - jenkins -c "bash stage.sh"'
stage('Deploy production')
sh'su - jenkins -c "bash prod.sh"'
}
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.
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.
Be the first to comment.