一、准备环境
准备三台机器
角色 | 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
[root@jenkins-master ~]# yum install java-1.8.0-openjdk-devel.x86_64
[root@jenkins-master ~]# 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
[root@jenkins-master ~]# source /etc/profile
[root@jenkins-master ~]# 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) [root@jenkins-master ~]# vi /etc/profile
2.2 安装jenkins
[root@jenkins-master ~]# cd /usr/local/src/
[root@jenkins-master src]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
[root@jenkins-master src]# nohup java -jar /usr/local/src/jenkins.war --httpPort=9000 &
[root@jenkins-master 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
[root@jenkins-master ~]# cat /root/.jenkins/secrets/initialAdminPassword
5d3a5a8d50304bcf90845c6b55e4e280
安装插件
创建用户
三、安装gitlab
3.1 安装依赖
[root@gitlab ~]# yum install -y curl policycoreutils-python openssh-server
[root@gitlab ~]# systemctl enable sshd
[root@gitlab ~]# systemctl restart sshd
[root@gitlab ~]# yum install -y postfix
[root@gitlab ~]# firewall-cmd --add-service=ssh --permanent
[root@gitlab ~]# firewall-cmd --add-service=http --permanent
[root@gitlab ~]# firewall-cmd --reload
3.2 添加GitLab社区版Package
[root@gitlab ~]# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
[root@gitlab ~]# 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
[root@gitlab ~]# 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
[root@node5 ~]# yum -y install java-1.8.0-openjdk-devel.x86_64
[root@node5 ~]# 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
[root@node5 ~]# 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)
[root@node5 ~]# mkdir /usr/local/jenkins-slave
[root@node5 ~]# cd /usr/local/jenkins-slave/
[root@node5 jenkins-slave]# ll
-rw-r--r--. 1 root root 789283 Apr 19 2019 agent.jar
根据提示执行
[root@node5 jenkins-slave]# java -jar agent.jar -jnlpUrl http://172.25.254.130:9000/computer/slave1/slave-agent.jnlp -secret e8b183c671c0f5efa09380c04bc54dedd1299528d2fce5d358a4c7b7b5af437d -workDir "/var/jenkins"
放后台启动
[root@node5 jenkins-slave]#nohup java -jar agent.jar -jnlpUrl http://172.25.254.130:9000/computer/slave1/slave-agent.jnlp -secret e8b183c671c0f5efa09380c04bc54dedd1299528d2fce5d358a4c7b7b5af437d -workDir "/var/jenkins" &
[root@node5 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
[root@node5 jenkins-slave]# mkdir /usr/local/maven
[root@node5 jenkins-slave]# cd /usr/local/maven
[root@node5 maven]# wget http://apache.communilink.net/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
[root@node5 maven]# tar -xf apache-maven-3.6.1-bin.tar.gz
[root@node5 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
[root@node5 maven]# cd apache-maven-3.6.1
[root@node5 apache-maven-3.6.1]# pwd
/usr/local/maven/apache-maven-3.6.1
[root@node5 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
[root@node5 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
[root@node5 apache-maven-3.6.1]# source /etc/profile
[root@node5 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" [root@node5 apache-maven-3.6.1]# vim /etc/profile
[root@node5 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
[root@node5 ~]# mkdir /git
[root@node5 ~]# cd /git
[root@node5 git]# git clone https://github.com/initcron/mvn-test
[root@node5 git]# ll
drwxr-xr-x. 4 root root 44 Apr 11 00:09 mvn-test
[root@node5 git]# cd mvn-test/
[root@node5 mvn-test]# ll
所有的maven项目都有一个pom.xml的文件
5.3 maven简单用法
[root@node5 mvn-test]# mvn clean #清除缓存数据
[INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 8.527 s [INFO] Finished at: 2019-04-11T00:15:03+08:00
[root@node5 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>
打包
[root@node5 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 测试
[root@node5 mvn-test]# mvn test
[root@node5 mvn-test]# ll
drwxr-xr-x. 7 root root 141 Apr 11 00:22 target
[root@node5 mvn-test]# cd target/
[root@node5 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
[root@node5 target]# java -jar maven-app-1.3-SNAPSHOT.jar
上传到自己本地的仓库
六、手动构建项目
6.1 创建一个新的project
6.2 把这个项目clone到本地
[root@node5 mvn-test01]# cd /git/
[root@node5 git]# git clone http://172.25.254.131/tester/mvn-test01.git
[root@node5 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
[root@node5 mvn-test01]# mv ../mvn-test/* ./
[root@node5 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 提交
[root@node5 mvn-test01]# git add *
[root@node5 mvn-test01]# git commit -m 'test'
*** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'root@node5.(none)')
[root@node5 mvn-test01]# git init
Reinitialized existing Git repository in /git/mvn-test01/.git/
[root@node5 mvn-test01]# git config user.name 'tester'
[root@node5 mvn-test01]# git config user.email joy04007@gmail.com
[root@node5 mvn-test01]# git add *
[root@node5 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
[root@node5 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://tester@172.25.254.131': No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master'. Everything up-to-date
[root@node5 mvn-test01]# git push origin master
Username for 'http://172.25.254.131': tester Password for 'http://tester@172.25.254.131': 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验证
构建成功