PHP unit test using Jenkins and Ansible script

 

1.1 Create the Jenkins Job

Jenkins -> New Item -> Freestype project – >

Enter the project name

 

1.2 At the Build section,  “Add build step” ->  Execute shell , enter the ansible-playbook details to be executed

1.3 In the “Post-build Actions” ->  “Add post-build actions” -> “Publish JUnit test result report” -> and set “Test reports XMLs” to the php unit test output xml file

2. Ansible playbook

– hosts: host1
vars:
base_dir: /projectdir/
tasks:
– name: run testrunner
shell: php vendor/bin/phpunit –log-junit results/phpunit/phpunit.xml
args:
chdir: “{{ base_dir }}/api”
register: phpunit_output
ignore_errors: yes

– name: rsync results/phpunit/phpunit.xml file to jenkins
command: rsync -vr root@host1:/project/results /var/lib/jenkins/jobs/Php-test/workspace/
delegate_to: localhost

 

eg: host1 , host where the php project is located

Jenkins backup and Restore

To backup Jenkins, backup the JENKINS_HOME folder.

To restore Jenkins, sync back the JENKINS_HOME folder and restart jenkins.

JENKINS_HOME  directory structure  looks like the following:

JENKINS_HOME
 +- config.xml     (jenkins root configuration)
 +- *.xml          (other site-wide configuration files)
 +- userContent    (files in this directory will be served under your http://server/userContent/)
 +- fingerprints   (stores fingerprint records)
 +- plugins        (stores plugins)
 +- workspace (working directory for the version control system)
     +- [JOBNAME] (sub directory for each job)
 +- jobs
     +- [JOBNAME]      (sub directory for each job)
         +- config.xml     (job configuration file)
         +- latest         (symbolic link to the last successful build)
         +- builds
             +- [BUILD_ID]     (for each build)
                 +- build.xml      (build result summary)
                 +- log            (log file)
                 +- changelog.xml  (change log)

 

Script to Backup Jenkins:

Backup Jobs configuration file :

 rsync -av --include='config.xml' --exclude='**/**/**' /var/lib/jenkins/jobs/ root@192.168.1.2:/var/lib/jenkins/jobs/

Backup main configuration files:

 rsync -av --include='config.xml' --exclude='**/**/**' /var/lib/jenkins/plugins/ root@192.168.1.2:/var/lib/jenkins/plugins/

Backup main configuration files:

 rsync -av --include='*.xml' --exclude='**/**/**' /var/lib/jenkins/ root@192.168.1.2:/var/lib/jenkins/

Backup users configuration :

 rsync -av /var/lib/jenkins/user* root@192.168.1.2:/var/lib/jenkins/

Script to restore Jenkins:

Copy back the /var/lib/jenkins/ folder in 192.168.1.2 to the Jenkins main server and restart the jenkins service.

rsync -av /var/lib/jenkins/ roo@192.168.1.1:/var/lib/jenkins/
systemctl restart jenkins

 

 

 

Install Jenkins in Centos 6.8

1. Install Java

$ yum install java

2.  Installing the Jenkins repo

$ wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo

3.  Import the verification key

$ rpm –import https://jenkins-ci.org/redhat/jenkins-ci.org.key

4. Install Jenkins

$ yum install jenkins

Note: Download and install RHEL repo if needed,

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}
span.s1 {font-variant-ligatures: no-common-ligatures}

 

wget http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}
span.s1 {font-variant-ligatures: no-common-ligatures}

rpm -ivh epel-release-6-8.noarch.rpm

5. Start Jenkins

$ service jenkins start

6. Access the Jenkins using the url http://<ip-address>:8080
Then copy and paste the key  Administrator password from the file /var/lib/jenkins/secrets/initialAdminPassword

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}
span.s1 {font-variant-ligatures: no-common-ligatures}

 

7.  Select and install the required plugins

 

8. Create user first Admin User