1. 程式人生 > >DEVOPS技術實踐_01:jenkins集成平臺

DEVOPS技術實踐_01:jenkins集成平臺

mls borde commit instance github nothing 全局 reload $path

一、準備環境

準備三臺機器

角色 IP地址 用戶名 密碼
jenkins-master 172.25.254.130 admin meiyoumima
gitlab 172.25.254.131 tseter meiyoumima
jenkins-slave(Maven 172.25.254.134 N/A N/A

二、jenkins-master安裝

2.1 安裝Java

[[email protected] ~]# yum install java-1.8.0-openjdk-devel.x86_64

[[email protected] ~]# vi /etc/profile

export JAVA_HOME=/usr/lib/jvm/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

[[email protected] ~]# source /etc/profile

[[email protected] ~]# java -version

openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
[[email protected]-master ~]# vi /etc/profile

2.2 安裝jenkins

[[email protected] ~]# cd /usr/local/src/

[[email protected] src]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war

[[email protected] src]# nohup java -jar /usr/local/src/jenkins.war --httpPort=9000 &

[[email protected] src]# tail -f nohup.out

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

5d3a5a8d50304bcf90845c6b55e4e280

This may also be found at: /root/.jenkins/secrets/initialAdminPassword

技術分享圖片

[[email protected] ~]# cat /root/.jenkins/secrets/initialAdminPassword

5d3a5a8d50304bcf90845c6b55e4e280

安裝插件

技術分享圖片

創建用戶

技術分享圖片

三、安裝gitlab

3.1 安裝依賴

[[email protected] ~]# yum install -y curl policycoreutils-python openssh-server

[[email protected] ~]# systemctl enable sshd

[[email protected] ~]# systemctl restart sshd

[[email protected] ~]# yum install -y postfix

[[email protected] ~]# firewall-cmd --add-service=ssh --permanent

[[email protected] ~]# firewall-cmd --add-service=http --permanent

[[email protected] ~]# firewall-cmd --reload

3.2 添加GitLab社區版Package

[[email protected] ~]# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

[[email protected] ~]# yum install -y gitlab-ce

技術分享圖片

3.3 配置GitLab站點Url

GitLab默認的配置文件路徑是/etc/gitlab/gitlab.rb

默認的站點Url配置項是:external_url ‘http://gitlab.example.com‘

這裏我將GitLab站點Url修改external_url ‘http://172.25.254.131‘

3.4 啟動gitlab

[[email protected] ~]# gitlab-ctl reconfigure

Running handlers:
Running handlers complete
Chef Client finished, 475/1268 resources updated in 03 minutes 23 seconds
gitlab Reconfigured!

技術分享圖片

四、jenkins配置slave

4.1 master端配置

點擊系統管理------------------->全局安全配置----------------->設置TCP端口

技術分享圖片

系統管理-節點管理-新建節點:

技術分享圖片

技術分享圖片

店家slave1

技術分享圖片

技術分享圖片

4.2 配置slave

在slave節點上安裝agent.jar

[[email protected] ~]# yum -y install java-1.8.0-openjdk-devel.x86_64

[[email protected] ~]# vim /etc/profile

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

[[email protected] ~]# java -version

openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)

[[email protected] ~]# mkdir /usr/local/jenkins-slave

[[email protected] ~]# cd /usr/local/jenkins-slave/

[[email protected] jenkins-slave]# ll

-rw-r--r--. 1 root root 789283 Apr 19  2019 agent.jar

根據提示執行

[[email protected] jenkins-slave]# java -jar agent.jar -jnlpUrl http://172.25.254.130:9000/computer/slave1/slave-agent.jnlp -secret e8b183c671c0f5efa09380c04bc54dedd1299528d2fce5d358a4c7b7b5af437d -workDir "/var/jenkins"

放後臺啟動

[[email protected] jenkins-slave]#nohup java -jar agent.jar -jnlpUrl http://172.25.254.130:9000/computer/slave1/slave-agent.jnlp -secret e8b183c671c0f5efa09380c04bc54dedd1299528d2fce5d358a4c7b7b5af437d -workDir "/var/jenkins" &

[[email protected] jenkins-slave]# tail nohup.out

Apr 10, 2019 10:26:15 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Handshaking
Apr 10, 2019 10:26:15 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connecting to 172.25.254.130:8181
Apr 10, 2019 10:26:15 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Trying protocol: JNLP4-connect
Apr 10, 2019 10:26:15 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Remote identity confirmed: e7:b8:51:28:e5:14:c4:3f:a2:ca:9d:08:01:20:7a:ce
Apr 10, 2019 10:26:16 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connected

刷新頁面

成功連接

技術分享圖片

五、Jenkins構建maven項目

5.1安裝maven

[[email protected] jenkins-slave]# mkdir /usr/local/maven

[[email protected] jenkins-slave]# cd /usr/local/maven

[[email protected] maven]# wget http://apache.communilink.net/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz

[[email protected] maven]# tar -xf apache-maven-3.6.1-bin.tar.gz

[[email protected] maven]# ll

total 8924
drwxr-xr-x. 6 root root 99 Apr 10 22:56 apache-maven-3.6.1
-rw-r--r--. 1 root root 9136463 Apr 5 03:30 apache-maven-3.6.1-bin.tar.gz

[[email protected] maven]# cd apache-maven-3.6.1

[[email protected] apache-maven-3.6.1]# pwd

/usr/local/maven/apache-maven-3.6.1

[[email protected] apache-maven-3.6.1]# vim /etc/profile

export M2_HOME=/usr/local/maven/apache-maven-3.6.1
export PATH=$PATH:$M2_HOME/bin

