Jenkins Setup for PHP unit testing and selenium framework on AWS

Please visit my website and subscribe to my youtube channel for more articles

https://devops4solutions.com/

Login to AWS EC2 Linux Instance

Install Java : Click Here for more details

wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.rpmsudo yum localinstall jdk-8u171-linux-x64.rpmexport JAVA_HOME=/usr/java/jdk1.8.0_171/
export JRE_HOME=/usr/java/jdk1.8.0_171/jre

PATH=$PATH:$HOME/bin:$JAVA_HOME/bin

export PATH
sudo alternatives --config java

Install Jenkins : Click here for more details

sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.reposudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.keysudo yum install jenkinssudo yum install gitsudo service jenkins start

Open the url in the browser. Default port is 8080

http://localhost:8080/

It will ask for the initial password, please run the below command

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Jenkins setup with windows agent is completed successfully.

Now to run any job on windows slave, you have to provide the label as “windows” in any Jenkins job as shown below

Integrate Jenkins with Gitlab

Go to GitLab ->Profile Settings

Then, click “Access Token” and create a Jenkins API Token

Then, save and copy this API Token. You won’t be able to access it again

Install Plugins in Jenkins

Manage Jenkins -> Manage Plugins -> Gitlab,gitlab hook,junit,testng results

Configure the Jenkins Server with Gitlab Server

Go to Manage Jenkins -> Configure System and scroll down to the ‘GitLab‘ section. Write a custom connection name, enter the GitLab server URL in the ‘GitLab host URL‘ field and click Add -> Jenkinsbutton.

Then, fill required fields as shown below with the Jenkins Access API Token which we created in GitLab before.

Then, select this API Token credential and finally click “Test” and see the “Success” message as shown below

Setup SSH connection between Jenkins and Gitlab

Run on Jenkin Server

sudo -su jenkinsssh-keygen
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa

cat /home/ec2-user/.ssh/id_rsa.pub

Go to Gitlab setting -> SSH Keys -> paste the key

Jenkins ->Credentials -> Add Crdentials -> SSH username with private key

Copy key from here cat /home/ec2-user/.ssh/id_rsa

SSH setup is completed successfully.

Configure Windows Agent to clone git repo.

Manage Jenkins -> Manage Nodes -> Windows Node — Configure -> Add Tools Location

Configure Java and Git ( Installed Java and Git manually on windows agent)

Configuring webhooks in GitLab

This will trigger the Jenkins job as an when there is a push in the repository

Go to specific project in gitlab-> click on settings ->integration as shown below

Put the jenkins job project url and all default setting

http://34.228.188.96:8080/gitlab/build_now

Click on test and push events

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /job/Test_php/. Reason:
<pre> No valid crumb was included in the request</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>
</body>
</html>
Error after comitting in gitlabNo need for this -> disable csrf in security
configure credential — for anonymous access

All configuration is setup successfully.

Now we will create a jenkins job for running selenium test case

Create a Freestyle job in Jenkins

Set Label that this job will run only on windows agent

Job Successfully completed. Click on Build now and see the results.

Setup for PHP Unit test cases

Install php and phpunit on linux jenkins server

sudo yum install php56wget https://phar.phpunit.de/phpunit-5.6.1.phar
php phpunit-5.6.1.phar — version
chmod +x phpunit-5.6.1.phar
sudo mv phpunit-5.6.1.phar /usr/local/bin/phpunit

Manage Plugins -> Junit (if not already installed)

Create a freestyle project

/usr/local/bin/phpunit — log-junit test.xml -c phpunit.xml .

Job is successfully setup.

Written by

Devops Automation Enginneer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store