一、实验环境
selinux iptables off
主机名 | IP | 系统版本 |
---|---|---|
gitlab | 10.10.10.200 | rhel7.5 |
jenkins | 10.10.10.10 | rhel7.5 |
tomcat | 10.10.10.11 | rhel7.5 |
二、安装jenkins
1、解压安装包
下载地址:https://download.docker.com/linux/static/stable/x86_64/
[root@jenkins ~]# tar xf docker-18.06.3-ce.tgz
[root@jenkins ~]# cd docker/
[root@jenkins docker]# cp * /usr/bin/
2、配置docker.service
[root@jenkins ~]# vim /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
3、启动docker
[root@jenkins ~]# chmod +x /etc/systemd/system/docker.service
[root@jenkins ~]# systemctl daemon-reload
[root@jenkins ~]# systemctl enable docker && systemctl restart docker
4、修改镜像源
[root@jenkins ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
[root@jenkins ~]# systemctl restart docker
三、gitlab安装
1、安装
[root@gitlab ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
[root@gitlab ~]# yum install -y gitlab-ce
2、初始化GitLab配置
[root@gitlab ~]# gitlab-ctl reconfigure
其他命令:
# 启动 gitlab 服务
gitlab-ctl start
# 停止 gitlab 服务
gitlab-ctl stop
3、浏览器登陆
http://10.10.10.200
[root@gitlab ~]# cat /etc/gitlab/initial_root_password
4、修改root密码
5、上传项目
四、tomcat安装
1、解压
[root@server1 ~]# tar xf jdk-8u45-linux-x64.tar.gz
[root@server1 ~]# mv jdk1.8.0_45/ /usr/local/jdk
2、声明变量
[root@server1 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
[root@server1 ~]# . /etc/profile
3、安装Tomcat
官网下载地址:https://tomcat.apache.org/download-80.cgi
[root@server1 ~]# tar xf apache-tomcat-8.5.59.tar.gz
[root@server1 ~]# mv apache-tomcat-8.5.59 /usr/local/tomcat
4、启动Tomcat
[root@server1 ~]# /usr/local/tomcat/bin/startup.sh
[root@server1 ~]# /usr/local/tomcat/bin/shutdown.sh ###关闭
5、查看Tomcat
五、jenkins安装
1、拉取镜像
[root@jenkins ~]# docker pull jenkins/jenkins:2.361.4-lts-jdk11
[root@jenkins ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jenkins/jenkins 2.361.4-lts-jdk11 25fa92c47840 6 months ago 463MB
2、解压文件
[root@jenkins ~]# tar xf jdk-8u45-linux-x64.tar.gz
[root@jenkins ~]# mv jdk1.8.0_45/ /usr/local/jdk
[root@jenkins ~]# tar xf apache-maven-3.5.0-bin.tar.gz
[root@jenkins ~]# mv apache-maven-3.5.0 /usr/local/maven
3、配置maven阿里云仓库
[root@jenkins ~]# vim /usr/local/maven/conf/settings.xml
<mirrors>
<mirror>
<id>central</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
4、运行jenkins
docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -u root \
-v /opt/jenkins_home:/var/jenkins_home \
-v /usr/local/maven:/usr/local/maven \
-v /usr/local/jdk:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
-v /var/run/docker.sock:/var/run/docker.sock \
--restart=always \
jenkins/jenkins:2.361.4-lts-jdk11
5、查看密码
[root@jenkins ~]# cd /opt/jenkins_home/
[root@jenkins jenkins_home]# sed -i ‘s#https://updates.jenkins.io/update-center.json#http://mirror.esuni.jp/jenkins/updates/update-center.json#g’ hudson.model.UpdateCenter.xml
[root@jenkins jenkins_home]# systemctl restart docker
[root@jenkins ~]# cat /opt/jenkins_home/secrets/initialAdminPassword
375dd3d01ef9411b833a8860d028683d
6、通过密码登录
7、安装插件
8、创建管理员用户
9、安装重启
10、修改为国内源
[root@jenkins ~]# cd /opt/jenkins_home/updates/
[root@jenkins updates]# sed -i 's/https:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json
[root@jenkins updates]# sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
六、通过FreeStyle发布
提前下载
git parameter
、Publish Over SSH
插件,git parameter版本低于0.95正常,否则报错:Retrieving Git references
,无法获取到正常版本
1、配置Tomcat SSH Server
2、创建FreeStyle项目
3、配置git参数
4、git拉取地址设置
5、maven编译
JAVA_HOME=/usr/local/jdk
PATH=$PATH:$JAVA_HOME/bin
/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true
6、Tomcat服务器执行程序
tomcat=/usr/local/tomcat
cd $tomcat/webapps
rm -rf $tomcat/webapps/ROOT
mv *.war /data/backup/$(date +"%F_%T")_ROOT.war
# 部署新程序并重启Tomcat
mv /tmp/$JOB_NAME/*.war $tomcat/webapps/ROOT.war
pid=$(ps -ef |grep $tomcat |egrep -v 'grep' |awk '{print $2}')
[ -n "$pid" ] && kill -9 $pid
export JAVA_HOME=/usr/local/jdk
/bin/bash /usr/local/tomcat/bin/startup.sh
7、构建并查看结果
七、通过Maven发布
安装插件
Maven IntegrationVersion 3.22
1、配置JDK和maven
系统管理->全局工具配置
2、创建maven项目
3、配置Git参数
4、git拉取地址设置
5、maven编译
clean package -DskipTests=true
6、Tomcat服务器执行脚本
tomcat=/usr/local/tomcat
cd $tomcat/webapps
rm -rf $tomcat/webapps/ROOT
mv *.war /data/backup/$(date +"%F_%T")_ROOT.war
# 部署新程序并重启Tomcat
mv /tmp/$JOB_NAME/*.war $tomcat/webapps/ROOT.war
pid=$(ps -ef |grep $tomcat |egrep -v 'grep' |awk '{print $2}')
[ -n "$pid" ] && kill -9 $pid
export JAVA_HOME=/usr/local/jdk
/bin/bash /usr/local/tomcat/bin/startup.sh
7、构建并查看结果
八、通过Pipline发布
1、创建流水线项目
2、配置git参数
3、流水线语法
4、git拉取流水线脚本
5、maven编译流水线脚本
6、发送并启动流水线脚本
7、配置流水线脚本
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout scmGit(branches: [[name: '$Branch']], extensions: [], userRemoteConfigs: [[credentialsId: 'e94bf465-be00-490b-9f24-856106362015', url: 'http://10.10.10.200/wielun/java-test.git']])
}
}
stage('maven编译') {
steps {
sh '''JAVA_HOME=/usr/local/jdk
PATH=$PATH:$JAVA_HOME/bin
/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'''
}
}
stage('发送到tomcat服务器并启动') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'tomcat-test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''tomcat=/usr/local/tomcat
cd $tomcat/webapps
rm -rf $tomcat/webapps/ROOT
mv *.war /data/backup/$(date +"%F_%T")_ROOT.war
# 部署新程序并重启Tomcat
mv /tmp/$JOB_NAME/*.war $tomcat/webapps/ROOT.war
pid=$(ps -ef |grep $tomcat |egrep -v \'grep\' |awk \'{print $2}\')
[ -n "$pid" ] && kill -9 $pid
export JAVA_HOME=/usr/local/jdk
/bin/bash /usr/local/tomcat/bin/startup.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '$JOB_NAME', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.war')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
8、构建并查看结果
注:第一次看不到分支,构建一次就好了