Jenkins去GitLab拉取Java代码自动打包

时间:2022-01-11 13:36:10

jenkins的部署

一、部署git

1)先检查系统是否已经自带了git,如果有,就卸载

$ rpm -qa | grep git &&  rpm -e git --nodeps

2)开始安装git所需要的依赖包

$ yum -y install curl-devel gettext-devel expat-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

3)下载git源码包并安装

$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
$ tar xvf git-2.9.5.tar.gz -C /usr/local/src
$ cd /usr/local/src/git-2.9.5/
$ make prefix=/usr/local/git all
$ make prefix=/usr/local/git install

4)修改环境变量

$ vim /etc/profile
export PATH=$PATH:/usr/local/git/bin
$ source /etc/profile

5)测试

$ git --version
git version 1.8.3.1

二、部署jdk环境

2.1. 检测是否有自带的jdk环境,如果有就卸载

$ rpm -qa | grep jdk && yum -y remove java*

2.2. 下载JDK

$ wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz
#wget --no-check-certificate --no-cookies --header "Cookie直接下载JDK,避免用户验证"

$ tar xvf jdk-8u181-linux-x64.tar.gz -C /usr/local/
$ mv /usr/local/jdk1.8.0_181 /usr/local/java

2.3. 修改环境变量

$ vim /etc/profile
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/git/bin
$ source /etc/profile

2.4. 测试

$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

三、部署Maven

3.1. 下载maven包

$ wget http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz

3.2. 解压Maven

$ tar xvf apache-maven-3.5.4-bin.tar.gz -C /usr/local
$ mv /usr/local/apache-maven-3.5.4 /usr/local/maven

3.3. 修改环境变量

$ vim /etc/profile
export JAVA_HOME=/usr/local/java
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:/usr/local/git/bin
$ source /etc/profile

3.4.测试

$ mvn -version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_181/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-514.26.2.el7.x86_64", arch: "amd64", family: "unix"

四、部署Tomcat

4.1. 下载Tomcat

$ wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-9/v9.0.16/bin/apache-tomcat-9.0.16.tar.gz

4.2. 解压Tomcat

$ tar xvf apache-tomcat-9.0.16.tar.gz -C /usr/local
$ mv /usr/local/apache-tomcat-9.0.16 /usr/local/tomcat

4.3. 修改环境变量

$ vim /etc/profile
export JAVA_HOME=/usr/local/java
export MAVEN_HOME=/usr/local/maven
export CATALINA_HOME=/usr/local/tomcat
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:/usr/local/git/bin
$ source /etc/profile

4.4. 增加熵池大小,快速启用tomcat

$ yum -y install rng-tools
$ cp /usr/lib/systemd/system/rngd.service /etc/systemd/system
$ vim /etc/systemd/system/rngd.service
ExecStart=/sbin/rngd -f -r /dev/urandom
$ systemctl daemon-reload
$ systemctl restart rngd

4.5. 编写tomcat的启动脚本

$ vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
Environment="JAVA_HOME=/usr/local/java" //tomcat启用时需要识别JAVA_HOME
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
[root@jenkins ~]# chmod 600 /usr/lib/systemd/system/tomcat.service
[root@jenkins ~]# systemctl daemon-reload

5.6. 测试

$ systemctl start tomcat
$ ss -antp | grep java
LISTEN 0 100 :::8009 :::* users:(("java",pid=5717,fd=54))
LISTEN 0 100 :::8080 :::* users:(("java",pid=5717,fd=49))
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",pid=5717,fd=107))

五、部署jenkins

5.1. 下载jenkins.war包

$ wget http://mirrors.jenkins.io/war/latest/jenkins.war

5.2. 安装jenkins(放到tomcat的webapps目录下,启用tomcat即可)

$ cp jenkins.war /usr/local/tomcat/webapps

5.3. 测试

$ systemctl restart tomcat

