Writing Jenkins File For My Node.js Typescript Services
By: Joshua Isaac
Tags: DevOps, Jenkins, Backend Development, Typescript, Jenkinsfile

Jenkins is a free and open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery of applications and services.

Getting started

In this article we are going to dive into preparing a jenkins file that will help in deploying your Nodejs Typescript service.

To proceed , refer to my previous article on setting up your Dockerfile:

Why use jenkins?

Before we understand the why and get started writing a script, we need to understand what is continuous integration and continuous delivery ( CI/CD).

CI and CD stand for continuous integration and continuous delivery/ continuous deployment. In very simple terms, CI is a modern software development practice in which incremental code changes are made frequently and reliably. Automated build-and-test steps triggered by CI ensure that code changes being merged into the repository are reliable. The code is then delivered quickly and seamlessly as a part of the CD process. In the software world, the CI/CD pipeline refers to the automation that enables incremental code changes from developers’ desktops to be delivered quickly and reliably to production.

To achieve this, the jenkins tool should be of our development process and help us initiate deployments from our favorite version control tools eg. gitlab to the server.

Pipeline concepts

The following concepts are key aspects of Jenkins Pipeline, which tie in closely to Pipeline syntax.

Pipeline: A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it.

Node: A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline.

Stage: A stage block defines a conceptually distinct subset of tasks performed through the entire Pipeline (e.g. "Build", "Test" and "Deploy" stages), which is used by many plugins to visualize or present Jenkins Pipeline status/progress.

Step: A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or “step” in the process). For example, to execute the shell command make use the sh step: sh 'make' . When a plugin extends the Pipeline, that typically means the plugin has implemented a new step.

Let us look at the pipeline syntax overview:

pipeline {

agent any

stages {

stage('clone') {

steps {

//

}

}

stage('Build') {

steps {

//

}

}

stage('Deployment') {

steps {

//

}

}

}

}

What will happen in each stage?

clone stage: Clone project from repository in my case i will be using gitlab

build stage: Create environment variables, build project to docker image and then push it to registry

Deployment stage: Create a deployment script that will point docker container in registry to desired server and run the deployment script.

Finally we will create variables to use in the jenkins file so we have an easier time editing the jenkins file for future projects.

let us define some useful variable:

Now let us build the stages:

in the clone stage first remove any old cloned project folder then clone the repository branch you will need your username and user key.

Generating a user key in gitlab:

In your menu options, select the preferences link.

Select access token and provide a name for it but don’t select expiration date. Select your desired scope and create personal access token. Make sure you copy it immediately since once you exit the page you will never see it again.

In the build stage, we will be creating environment variables, build our docker image and push the image to our docker registry

And in the final stage, we will need to create a deployment script to get our image from the docker registry and deploy it to our server. then we will run the deployment script.

The final complete code:

What about the docker-compose-build ? i guess we will have an in-depth look at it next.

Is this all?

No, there are some stages that one can add an example being the test stage. The jenkins fill will depend entirely on your application.

Happy coding!!!