Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Jenkins to build CI/CD pipeline

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Jenkins to build CI/CD assembly line", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Jenkins to build CI/CD assembly line" this article.

What is an assembly line?

Before entering this tutorial, it is helpful to know about the CI/CD pipeline pipeline.

First of all, it is helpful to understand that Jenkins itself is not an assembly line. Simply creating a new Jenkins job does not build an assembly line. Think of Jenkins as a remote control, just click the button here. What happens when you click the button depends on what the remote control wants to control. Jenkins provides a way to plug in Jenkins for other application API, software libraries, build tools, and so on, which can perform and automate tasks. Jenkins itself does not perform any functions, but becomes more and more powerful as other tools are plugged in.

Pipelining is a separate concept that refers to groups of events or assignments that are connected in sequence:

A pipelined pipeline is a series of events or jobs that can be executed.

The easiest way to understand pipelining is to visualize a series of phases, as follows:

Pipeline example

Here, you should see two familiar concepts: phase Stage and step Step.

Phase: a block that contains a series of steps. The phase block can be named anything; it is used to visualize pipelined processes.

Step: a task that indicates what to do. The steps are defined within the phase block.

In the example diagram above, phase 1 can be named "build", "gather information", or other names, and other phase blocks can take a similar line of thinking. " The "step" simply puts on the content to be executed, and it can be a simple print command (for example, echo "Hello, World"), a program execution command (for example, java HelloWorld), a shell execution command (for example, chmod 755 Hello), or any other command, as long as it is recognized as an executable command through the Jenkins environment.

The Jenkins pipeline is provided in the form of an encoding script, often referred to as "Jenkinsfile", although different file names can be used. Here is an example of a simple Jenkins pipeline file:

/ / Example of Jenkins pipeline script pipeline {stages {stage ("Build") {steps {/ / Just print a Hello, Pipeline to the console echo "Hello, Pipeline!" / / Compile a Java file. This requires JDKconfiguration from Jenkins javac HelloWorld.java / / Execute the compiled Java binary called HelloWorld. This requires JDK configuration from Jenkins java HelloWorld / / Executes the Apache Maven commands, clean then package. This requires Apache Maven configuration from Jenkins mvn clean package. / HelloPackage / / List the files in current directory path by executing a default shell command sh "ls-ltr"}} / / And next stages if you want to define further... } / / End of stages} / / End of pipeline

From this sample script, it is easy to see the structure of the Jenkins pipeline. Note that some commands, such as java, javac, and mvn, are not available by default and need to be installed and configured through Jenkins. Therefore:

Jenkins pipelining is a way to execute Jenkins jobs sequentially in a defined manner by coding and structuring them in blocks that can contain the steps of multiple tasks.

good. Now that you know what a Jenkins pipeline is, I'll show you how to create and execute a Jenkins pipeline. At the end of this tutorial, you will set up a Jenkins pipeline, as follows:

Final Result

How to build Jenkins pipeline

To make it easier to follow the steps in this tutorial, I created a sample GitHub repository and a video tutorial.

Before you start this tutorial, you need to:

Java Development Kit (JDK): if it is not already installed, install JDK and add it to the environment path so that you can execute Java commands (such as java jar) from the terminal. This is necessary to take advantage of the Java Web Archive (WAR) version of Jenkins used in this tutorial (although you can use any other distribution).

Basic computer skills: you should know how to type some code, execute basic Linux commands through shell, and open a browser.

Let's get started.

Step 1: download Jenkins

Navigate to the Jenkins download page. Scroll down to Generic Java package (.war), and then click to download the file; save it in an easy-to-find location. (if you choose another Jenkins distribution, the steps in this tutorial should be almost the same except for step 2. The reason for using the WAR file is that it is an one-time executable file that can be easily executed and deleted.

Download Jenkins as Java WAR file

Step 2: execute Jenkins in Java binary mode

Open a terminal window and use cd to enter the directory where you downloaded Jenkins. Before continuing, make sure that JDK is installed and added to the environment path. ) execute the following command, which runs the WAR file as an executable binary:

Java-jar. / jenkins.war

