Now days CI/CD pipeline is to develop and deliver quick succession environment, I have created triggered build which is not as good Jenkins2.0 pipeline, i really praise Jenkins2.0 pipeline i do have word on GoCD which i will cover in other post. Here we will use basic pipeline setup for nodejs app. If you check out my code you will find the JenkinsFile already in place.
Refer here for Jenkins setup on AWS.
Requirement:-
- Jenkins Master installed on any machine i preferred ubuntu16.X.
- Jenkins Slave001 (Label = testing) installed on any machine i preferred ubuntu 16.X.
- Jenkins Slave 002 (Label = staging) installed on any machine i preferred ubuntu16.X.
NOTE: i have setup jenkins master and slave on AWS, currently my AWS account is not functioning i will provide more details of how to setup AWS jenkins environment here.
- git repo for source here.
- Open source in any IDE use VSC or ATOM.
- In the source you can navigate to where you have Jenkinsfile, if you read the source available there it has been divided in to two different section one is node(“testing”) and node (“staging”), so if thing ideally a code has to build & test and build and test in staging.
- Lets cover first “node (testing)”
node(‘testing’){
// To setup the nodejs/npm environment with basic packages on Jenkins slave.
stage(‘Initialize’) {echo ‘Initializing…’sh ‘echo $(whoami)’sh ‘sudo apt-get update’sh ‘sudo apt-get install nodejs -y’// unlink after first run else pipeline will fail// sh ‘sudo ln -s /usr/bin/nodejs /usr/bin/node’sh ‘/usr/bin/node -v’sh ‘sudo apt-get install npm -y’sh ‘npm -v’}//To check out the source from SCM.
stage(‘Checkout’) {echo ‘Getting source code…’checkout scm}// To build the node app in Jenkins slave.stage(‘Build’) {echo ‘Building dependencies…’sh ‘npm i’}// As in the above stage build is done it has to run test on the build.stage(‘Test’) {echo ‘Testing…’sh ‘npm test’}// Publish the test coverage on Jenkins.stage(‘Publish’) {echo ‘Publishing Test Coverage…’publishHTML (target: [allowMissing: false,alwaysLinkToLastBuild: false,keepAll: true,reportDir: ‘coverage/lcov-report’,reportFiles: ‘index.html’,reportName: “Application Test Coverage”])}}
node(‘staging’){// On Jenkins slave with label “staging” initialise the nodejs/npm environment.stage(‘Initialize’) {echo ‘Initializing…’sh ‘echo $(whoami)’sh ‘sudo apt-get update’sh ‘sudo apt-get install nodejs -y’// unlink after first run else pipeline will fail//sh ‘sudo ln -s /usr/bin/nodejs /usr/bin/node’sh ‘/usr/bin/node -v’sh ‘sudo apt-get install npm -y’sh ‘npm -v’}//Check out the code from git hub.stage(‘Checkout’) {echo ‘Getting source code…’checkout scm}// Install PM2 its nodes process manager click here for details.stage(‘PM2 Install’) {echo ‘Installing PM2 to run application as daemon…’sh “sudo npm install pm2 -g”}// Build dependenciesstage(‘Build’) {echo ‘Building dependencies…’sh ‘npm i’}// Run tests again on staging i would prefer integration tests.stage(‘Test’) {echo ‘Testing…’sh ‘npm test’}// Now its time to deploy your node using PM2 on your slave.stage(‘Run Application’) {echo ‘Stopping old process to run new process…’sh ”’sudo npm run pm2-stopsudo npm run pm2-start”’}}
- Go to your git hub repo.
- Go to settings in git hub repo.
- Click on “Integration and services”.
- Click on “Add Service” button.
- In Drop down select “Jenkins” or type in search box and select Jenkins (Git hub) plugin.
- It will render a small window and there you have to add your Jenkins(http://master:8080/) url.
- Click on “Add service” button.
- Once done from there go to Jenkins.
- Navigate to your pipeline >> configure
- Click on check box against “GitHub hook trigger for GITScm polling” under “Build Triggers”
- So you are done now to test the integration check in a line of change into your git. It will kick off your Jenkins pipeline.
Here is your pipeline running.