5.4. 修改首页

<Context path="" docBase="/usr/local/tomcat/webapps/jenkins/" debug="0"/>

 

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

六、配置jenkins

6.1. jenkins的安装

Jenkins去GitLab拉取Java代码自动打包

$ cat /root/.jenkins/secrets/initialAdminPassword
5e94c58bcd6a4fc5b7f49675c39a44bf
6.1.1. 报错

Jenkins去GitLab拉取Java代码自动打包

解决办法:
$ find / -name "hudson.model.UpdateCenter.xml"
/root/.jenkins/hudson.model.UpdateCenter.xml <?xml version='1.1' encoding='UTF-8'?>
<sites>
<site>
<id>default</id>
<url>https://updates.jenkins.io/update-center.json</url>
</site>
</sites> https修改为http

6.2. 点击安装推荐的插件

Jenkins去GitLab拉取Java代码自动打包

6.3. 等待插件下载完成

Jenkins去GitLab拉取Java代码自动打包

6.4. 设置管理员账号密码

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

6.5. 配置访问Jenkins的URL

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

6.6. Start Using Jenkins

Jenkins去GitLab拉取Java代码自动打包 输入账号密码登录

6.7. jenkins插件的安装

Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

6.7.1 安装SSH Publish Over SSH 用于ssh连接的

Jenkins去GitLab拉取Java代码自动打包

6.7.2 安装GitLab 为git客户端从git主库取代码

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

6.7.3. 安装Maven Integration plugin 编译打包

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

6.8. jenkins系统设置

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

6.8.1. 配置jdk根目录

Jenkins去GitLab拉取Java代码自动打包

6.8.2. 配置git命令的绝对路径

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

6.8.3. 配置Maven根目录

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

Apply and Save

6.8.4. 配置ssh

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

$ ssh-copy-id -i 192.168.1.10       //后端gitlab的ip地址
$ cat .ssh/id_rsa

Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

Apply and Save

Hostname为后端tomcat服务器的ip地址
Remote Directory ssh过去后在机器的什么目录下 点击Test Configuration 显示Success则表示成功----> Apply and Save

6.9. 在GitLab服务器上获取Token

6.9.1. 创建用户获取Token

Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包

6.10. jenkins服务器上配置Gitlab

Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包

七、创建一个Jenkins Job

7.1. 创建job

Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包

7.2. 生成访问GitLab的ssh秘钥

Jenkins去GitLab拉取Java代码自动打包

从Gitlab上以SSH的方式拉取或提交代码需要用到这个SSH秘钥,哪台机器需要从GitLab上拉取代码,就在哪台机器上生成SSH key

$ ssh-keygen -t rsa -C "<添加一些注释>" -b 4096
$ cat .ssh/id_rsa.pub

7.3. 配置Job

Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包
Jenkins去GitLab拉取Java代码自动打包
Jenkins去GitLab拉取Java代码自动打包
Jenkins去GitLab拉取Java代码自动打包

7.4. 配置Job的构建触发器

选择“构建触发器”,勾选“Pull SCM”,这个选项会每隔一段时间检查一下GitLab仓库中代码是否有更新,有的话就执行构建操作。日程表如何设置,在这个输入框下面有说明。 Jenkins去GitLab拉取Java代码自动打包

Jenkins去GitLab拉取Java代码自动打包

7.5. 配置打包

Jenkins去GitLab拉取Java代码自动打包

7.6.打包完成之后的动作

Jenkins去GitLab拉取Java代码自动打包

$ mkdir /jenkins/{jar,sh} -p
$ vim /jenkins/sh/web.sh
cp /jenkins/jar/*.jar /jenkins
echo successfully > /root/jenkins.log
$ chmod +x /jenkins/sh/web.sh
7.6.1. 报错

Jenkins去GitLab拉取Java代码自动打包

脚本没有执行权限,需要给脚本授权

$ chmod +x 脚本