In this post, we will create our first job to do the following tasks automatically,
- Pull the latest code from a git repository.
- Create a docker image using the Dockerfile in the repository.
- This will take care of npm install and exposing of the ports.
- Publish to a docker hub repository.
A prerequisite for building node js applications is to have the nodejs plugin installed in Jenkins. We have a lot of plugins in jenkins that makes it easy to integrate with third party applications, perform frequently done operations easily, etc.
- To add a plugin, we have to navigate to DashBoard > Manage Jenkins > Manage Plugins.
- Navigate to available plugins, search for NodeJS plugin and choose Download now and install after restart. Select restart when no jobs are running to initiate restart when no jobs are running.
- Similarly, download and install the Docker plugin as well. We will be using CloudBees Docker Build and Publish plugin for this peoject.
- To create a new job, click on new item, give a name for the job and choose Freestyle project. You will land at the configuration page.
- We will configure the Source Code Management as git, give the git repository url and specify the branch as master.
- We will then add the build step. We will choose Docker Build and Publish. We will fill in the docker repository name and supply the registry credentials. Here we have used a docker hub repository(https://hub.docker.com/repository/docker/vigneshm243/nodejs-test).
- We can check the results by issuing build now and check the console output. From the console output, we can see the steps getting executed sequentially.
- Finally, we can check the image updated in Docker Hub.
We will look at more complex jobs in further posts and explore few alternativies like gitlab-ci and github actions.