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.

Xshell.png



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.

  1. Search for the JDK version by running the following command:

    yum search java|grep jdk
  2. 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 makecache
  3. Verify your installation:

    java -version

Installation using Xftp

  1. Choose and download the JDK file
    https://www.oracle.com/java/technologies/downloads/##java8

  2. Create a java folder:

    cd /usr
    mkdir java
  3. Upload the JDK file using Xftp and then extract it:

    tar -zxvf jdk-17_linux-x64_bin.tar.gz
  4. 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.

  5. 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 %
  6. Check your installation and configuration:

    javac
    java --version
    echo $PATH

Download Jenkins

Download Jenkins from the official website: https://www.jenkins.io/download/

jenkinswar.png

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>
java -jar jenkins.war

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:

  1. Open your web browser and enter the URL http://ip:8080

  2. If everything is set up correctly, you will see the following screen:

    unlock.png

  3. Enter the password provided during the initial setup to proceed.

    password.png

  4. You can now choose the plugins you want or need to install.

    plugins.png

  5. After creating your own account, you will be taken to the Jenkins dashboard:

    dashboard.png


    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:

select.png


Pipeline script

// Declarative 
pipeline {
agent any
stages {
stage('Build') {
steps {
println "Build"
}
}
stage('Test') {
steps {
sh "echo `pwd`"
}
}
stage('Deploy') {
steps {
println "Deploy"
}
}
}
}

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:

log.png

console1.png


Pipeline script from SCM

For this example, we will use Github as our repository.

Github Settings - Webhooks

webhook_github.png

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.

SCM.png


You can check how the source control cloning process went as well.

SCMlogs.png



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