Jenkins is an open-source CI/CD software that allows automated workflows and is essential to DevOps. This automated workflow is also called a Pipeline. These Pipelines test and report changes in the codebase. Moreover, they can be used to build software, automate the testing of those builds, prepare the codebase for deployment and/or delivery, and finally, deploy the codebase to the required platform.
As mentioned above, Jenkins can build files as a build agent. It allows the user to set up steps and instructions for an automated process. This Pipeline method then creates the files as instructed and performs the checks mentioned. An example of a build file is a Docker container. This Answer will discuss creating a Docker container as a build file using a Jenkins Pipeline.
Before starting our Docker containers, we need to install Jenkins on our device. For this Answer, we will use Linux as our operating system. If you have another operating system, head to the official installation page for Jenkins and download it according to your requirements.
To begin, we need to open our terminal and enter the commands given below.
We now need to execute these commands in our terminal one by one.
sudo apt install openjdk-8-jdk // installs Java Development Kit based on the Java SE 8 specificationsudo apt install git // not mandatory but recommendedsudo apt install maven // installs maven build management toolwget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'sudo apt updatesudo apt install jenkins // install jenkinssudo systemctl start jenkins // start
After we have done so, we can go to our browser and enter the following address in our address bar to be redirected to our Jenkins administration page.
localhost:8080 // default Jenkins port
We will copy the address given on the page and paste it into our terminal with the prefix "sudo cat". This will give us our password.
We will be redirected to another page after we enter the correct password. There, we select either option depending on our preferences. It is recommended to install suggested plugins.
After installation, we are prompted to set up a profile. We can either do that or skip by signing in as admin. We then complete the forms by following the navigation. Now, we have set up our Jenkins at our port 8080.
Let us now set up the Docker Pipeline. For this, we must head to our Jenkins page.
www.localhost:8080
Once there, click on "Manage Jenkins," then click on "Plugins". Now we arrive at the plugin page. From here, we go to "Available Plugins" and search for "Docker Pipeline". After selecting the desired plugin, click "Download now and install after restart".
Now click on the restart option and log in again to your Jenkins.
As your Docker Pipeline has finished installing, click "New Item", enter the project name, and select "Pipeline". Now click "Ok" to proceed.
For uploading our Dockerfile, we need a GitHub or a Docker Hub account. In this answer, we will use a GitHub account. Therefore, we will head to our GitHub repository and upload a Dockerfile of our own choice.
--| Main Branch--| Dockerfile--| JenkinsFile (can be renamed)
FROM ubuntu:20.04# Update packages and install curlRUN apt-get update && \apt-get install -y curl# Install Node.jsRUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \apt-get install -y nodejs
pipeline {agent { dockerfile true } // Use Dockerfile as the agent for the pipelinestages {stage('Test') {steps {sh '''curl --version // Check the version of curlnode --version // Check the version of Node.jsnpm --version // Check the version of npm'''}}}}
In the configuration section, we need to attach our Pipeline script. We can either add it using its inbuilt text editor, or we can add our GitHub link. After modifying the configurations, we can click save and proceed to the next page.
Inside our Pipeline page, we can click "Build Now" to run our agents. When we do that, we see a task running, as shown in the image below. We can click the number to go to the build details on this.
Here we click "Console Output" to check our Pipeline status. We can read through the prompts and see our process run from here. The final line of the output will tell us if our files ran successfully or not.
We have now learned how to use Jenkins to spin up Docker containers and perform our tasks through an automated Jenkin build agent. This allows us to create an isolated system to test our packages and applications every time. Moreover, it allows for better portability with automation, which helps boost the speed at which we can build and test applications. In essence, Jenkins provides an essential utility by creating a faster, more efficient, and better building and testing experience.
Free Resources