Jenkins is an open-source automation server that helps developers automate parts of their software development process. With Jenkins, you can easily set up continuous integration and continuous deployment (CI/CD) pipelines to automate the process of building, testing, and deploying software. In this article, we will explain how to install and apply Jenkins to deploy software.

Installing Jenkins

To install Jenkins, you need to have a server or a virtual machine with a supported operating system, such as Windows, Linux, or macOS. The installation process may vary based on your operating system, but the easiest way to install Jenkins is to use the official Jenkins repository.

On a Linux system, you can follow these steps:

  1. Add the Jenkins repository to your system wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
  2. Update the package index: sudo apt update
  3. Install Jenkins: sudo apt install jenkins

On a Windows system, you can follow these steps:

  1. Download the Jenkins Windows installer from the official website.
  2. Double-click the installer to start the installation process.
  3. Follow the on-screen instructions to complete the installation.

Setting Up Jenkins

Once Jenkins is installed, you can access the Jenkins dashboard by visiting http://localhost:8080 in your web browser. On the initial page, you will be asked to unlock Jenkins using the initial admin password, which you can find in the /var/lib/jenkins/secrets/initialAdminPassword file on a Linux system or in the %JENKINS_HOME%\secrets\initialAdminPassword file on a Windows system.

Next, you will be prompted to install the recommended plugins or choose specific plugins to install. After the plugins are installed, you can set up your first Jenkins user account by entering your desired username and password.

Configuring the Jenkins Environment

To use Jenkins to deploy software, you need to set up a build environment that includes the necessary tools and dependencies to build your software. This can be done by installing the necessary plugins and configuring the build environment in the Jenkins dashboard.

  1. Install the necessary plugins: In the Jenkins dashboard, go to the "Manage Jenkins" section and select "Manage Plugins." From there, you can search for and install the necessary plugins, such as Git, Maven, or Docker.
  2. Configure the build environment: In the Jenkins dashboard, go to the "Manage Jenkins" section and select "Global Tool Configuration." From there, you can configure the necessary tools, such as Git, Maven, or Docker, and set their path and version.

Creating a Jenkins Job

A Jenkins job is a task that Jenkins can run, such as building software or deploying software. To create a Jenkins job, you need to follow these steps:

  1. In the Jenkins dashboard, click the "New Item" button.
  2. Enter a name for the job and select "Freestyle project."
  3. In the job configuration section, you can specify the source code repository, build triggers, and build steps.

Jenkins Plugins

Jenkins plugins are a powerful and flexible way to extend the functionality of your Jenkins server. By configuring plugins for specific pipeline jobs, you can enhance the capabilities of your Jenkins server and improve the efficiency of your build processes. Whether you are looking to automate your build process, manage your artefacts, or simply add new functionality to your Jenkins server, there is likely a plugin that can help you achieve your goals.

Before diving into the details of configuring plugins for specific pipeline jobs, it is important to understand the concept of plugin management in Jenkins. Jenkins has a rich library of plugins that can be installed and configured to enhance its functionality. Once installed, plugins can be globally configured, or they can be configured for specific jobs.

How to add or configure plugins

To configure plugins for a specific pipeline job, you need to go to the job's configuration page. From here, you can select the "Build Environment" section and then choose "Configure plugins". You can then select the plugins that you want to configure for this specific job. Plugins needed for this job are as follows :

1. "Pipeline Plugin". This plugin allows you to define your entire build process in a Jenkins-file, which is a file that is stored in your source control repository. By using the pipeline plugin, you can define a series of steps that are executed in order, allowing you to automate the entire build process.

2. "Build Timeout Plugin". This plugin allows you to set a timeout for your build, so that if it runs for too long, it will be automatically stopped. This is particularly useful for pipeline jobs that may run for a long time, such as those that involve a lot of tests or deployments.

3."Parameterised Builds Plugin" is also useful for pipeline jobs. This plugin allows you to pass parameters to your build, which can be used to control the behavior of your build. For example, you might pass a parameter to your build that specifies the version of your application that you want to build.

4. "Discard Old Builds Plugin" is a useful tool for managing the artifacts that are generated by your pipeline jobs. This plugin allows you to automatically discard old builds, which can help to reduce the amount of disk space that is consumed by your Jenkins server.

MAKING THE PIPELINE SCRIPT

