close

最近幫Automation Team的同事建構Linux Base的Jenkins,參考網路上的資料去架設

,但設定Home directory遇到設定過不去的狀況,也許是工作機上的VM OS是比較新的Ubuntu,造成設定吃不進去,寫個筆記紀錄一下。

(目前部門線上使用的Jenkins安裝在Windows上,除了之前遇到的Yafuflash.exe沒辦法判斷return code (最後改遠端去連Linux跑Linux Tool避開這個問題)
,但近期遇到的其他問題,懷疑是因為windows版的tools,造成Automation Code使用判斷SUT的狀態出錯,所以建個Linux Base的Jenkins當作實驗用途)

(有空再補截圖...)

Pre-condition:

OS: Ubuntu Server 20.04

Others Package installation:

            # sudo apt install openjdk-8-jdk / sudo apt-get install openjdk-8-jdk

            # sudo apt install git / sudo apt-get install git

 

I. Jenkins Installation Steps:

1. First, add the repository key to the system:

# wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

After the key is added the system will return with OK.

 

2. Next, let’s append the Debian package repository address to the server’s sources.list:

# sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

 

3. After both commands have been entered, we’ll run update so that apt will use the new repository.

# sudo apt update / sudo apt-get update

 

4. Finally, we’ll install Jenkins and its dependencies.

# sudo apt install Jenkins / sudo apt-get install jenkins

 

Now that Jenkins and its dependencies are in place.

And we will set some parameter before starting the Jenkins server.

 

II. Pre-config the Environment:

II-I: Because of planning the Jenkins’s file with custom location.

@ Create a new Jenkins’s Home

1. Create a new Jenkins Home directory using the mkdir command.

(For this example, creating /home/jenkins_home:)

# sudo mkdir /home/jenkins_home

 

2. Change permissions for the new Home directory with:

# sudo chown jenkins:jenkins /home/jenkins_home

 

3. Copy the contents from the old Jenkins Home directory to the new one:

# sudo cp -prv /var/lib/jenkins /home/jenkins_home

 

4. Assign Jenkins as the user for the new Home directory with:

# sudo usermod -d /home/jenkins_home jenkins

 

@ Setting Jenkins service setting.