If all goes well, Jenkins should be up and running on the default port 8080.

Execute as an executable JAR binary

Step 3: create a new Jenkins job

Open a Web browser and navigate to localhost:8080. Unless you have a previously installed Jenkins, you should go directly to the Jenkins dashboard. Click "Create New Jobs". You can also click "New Item" on the left.

Create New Job

Step 4: create an assembly line job

In this step, you can select and define the type of Jenkins job to create. Select "Pipeline" and name it (for example, "TestPipeline"). Click OK to create an assembly job.

Create New Pipeline Job

You will see a Jenkins job configuration page. Scroll down to find the "Pipeline" section. There are two ways to perform Jenkins pipelining. One way is to write pipelined scripts directly on Jenkins, and the other is to retrieve Jenkins files from SCM (source control). In the next two steps, we will experience both approaches.

Step 5: configure and execute pipeline jobs through direct scripts

To use a direct script to execute the pipeline, first copy the contents of the Jenkinsfile example from GitHub. Select "Pipeline script" as "Destination", and then paste the contents of the Jenkinsfile into "Script". Take some time to study the structure of the Jenkins file. Note that there are three phases: Build, Test, and Deploy, which are arbitrary and can be any one. There are steps in each stage; in this example, they just print random messages.

Click "Save" to keep the changes, which automatically takes you back to the "Job Overview" page.

Configure to Run as Jenkins Script

To begin the process of building the pipeline, click Build Now. If all goes well, you will see the first assembly line (such as the one below).

Click Build Now and See Result

To view the output of the pipeline script build, click any phase, and then click Log. You will see the news like this.

Visit sample GitHub with Jenkins get clone link

Step 6: configure and execute pipeline jobs through SCM

Now, put it another way: in this step, you will deploy the same Jenkins job by copying Jenkinsfile from the source-controlled GitHub. In the same GitHub repository, locate its repository URL by clicking "Clone or download" and copying its URL.

Checkout from GitHub

Click Configure to modify the existing job. Scroll to the Advanced Project Options setting, but this time, select the Pipeline script from SCM option from the Destination drop-down list. Paste the URL of the GitHub repository into Repository URL, and then type Jenkinsfile in Script Path. Click the Save button to save.

Change to Pipeline script from SCM

To build the pipeline, go back to the "Task Overview" page and click "Build Now" to execute the job again. The result is the same as before, except for one more phase called "Declaration: Checkout SCM".

Build again and verify

To view the output from the pipeline built by SCM, click the phase and view "Log" to check the progress of the source control cloning process.

Verify Checkout Procedure

More can be done than printing messages

Congratulations! You have set up the first Jenkins pipeline!

"but wait," you said, "it's too limited. I can do nothing but print useless messages." That's fine. So far, this tutorial has only given a brief introduction to what Jenkins pipelining can do, but you can extend its functionality by integrating it with other tools. Here are some ideas for your next project:

Establish a multi-stage Java build pipeline, starting with the following stages: pulling dependencies from JAR repositories such as Nexus or Artifactory, compiling Java code, running unit tests, packaging as JAR/WAR files, and then deploying to the CVM.

Implement an advanced code testing dashboard that will report on the health of the project based on Selenium unit tests, load tests, and automated user interface tests.

Build a multi-pipeline or multi-user pipeline to automate the task of performing Ansible scripts while allowing authorized users to respond to ongoing tasks.

Design a complete end-to-end DevOps pipeline that extracts infrastructure resource files and configuration files (such as GitHub) stored in SCM and executes the script through various runtime programs.

Follow any of the tutorials at the end of this article to learn about these more advanced cases.

Manage Jenkins

In the Jenkins main panel, click "Manage Jenkins".

Manage Jenkins

Global tool configuration

There are many tools available, including managing plug-ins, viewing system logs, and so on. Click Global Tool Configuration.

Global Tools Configuration

Increase additional capacity

Here, you can add JDK paths, Git, Gradle, and so on. After configuring the tool, simply add the command to Jenkinsfile or execute it through a Jenkins script.

These are all the contents of the article "how to use Jenkins to build CI/CD pipelining". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report