Setting up a Jenkins Pipeline on Linux
Prerequisites
Linux(Recommanded)
For this tutorial, it is recommended to use a Linux server. You will also need to install Xshell and Xftp.
JDK
To get started, you need to install JDK and add it to the environment path.
Installation using Command Line
If you choose this method, there is no need to configure the environment path.
Search for the JDK version by running the following command:
yum search java|grep jdk
Select the version you need to install:
yum install java-11-openjdk.x86_64 -y
If you get an error saying “Nothing to do”, you can try the following solution:
yum -y update
yum clean all
yum makecacheVerify your installation:
java -version
Installation using Xftp
Choose and download the JDK file
https://www.oracle.com/java/technologies/downloads/##java8Create a
java
folder:cd /usr
mkdir javaUpload the JDK file using Xftp and then extract it:
tar -zxvf jdk-17_linux-x64_bin.tar.gz
Configure the environment path:
vim /etc/profile
Type
i
to switch into insert mode.
And then insert the following content:export JAVA_HOME=/usr/java/jdk17.0.1.1
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}Then press
esc
and type:wq!
to quit.Ensure the configuration changes take effect by sourcing the profile file with the command:
source /etc/profile
Tips:
- If you forgot
sudo
before, you can save file as following::w !sudo tee %
- If you forgot
Check your installation and configuration:
javac
java --version
echo $PATH
Download Jenkins
Download Jenkins from the official website: https://www.jenkins.io/download/
You can use Xftp, yum, or wget to install Jenkins.
No further demonstration is provided here.
Execute as Java Binary
After downloading Jenkins, execute it by running the following command:
cd <your Jenkins downloaded path> |
You can also set the port by --httpPort=2022
. Use netstat -tunlp
to check the port situation.
Access Jenkins via Web Browser
Assuming everything is set up correctly, you can access Jenkins via your web browser. Follow these steps:
Open your web browser and enter the URL http://ip:8080
If everything is set up correctly, you will see the following screen:
Enter the password provided during the initial setup to proceed.
You can now choose the plugins you want or need to install.
After creating your own account, you will be taken to the Jenkins dashboard:
Tips: jenkinsUrl is ip:httpPort
- jenkinsUrl/safeRestart - Allows you to wait for running jobs to complete before restarting.
- jenkinsUrl/restart - Restarts Jenkins immediately without waiting for currently running jobs.
- jenkinsUrl/exit - Stops or shuts down the Jenkins service.
- jenkinsUrl/reload - Reloads configuration changes.
Create A Job(Pipeline)
Visit https://www.jenkins.io/doc/book/pipeline/syntax/ and enter your pipeline name to create a job. Then, configure the pipeline as follows:
Pipeline script
// Declarative |
Click “Build Now” to build the pipeline. Since it is a small script, it should not take long to build. Once the build is complete, you can view the output of each stage by clicking “logs.”
Here’s an example of the stage logs message:
Pipeline script from SCM
For this example, we will use Github as our repository.
Github Settings - Webhooks
Jenkins Settings
- Create your own Credentials. ( Same username and password as your Github account )
- Set your Repositories. ( Repository URL and Credentials )
- (Optional) Set Repository browser as githubweb and enter url
- Set Relative location. ( Where your Jenkinsfile located )
- Save
If everything goes right, you can see the output of each stages by click logs.
Here’s an example of the stage logs message. It has one additional stage called Declarative: Checkout SCM.
You can check how the source control cloning process went as well.
Summary
- Firstly, congratulations on actually building your Jenkins Pipeline.
- This tutorial has just provided to you a glimpse of what Jenkins Pipeline can do.
- You can extend its capabilities by integrating it with other tools.
Here are some examples for pipeline:
- https://github.com/bryantson/CICDPractice
- https://github.com/jenkinsci/pipeline-examples