In this module you will create two jobs. The first job will run our project's unit tests and, if it is successful, deploy our website to the production box.
- From the Jenkins home page, click on New item
-
Enter Unit_test as the name for the job and select Freestyle project. Click OK
-
On General add a description and select GitHub project to add the project's URL
-
On Source Code Management select Git and add the repo's clone URL on the Repository URL text box. On the Credentials dropdown menu, select "jenkins"
-
On Build Environment select Delete workspace before build starts.
-
On Build click on Add build step and select Execute shell
-
On the Command box, type:
cd ./app/
npm i --save-dev
node_modules/mocha/bin/mocha test
- Click Save
To test the job, click on Build Now
- From the Jenkins home page, click on New item
-
Enter deploy_app as the name for the job and select Freestyle project. Click OK
-
On General add a description.
-
On Build Triggers select Build after other projects are built and add the name of the previous job on the Projects to watch text box. Click Trigger only if build is stable
-
On Build click on Add build step and select Execute shell
-
On the Command box, type:
cd /var/jenkins_home/workspace/Unit_test/app
rm -rf ./node_modules
tar -cvf workshop_node_app.tar .
ssh -i /var/jenkins_home/.ssh/id_rsa [email protected] rm -rf /home/jenkins/tmp/
ssh -i /var/jenkins_home/.ssh/id_rsa [email protected] mkdir /home/jenkins/tmp/
scp -i /var/jenkins_home/.ssh/id_rsa workshop_node_app.tar [email protected]:/home/jenkins/tmp
ssh -i /var/jenkins_home/.ssh/id_rsa [email protected] tar -xvf /home/jenkins/tmp/workshop_node_app.tar -C /home/jenkins/tmp
ssh -i /var/jenkins_home/.ssh/id_rsa [email protected] chmod 700 /home/jenkins/tmp/deploy_app.sh
ssh -i /var/jenkins_home/.ssh/id_rsa [email protected] /home/jenkins/tmp/deploy_app.sh
-
Click Save
-
Go back to the Unit_test job and click Configure
-
On the Post-build Actions section click Add post-build Actions and select Build other projects
- Enter the project name deploy_app and select Trigger only if build is stable
To test the job, click on Build Now Note if the jobs fails with Host key verification failed try running docker exec -u root -it jenkins-kt bash and then ssh -i /var/jenkins_home/.ssh/id_rsa [email protected] rm -rf /home/jenkins/tmp/ from the ops machine.
The Nodejs site should be available at 192.168.1.3:8080