[[email protected] apache-maven-3.6.1]# mvn -version

The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

是因為Java的環境變量配置錯誤

修改

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
export M2_HOME=/usr/local/maven/apache-maven-3.6.1
export PATH=$PATH:$M2_HOME/bin

[[email protected] apache-maven-3.6.1]# source /etc/profile

[[email protected] apache-maven-3.6.1]# mvn -version

Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/maven/apache-maven-3.6.1
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"
[[email protected] apache-maven-3.6.1]# vim /etc/profile

[[email protected] apache-maven-3.6.1]# mvn

[INFO] Finished at: 2019-04-10T23:45:38+08:00

5.2簡單測試Maven項目

在github找一個maven項目

https://github.com/initcron/mvn-test

[[email protected] ~]# mkdir /git

[[email protected] ~]# cd /git

[[email protected] git]# git clone https://github.com/initcron/mvn-test

[[email protected] git]# ll

drwxr-xr-x. 4 root root 44 Apr 11 00:09 mvn-test

[[email protected] git]# cd mvn-test/

[[email protected] mvn-test]# ll

所有的maven項目都有一個pom.xml的文件

技術分享圖片

5.3 maven簡單用法

[[email protected] mvn-test]# mvn clean #清除緩存數據

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.527 s
[INFO] Finished at: 2019-04-11T00:15:03+08:00

[[email protected] mvn-test]# cat pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.app</groupId>
  <artifactId>maven-app</artifactId>
  <packaging>jar</packaging>
  <version>1.3-SNAPSHOT</version>
  <name>maven-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

打包

[[email protected] mvn-test]# mvn install

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:16 min
[INFO] Finished at: 2019-04-11T00:20:11+08:00
[INFO] ------------------------------------------------------------------------

會生成一個target目錄

5.4 測試

[[email protected] mvn-test]# mvn test

[[email protected] mvn-test]# ll

drwxr-xr-x. 7 root root 141 Apr 11 00:22 target

[[email protected] mvn-test]# cd target/

[[email protected] target]# ll

drwxr-xr-x. 3 root root   17 Apr 11 00:19 classes
-rw-r--r--. 1 root root 2275 Apr 11 00:20 maven-app-1.3-SNAPSHOT.jar   #生成一個可以直接運行的jar包
drwxr-xr-x. 2 root root   28 Apr 11 00:20 maven-archiver
drwxr-xr-x. 3 root root   35 Apr 11 00:19 maven-status
drwxr-xr-x. 2 root root   81 Apr 11 00:20 surefire-reports
drwxr-xr-x. 3 root root   17 Apr 11 00:19 test-classes

[[email protected] target]# java -jar maven-app-1.3-SNAPSHOT.jar

上傳到自己本地的倉庫

六、手動構建項目

6.1 創建一個新的project

技術分享圖片

技術分享圖片

6.2 把這個項目clone到本地

[[email protected] mvn-test01]# cd /git/

[[email protected] git]# git clone http://172.25.254.131/tester/mvn-test01.git

[[email protected] git]# ll

drwxr-xr-x. 5 root root 58 Apr 11 00:19 mvn-test
drwxr-xr-x. 3 root root 18 Apr 11 00:38 mvn-test01

[[email protected] mvn-test01]# mv ../mvn-test/* ./
[[email protected] mvn-test01]# ll

-rw-r--r--. 1 root root 646 Apr 11 00:09 pom.xml
drwxr-xr-x. 4 root root  30 Apr 11 00:09 src
drwxr-xr-x. 7 root root 141 Apr 11 00:22 target

6.3 提交

[[email protected] mvn-test01]# git add *

[[email protected] mvn-test01]# git commit -m ‘test‘

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your accounts default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got [email protected](none))

[[email protected] mvn-test01]# git init

Reinitialized existing Git repository in /git/mvn-test01/.git/

[[email protected] mvn-test01]# git config user.name ‘tester‘

[[email protected] mvn-test01]# git config user.email [email protected]

[[email protected] mvn-test01]# git add *

[[email protected] mvn-test01]# git commit -m ‘test‘

 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/example/app/App.java
 create mode 100644 src/test/java/com/example/app/AppTest.java
 create mode 100644 target/classes/com/example/app/App.class
 create mode 100644 target/maven-app-1.3-SNAPSHOT.jar
 create mode 100644 target/maven-archiver/pom.properties
 create mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst
 create mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
 create mode 100644 target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst
 create mode 100644 target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst
 create mode 100644 target/surefire-reports/TEST-com.example.app.AppTest.xml
 create mode 100644 target/surefire-reports/com.example.app.AppTest.txt
 create mode 100644 target/test-classes/com/example/app/AppTest.class

[[email protected] mvn-test01]# git push

warning: push.default is unset; its implicit value is changing in
Git 2.0 from matching to simple. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See git help config and search for push.default for further information.
(the simple mode was introduced in Git 1.7.11. Use the similar mode
current instead of simple if you sometimes use older versions of Git)

Username for http://172.25.254.131: tester                            
Password for http://[email protected]: 
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as master.
Everything up-to-date

[[email protected] mvn-test01]# git push origin master

Username for http://172.25.254.131: tester
Password for http://[email protected]: 
Counting objects: 43, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (43/43), 6.13 KiB | 0 bytes/s, done.
Total 43 (delta 0), reused 0 (delta 0)
To http://172.25.254.131/tester/mvn-test01.git
 * [new branch]      master -> master

6.4 打開gitlab驗證

技術分享圖片

構建成功

DEVOPS技術實踐_01:jenkins集成平臺