The Pipeline script, written in Groovy, is the glue that holds everything together, allowing developers to define the flow of work through the various stages of the pipeline. By breaking down the development process into stages and using Jenkins to automate as much of it as possible, developers can create a more efficient and reliable software development process, reducing the chance of human error and improving the overall quality of the final product. Whether you're a seasoned developer or just starting out, Jenkins and its Pipeline plugin are tools that are worth exploring to streamline your development workflow and improve the quality of your software products.

The first step in creating a Jenkins Pipeline script is to create a Jenkinsfile, which is a text file that contains the script. You can either create the Jenkinsfile in your code repository or create it directly in the Jenkins UI. Once you have created the Jenkinsfile, you can define the stages of the pipeline.

The stages of the Jenkins Pipeline represent the different steps involved in the software development process. For example, you can define stages for building, testing, and deploying your application. Each stage consists of one or more steps, which are the individual tasks that need to be executed in that stage.

Below we are going to create a pipeline script that is going extract a project from git, clone it, then run it through a checkout stage. After the code has been checked out, we move to the build stage, where the code is compiled and built into a jar file that will be sent to the next stage for testing once the code is successfully tested and shows that it runs or works, it can finally be moved to the final stage which is Deployment. At the deployment stage, the code is set into the Production environment where the intended user or final user is.

To create a stage in the Jenkins Pipeline, you can use the "stage" directive. For example :

CHECKOUT STAGE

The first stage in your Pipeline script for checking out code is to define the checkout step. You can use the "checkout" command to do this. For example, if you are using Git as your VCS, you can define the checkout stage as follows:

stage('Checkout') {
            steps {
                git 'https://github.com/your-repo.git'  // Replace with your Git repository URL
            }
        }

In this example, the "checkout" command checks out the latest version of the code from the "master" branch of your Git repository. You can replace "your-repository" with your own repository URL.

BUILDING STAGE

After defining the checkout stage, you can move on to the next stage in your Pipeline script. You can define multiple stages in your script to perform various tasks, such as building, testing, and deploying your application.

For example, if you want to build your application after checking out the code, you can define a new stage for building:

stage('Build') {
            steps {
                sh 'mvn clean package'  // Replace with your build command
            }
        }

In this example, the "sh" command is used to execute the "mvn clean install" command, which will build your application.

Once you have defined the stages and steps of your Jenkins Pipeline, you can run the pipeline. To run the pipeline, you can either trigger it manually or set up a webhook to trigger it automatically when changes are pushed to your code repository.

TESTING STAGE

stage('Test') {
    steps {
        sh 'mvn test'  // Replace with your testing command
    }
}

This stage assumes that your Java application is using Maven as the build tool, and that the testing command is mvn test. If you're using a different build tool or testing framework, you'll need to modify the sh command accordingly.

If any tests fail, the stage will fail and the pipeline will stop. Otherwise, the pipeline will continue to the next stage. You can configure Jenkins to send notifications or take other actions based on the success or failure of each stage in the pipeline.

DEPLOYING STAGE

A stage for deploying to staging, you can use  the following code:

stage('Deploy') 
{// Code for deploying to staging goes here}

stage('Deploy') {
            steps {
                sshagent(['ssh-key']) {
                    sh "ssh -o StrictHostKeyChecking=no -i ${SSH_KEY} -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 'mkdir -p /opt/app'"
                    sh "scp -o StrictHostKeyChecking=no -i ${SSH_KEY} -P ${SSH_PORT} target/my-app.jar ${SSH_USER}@${SSH_HOST}:/opt/app/"
                    sh "ssh -o StrictHostKeyChecking=no -i ${SSH_KEY} -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 'java -jar /opt/app/my-app.jar &'"

In the Deploy stage, the sshagent function is used to securely access the SSH key. The mkdir command creates a directory for the application on the remote server. The scp command copies the application JAR file to the remote server. Finally, the java command starts the application as a background process.

Once you have defined the stages and steps of your Jenkins Pipeline for deployment, you can run the pipeline. To run the pipeline, you can either trigger it manually or set up a webhook to trigger it automatically when changes are pushed to your code repository.

CONCLUSION

Jenkins is a powerful and flexible tool that can greatly enhance the software development process. By using Jenkins and its many plugins, developers can automate tasks, streamline workflows, and increase productivity. The Pipeline plugin takes automation to the next level by allowing developers to define their entire software development process as a series of stages, each with its own set of tasks and dependencies.