Introduction

For this tutorial, you need jenkins installed. You can run in docker with this tutorial Quick start with jenkins in docker.

The pipeline is better because:

  • you can reuse everything you did
  • you can put your build code inside project together with your code
  • you can version the job code
  • the change in pipeline is showed in “changes” inside job history

All changes in git the job is started. With Jenkinsfile(file with pipeline script) inside your git project, all changes in pipeline or your project the job will start and the modification is logged by jenkins.

Installing Node.js tool

In home of jenkins, click on Manage Jenkins:

jenkins menu

Click on Manage Plugins:

Manage Plugins link

In Plugin manager, click on Available tab and after load the tab, search the plugins.

Tabs to search and manage plugins

Search for nodejs and mark the checkbox.

Nodejs plugin

Click on button “Download now and install after restart”.

Download now and install after restart button

On next screen, mark the checkbox “Restart Jenkins when installation is complete and no jobs are running”.

Installating Plugins with Restart checked

Wait a moment, if the screen not change, go to jenkins home clicking in jenkins’s logo on top, in the left side of the site.

Configuring Node.js tool

After installed and restarted, go to jenkins’s home > Manage Jenkins > Global Tool Configuration.

Global Tool Configuration

Search for NodeJS and click on NodeJS instalation button.

NodeJS Installations button

Put a name for node configuration, ex: node. Select the version of node that you need.

NodeJS config

Test the nodejs plugin

OK. NodeJS is installed with sucess. Let’s try it.

Go to jenkins’s homne, and click on New Item on jenkins’s menu.

jenkins menu

Let’s create a job. Name it with build-test and select Pipeline. Click on “OK”.

creating job

You will see this:

job tabs

In pipeline, past this code:

pipeline {
  agent any
 
  tools {nodejs “node”}
 
  stages {
    stage(‘Example’) {
      steps {
        sh ‘npm config ls’
      }
    }
  }
}

The job will be like this:

Pipeline Script

Pipeline explanation

Before continue, let’s understand the pipeline job:

agent any

The job will run in any jenkins agent.

  • You can have a lot of jenkins nodes, one master and some slaves, and the job will run in any node.
  • You can run the job on specific jenkins, ex: You have 3 jenkins with Linux and one with Windows. You can run this job only in jenkins slave with Windows.
tools {nodejs “node”}

This line search for nodejs tool named “node” and use it in pipeline. It’s necessary for next scripts find the commands.

stages {

Stages is the inicialization from pipeline steps.

stage(‘Example’) {
  steps {
    ...
  }
}

This commands start a new stage named “Example” and run the steps inside this stage.

sh ‘npm config ls’

It execute a sh script in machine. With this, we can test the npm running correctly.

Running the test

Now, let’s run the script. Save the job. Inside the job, in left menu, click on Build Now.

Job menu

Wait some time for the build apear in build history below the menu (If necessary, reload the page).

Build Histoy

After the build over, the ball can be red or blue. If the ball is blue, the job is builded with success.

In center of page, you can visualize the stage view.

Stage View

You can see the build log clicking on blue ball inside build history. You will see this:

Started by user Gustavo
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/build-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Example)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
[build-test] Running shell script
+ npm config ls
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/5.6.0 node/v9.10.1 linux x64"

; node bin location = /var/jenkins_home/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/bin/node
; cwd = /var/jenkins_home/workspace/build-test
; HOME = /var/jenkins_home
; "npm config ls -l" to show all defaults.

[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Ok. The build test right. Let’s create a real node job.

Testing the application

Go to jenkins’s home, click on “new job”. Select pipeline and create the job named “node_test”, and put this script:

pipeline {
  agent any
    
  stages {    
    stage('Cloning Git') {
      steps {
        git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git'
      }
    }
  }     
}

Test this job, it will download a node project to your jenkins. The stage view need be:

Stage View Git clone

Now, in job screen. Click on “configure” and let’s change the code.

pipeline {
  agent any
    
  tools {nodejs "node"}
    
  stages {
        
    stage('Cloning Git') {
      steps {
        git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git'
      }
    }
        
    stage('Install dependencies') {
      steps {
        sh 'npm install'
        sh 'npm run bowerInstall'
      }
    }
     
    stage('Test') {
      steps {
         sh 'npm test'
      }
    }      
  }
}

After cloning step, we install the npm dependencies and the bower dependencies using npm run. Finally, the test is executed.

Stage View with git, download dependencies and npm test

All right, the application is tested with success.

Pipeline inside project

Now, put the script of pipeline inside your git project in a file named Jenkinsfile.

Jenkinsfile inside git project

Go back to jenkins and change the job.

First step, change the definition of pipeline job to “Pipeline script from SCM”.

Pipeline script from SCM

Select Git on SCM Select. Put your git repository on Repository URL.

Build your pipeline from git repository

Git credentials

If you need credential to access your git repository, click on “Add” button.

Button Add git Credentials

Add your git credentials.

Add Credentials to git

And the configuration will be right with your credentials.

Git configuration with credentials

We have the node project tested. Now we can build the docker container with our project.

Soon.