Ubuntu 18.04: (Ref https://phoenixnap.com/kb/change-jenkins-home-directory)

(But it not working under Ubuntu Server 20.04)

1. stop the Jenkins service with the following command:

# sudo systemctl stop jenkins

2. Open the Jenkins configuration file using a text editor (such as Nano / vi / vim):

# sudo vim /etc/default/jenkins

Find the parameter in default file.

JENKINS_HOME=/var/lib/$NAME

And modify it as below. And save file.

JENKINS_HOME=/home/jenkins_home

3. Restart the Jenkins service to apply the new configuration:

sudo systemctl restart jenkins

 

Ubuntu 20.04: (Different with Ubuntu 18.04)

1. Because of it does not work by modify “/etc/default/jenkins” under Ubuntu Server 20.04.

Jenkins is start with Java.

And Jenkins service is controlled with System-Service(systemd).

So, check the Jenkins service with the following command:

# sudo systemctl status jenkins

And it shows that

/usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080

So, I check the System-Service file and the JENKINS_HOME is setting in the file.

 

2. stop the Jenkins service with the following command:

# sudo systemctl stop Jenkins

 

3. Search and Modify the Jenkins service file with the following command:

The Jenkins service location as blow

Path: /etc/systemd/system/multi-user.target.wants/jenkins.service -> /lib/systemd/system/jenkins.service

# vim /lib/systemd/system/jenkins.service

And find the parameter line with “JENKINS_HOME”, and modify it as below. And save file.: 

Environment="JENKINS_HOME=/home/jenkins_home"

4. Reload service

# systemctl daemon-reload

 

II-II: Because of require “allow local checkout” when running something on the Jenkins.

Ubuntu 20.04:

1. Modify the Jenkins service file with the following command:

The Jenkins service location as blow

Path: /etc/systemd/system/multi-user.target.wants/jenkins.service -> /lib/systemd/system/jenkins.service

# vim /lib/systemd/system/jenkins.service

And find the parameter line with “JAVA_OPTS”: 

Default:

Environment="JAVA_OPTS=-Djava.awt.headless=true"

Add “-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true” to JAVA options.

Modify it as below. And save file.

Environment="JAVA_OPTS=-Djava.awt.headless=true -Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true"

4. Reload service

# systemctl daemon-reload

 

II-III: Opening the Firewall (If inactive Firewall, can skip these steps.)

1. To set up a UFW firewall, visit Initial Server Setup with Ubuntu 20.04, Step 4- Setting up a Basic Firewall. By default, Jenkins runs on port 8080. We’ll open that port using ufw:

# sudo ufw allow 8080

Note: If the firewall is inactive, the following commands will allow OpenSSH and enable the firewall:

# sudo ufw allow OpenSSH

# sudo ufw enable

 

2. Check ufw’s status to confirm the new rules:

# sudo ufw status

 

You’ll notice that traffic is allowed to port 8080 from anywhere:

 

Output

Status: active

 

To                         Action      From

--                         ------      ----

OpenSSH                    ALLOW       Anywhere

8080                       ALLOW       Anywhere

OpenSSH (v6)               ALLOW       Anywhere (v6)

8080 (v6)                  ALLOW       Anywhere (v6)

With Jenkins installed and our firewall configured, we can complete the installation stage and dive into Jenkins setup.

 

 

III. Jenkins Installation Steps:

 

Step 2 — Starting Jenkins

1. Let’s start Jenkins by using systemctl:

# sudo systemctl start jenkins

 

2. Since systemctl doesn’t display status output, we’ll use the status command to verify that Jenkins started successfully:

# sudo systemctl status jenkins

 

If everything went well, the beginning of the status output shows that the service is active and configured to start at boot.

 

Step 4 — Setting Up Jenkins

1. To set up your installation, visit Jenkins on its default port, 8080, using your server domain name or IP address:

Web brouwser: http://your_server_ip_or_domain:8080

 

2. You should receive the Unlock Jenkins screen, which displays the location of the initial password:

Default location

# sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Custom location

# sudo cat /home/jenkins_home/initialAdminPassword

Graphical user interface, text, application, email

Description automatically generated

Fill the initialAdminPassword and continue.

 

 

3. Click the Install suggested plugins option or manually select plugins to install

Default Setting was without Plugin: Rebuilder. (這個鰻好用的,事後從Jenkins網頁->管理plugins ->加裝這個功能)

 

 

4. Enter the name and password for your Admin user:

i.e.: Jenkins_Admin , abcdef

 

 

5. Instance Configuration page that will ask you to confirm the preferred URL for your Jenkins instance. Confirm either the domain name for your server or your server’s IP address.

 

 

 

6. After confirming the appropriate information, click Save and Finish.

 “Jenkins is Ready!”

Graphical user interface, application

Description automatically generated

 

Start using Jenkins to visit the main Jenkins dashboard:

 

 

 

 

 

Ref:

https://phoenixnap.com/kb/change-jenkins-home-directory

https://stackoverflow.com/questions/72341686/jenkins-allow-local-checkout

https://phoenixnap.com/kb/change-jenkins-home-directory (Its location was wrong methods)

 

 

 

 

Automation May Use These Modules:

# pip3 install numpy

# pip3 install pypsexec

# pip3 install tabula

# pip3 install redfish

 

 

Jenkins Service Control:

# systemctl daemon-reload

# systemctl enable jenkins.service (Enable start after boot OS)

# systemctl status jenkins.service (Current Status)

# systemctl stop jenkins.service (Stop Service)

# systemctl start jenkins.service (Start Service)

# systemctl restart jenkins.service (Restart Service)

arrow
arrow

    吾給力 發表在 痞客邦 留言(0) 人氣()