赞
踩
1.首先安装jenkins。
1.1编写Dockfile文件。
cat Dockfile
FROM jenkins/jenkins:2.414.3-jdk17
USER root
RUN apt-get update && apt-get install -y lsb-release
ADD sources.list /etc/apt/
RUN apt-get update && apt-get install -y software-properties-common
RUN curl -fsSL https://mirrors.huaweicloud.com/docker-ce/linux/debian/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://mirrors.huaweicloud.com/docker-ce/linux/debian $(lsb_release -cs) stable"
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"
1.2编写启动脚本
cat run.sh
docker run --name jenkins-blueocean -u root --restart=on-failure -itd --network jenkins --env DOCKER_HOST=tcp://docker:2376 --env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 --publish 39412:8080 --publish 51206:50000 --volume /opt/jenkins/jenkins-data:/var/jenkins_home --volume /opt/jenkins/jenkins-docker-certs:/certs/client:ro myjenkins-blueocean:2.414.3-1
1.3编写sources.list文件
cat sources.list
deb https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
deb https://mirrors.aliyun.com/debian-security/ bookworm-security main
deb-src https://mirrors.aliyun.com/debian-security/ bookworm-security main
deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
deb https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib
2.首先安装docker-ce。
2.1通过阿里云镜像站安装,文档附上,根据系统版本进行安装。
docker-ce镜像_docker-ce下载地址_docker-ce安装教程-阿里巴巴开源镜像站
2.2 使用docker拉取jenkins镜像。
docker pull jenkins/jenkins:2.414.3-jdk17 # 拉取镜像
docker images # 查看镜像
docker network create jenkins # 安装jenkins所需网络服务
docker build -t myjenkins-blueocean:2.414.3-1 . #使用刚才的Dockerfile文件构建镜像
2.3启动构建的Jenkins镜像。
./run.sh #运行刚刚的脚本
docker ps #查看容器运行情况
netstat -nutlp #查看端口
2.4查看Jkenkins密码登录。
cat /opt/jenkins/jenkins-data/secrets/initialAdminPassword
账号为admin
IP+39412
2.5登录上jenkins后,修改密码并且安装必要的插件。
系统管理-插件管理-updates,搜索安装以下插件。
ssh server
GitLab
GitLab Authentication
Generic Webhook Trigger
2.6配置跨域,不然后续构建时会报错403相关无效的碎屑 。
报错如下
WARNING hudson.security.csrf.CrumbFilter#doFilter: Found invalid crumb c0e527c9839db9e6e01a72ced74467cadec80c034df05525bd89ec032883f411. If you are calling this URL with a script, please use the API Token instead. More information: https://www.jenkins.io/redirect/crumb-cannot-be-used-for-script
WARNING hudson.security.csrf.CrumbFilter#doFilter: No valid crumb was included in request for /$stapler/bound/2c81919f-4aa1-4390-b361-c0ab2c945e46/render by admin. Returning 403.
具体解决方法如下:
系统管理-全局安全配置-跨站请求伪造保护-启动代理兼容(此项勾选上即可)
2.7 以上jenkins 安装以及插件安装都已安装完成,后续安装完gitlab,会配置构建脚本参数等。
3.安装gitlab。
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm
yum -y install local gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm
3.1修改配置文件设置访问端口。
vi /etc/gitlab/gitlab.rb
- external_url 'http://ip地址:9091'
-
- # 公网内网都可以,后面是端口
3.2初始化安装并启动。
gitlab-ctl reconfigure #初始化
# 如果需要修改配置,修改完后需要重新初始化,否则无法生效。
gitlab-ctl start
gitlab-ctl status
3.3通过浏览器访问地址。
登录账号为root
登录密码在
cat /etc/gitlab/initial_root_password
jenkins 我这边选择的为推荐安装,后续可按特定需求安装特定插件。
3.4设置gitlab,修改登录密码,配置jenkins免密登录的key,实现免密登录。
3.5 在jenkins容器服务器内,生成ssh免密文件,获取Key并添加到gitlab上。
- docker ps
- #查看容器运行情况
-
- docker exec -it 容器id /bin/bash
- #进入容器并指定解释器
-
- ssh-keygen
- #在jenkins容器内运行此命令,生成密钥文件,一路回车即可。
-
- cd /root/.ssh/
- #会看到两个文件 id_rsa id_rsa.pub
-
- cat id_rsa.pub
- #将获取到的key上传到gitlab。
-
-
- ssh-copy-id root@ip
-
- ssh #刚刚的IP试一下 是否可以远程登录
-
- #这里是将生成的密钥文件传到部署服务的服务器上
- #ip为部署服务的服务器,也可以给容器服务器传,也可以给别的服务器传,主要实现免密。
-
-
- #此步骤为Jenkins将编译好,打包好的代码服务,需要分发到部署服务器进行部署,此步骤是方便jenkins部署服务。
-
- #由于jenkins,需要通过连接gitlab实现拉取代码,所以需要生成sshkey,并填写到gitlab上,实现拉取代码,打包操作。后续jenkins需要将打包好的代码发布到服务器,所以需要ssh-copy-id到每个部署服务的服务器上。
3.6创建新项目,并且发送给开发人员地址,他们即可提交代码到仓库,以下为图片教程。
# 将获取到的git地址,连同登录账号密码,一并发送给开发人员,方便他们后续提交代码。
3.8在jenkins容器内配置,环境变量,方便后续打包发布。
配置容器环境变量。
vim /etc/profiles
export MAVEN_HOME=/var/jenkins_home/mvn/apache-maven-3.8.8
export PATH=${PATH}:${MAVEN_HOME}/bin
export JAVA_HOME=/var/jenkins_home/jdk1.8.0_341
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}# 以上目录为容器启动时的映射目录,如果Dockerfile文件改了,这里也需要更改。
# 另外需要自行下载准备maven和jdk 我这里版本号都有,可自行下载
# maven需要修改配置文件,文件详情如下:
settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd"> <localRepository>/var/jenkins_home/mvn/repo</localRepository> <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors>#退出容器,由于容器启动时定义了映射,所以需要在宿主机映射目录下,上传maven和jdk即可。
exit #退出容器
# 以下操作在宿主机进行
cd /opt/jenkins/jenkins-data #此目录为容器映射目录
mkdir mvn
mkdir mvn/repo
#repo 是因为maven会用到这个目录,正常创建即可。
#将maven 上传至 mvn ,将jdk上传至当前目录解压即可。
4.0 现在已经配置好环境变量,现在配置Jenkins的页面。
4.1通过jenkins,新建任务。
记住这个GROUP 和 METHOD 这是一个变量调用,后面脚本的脚本里会说到。
上图这个脚本的位置为jenkins容器内的位置,放置和更改脚本可直接在宿主机对应的映射目录上传脚本即可。
4.2下面来我这边来放这个脚本,供大家参考。
- #!/bin/bash
-
- DATE=`date +%Y-%m-%d_%H:%M:%S`
- METHOD=$1
- GROUP=$2
-
- source /etc/profile
- MAVEN_HOME=/var/jenkins_home/mvn/apache-maven-3.8.8
- DOCKER_WOKER_HOME="/var/jenkins_home/deploy/sjyq" #脚本地址
- DOCKER_WOKER_JAR_HOME="/var/jenkins_home/deploy/sjyq/xxx" #项目地址
-
- function clone_code(){
- echo "开始clone代码..."
- cd $DOCKER_WOKER_HOME ; rm -rf xxx #项目名称
- git clone -b master git@xxx:root/xxx.git #填写git地址
- echo "clone 代码完成..."
- }
-
- function code_maven(){
- echo "开始编译代码..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delayMq-event-push && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delete-information && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delete-monitor && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delete-publicsentiment && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-monitor-MqHandle && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-monitor-isexist && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-monitor-provide && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-review-handle && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/monitor-scheme-task && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/redis-clustor-text && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"wx-master"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/wx-master && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- elif [[ $GROUP == *"monitor_auto_push"* ]];then
- cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn_monitor_auto_push && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
- fi
- echo "代码编译完成..."
- }
-
- usage() {
- echo "Usage: sh java_demo.sh [deploy_jar|rollback_last_version] [jar]"
- exit 1
- }
-
- function scp_jar_file() {
- echo "开始删除旧包..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-delete-information-1.0.jar"
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-delete-monitor-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-delete-publicsentiment-1.0.jar"
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-monitor-isexist-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-monitor-provide-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/kn-review-handle-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/monitor-scheme-task-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/redis-clustor-text-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"wx-master"* ]];then
- ssh root@ip地址1 "rm -rf /data/update/wxproject-1-SNAPSHOT.jar"
- fi
- echo "远程服务器旧版本jar包删除完成..."
-
- echo "开始拷贝文件到远程服务器..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delayMq-event-push/target/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delete-information/target/kn-delete-information-1.0.jar ip地址1:/data/update/
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delete-monitor/target/kn-delete-monitor-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delete-publicsentiment/target/kn-delete-publicsentiment-1.0.jar ip地址1:/data/update/
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-monitor-MqHandle/target/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-monitor-isexist/target/kn-monitor-isexist-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-monitor-provide/target/kn-monitor-provide-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-review-handle/target/kn-review-handle-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/monitor-scheme-task/target/monitor-scheme-task-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/redis-clustor-text/target/redis-clustor-text-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"wx-master"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/wx-master/target/wxproject-1-SNAPSHOT.jar ip地址1:/data/update/
- elif [[ $GROUP == *"monitor_auto_push"* ]];then
- scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn_monitor_auto_push/target/kn_monitor_auto_push-1.0.jar ip地址2:/data/update/
- fi
- }
-
- function bak_jar() {
- echo "开始备份文件..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- ssh ip地址1 "mv /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar-${DATE}"
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- #ssh ip地址1 "mv ${DATE}"
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- ssh ip地址1 "mv /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar-${DATE}"
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- #ssh ip地址1 "mv ${DATE}"
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- ssh ip地址1 "mv /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar-${DATE}"
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- #ssh ip地址1 "mv ${DATE}"
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- ssh ip地址1 "mv /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar-${DATE}"
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- ssh ip地址1 "mv /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar-${DATE}"
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- #ssh ip地址1 "mv ${DATE}"
- echo "404,server not found..."
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- ssh ip地址1 "mv /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar-${DATE}"
- elif [[ $GROUP == *"wx-master"* ]];then
- #ssh ip地址1 "mv ${DATE}"
- echo "404,server not found..."
- fi
- }
-
- function cp_jar_file() {
- echo "开始拷贝文件..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- ssh ip地址1 "cp /data/update/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- ssh ip地址1 "cp /data/update/kn-delete-monitor-0.0.1-SNAPSHOT.jar /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- ssh ip地址1 "cp /data/update/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- ssh ip地址1 "cp /data/update/kn-monitor-provide-0.0.1-SNAPSHOT.jar /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- ssh ip地址1 "cp /data/update/kn-review-handle-0.0.1-SNAPSHOT.jar /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- ssh ip地址1 "cp /data/update/redis-clustor-text-0.0.1-SNAPSHOT.jar /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"monitor_auto_push"* ]];then
- ssh ip地址2 "cp /data/update/kn_monitor_auto_push-1.0.jar /data/monitor_auto_push/kn_monitor_auto_push-1.0.jar"
- elif [[ $GROUP == *"wx-master"* ]];then
- echo "404,server not found..."
- fi
- echo "拷贝文件执行结束..."
- }
-
- function delete_history_jar() {
- echo "开始删除历史版本..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- NUM=$(ssh ip地址1 "ls -l -d -rt /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar* |wc -l")
- if [ ${NUM} -gt 3 ];then
- NAME=$(ssh ip地址1 "ls -l -d -rt /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
- ssh ip地址1 "rm -rf ${NAME}"
- echo "删除历史版本${NAME}成功..."
- fi
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- NUM=$(ssh ip地址1 "ls -l -d -rt /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar* |wc -l")
- if [ ${NUM} -gt 3 ];then
- NAME=$(ssh ip地址1 "ls -l -d -rt /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
- ssh ip地址1 "rm -rf ${NAME}"
- echo "删除历史版本${NAME}成功..."
- fi
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- NUM=$(ssh ip地址1 "ls -l -d -rt /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar* |wc -l")
- if [ ${NUM} -gt 3 ];then
- NAME=$(ssh ip地址1 "ls -l -d -rt /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
- ssh ip地址1 "rm -rf ${NAME}"
- echo "删除历史版本${NAME}成功..."
- fi
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- NUM=$(ssh ip地址1 "ls -l -d -rt /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar* |wc -l")
- if [ ${NUM} -gt 3 ];then
- NAME=$(ssh ip地址1 "ls -l -d -rt /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
- ssh ip地址1 "rm -rf ${NAME}"
- echo "删除历史版本${NAME}成功..."
- fi
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- NUM=$(ssh ip地址1 "ls -l -d -rt /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar* |wc -l")
- if [ ${NUM} -gt 3 ];then
- NAME=$(ssh ip地址1 "ls -l -d -rt /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
- ssh ip地址1 "rm -rf ${NAME}"
- echo "删除历史版本${NAME}成功..."
- fi
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- NUM=$(ssh ip地址1 "ls -l -d -rt /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar* |wc -l")
- if [ ${NUM} -gt 3 ];then
- NAME=$(ssh ip地址1 "ls -l -d -rt /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
- ssh ip地址1 "rm -rf ${NAME}"
- echo "删除历史版本${NAME}成功..."
- fi
- elif [[ $GROUP == *"wx-master"* ]];then
- echo "404,server not found..."
- fi
- echo "删除历史版本结束..."
- }
-
- function rollback_last_version() {
- echo "开始回滚..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- ssh ip地址1 "rm -rf /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- ssh ip地址1 "rm -rf /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- ssh ip地址1 "rm -rf /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- ssh ip地址1 "rm -rf /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- ssh ip地址1 "rm -rf /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- ssh ip地址1 "rm -rf /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar"
- elif [[ $GROUP == *"wx-master"* ]];then
- echo "404,server not found..."
- fi
- echo "回滚完成..."
- }
-
- function restart_app() {
- echo "重启中..."
- if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
- ssh ip地址1 "/bin/bash /data/kn-delayMq-event-push/server.sh restart"
- elif [[ $GROUP == *"kn-delete-information"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-delete-monitor"* ]];then
- ssh ip地址1 "/bin/bash /data/delete-monitor/server.sh restart"
- elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
- ssh ip地址1 "/bin/bash /data/MqHandle/server.sh restart"
- elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"kn-monitor-provide"* ]];then
- ssh ip地址1 "/bin/bash /data/provide/server.sh restart"
- elif [[ $GROUP == *"kn-review-handle"* ]];then
- ssh ip地址1 "/bin/bash /data/review-handle/server.sh restart"
- elif [[ $GROUP == *"monitor-scheme-task"* ]];then
- echo "404,server not found..."
- elif [[ $GROUP == *"redis-clustor-text"* ]];then
- ssh ip地址1 "/bin/bash /data/redis-clustor-text/server.sh restart"
- elif [[ $GROUP == *"monitor_auto_push"* ]];then
- ssh ip地址2 "/bin/bash /data/monitor_auto_push/server.sh restart"
- elif [[ $GROUP == *"wx-master"* ]];then
- echo "404,server not found..."
- fi
- echo "重启完成..."
- }
-
- main(){
- case $1 in
- deploy_jar)
- clone_code
- code_maven
- scp_jar_file
- bak_jar
- cp_jar_file
- delete_history_jar
- restart_app
- ;;
- rollback_last_version)
- rollback_last_version
- restart_app
- ;;
- *)
- usage
- ;;
- esac
- }
-
- main $1 $2
脚本按自己需求进行修改,需要注意的是发服务的服务器,需要提前配置好免密,否则jenkins无法免密登录,会导致部署失败。
注意: 第一次构建运行时,由于linux系统验证问题,需要手动点击yes,所以会导致构建报错,
需要手动在容器内 git clone 输入一次yes。
4.3 下面就可以在jenkins,上进行构建发布。
这里可查看构建和发布输出的日志。
输出 Finished: SUCCESS 代表发布成功,去对应部署服务的服务器上验证即可。
补充一点,编辑打包和发布的脚本里,发布到对应服务器上时,会在发布服务的服务器上执行一个服务器重启的脚本,这个脚本需要提前准备,我也把我目前用的重启脚本发出来,大家可以参考。
- [root@iv-yd5ck6aeww5i3z3d41dk ~]# cat server.sh
-
- #!/bin/bash
-
- APP_NAME=test.jar
- BASE_PATH=/root
-
- usage() {
- echo "Usage: sh java_demo.sh [start|stop|restart|status]"
- exit 1
- }
-
- is_exist() {
- pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}' `
- #如果不存在返回1,存在返回0
- if [ -z "${pid}" ]; then
- return 1
- else
- return 0
- fi
- }
-
- start() {
- is_exist
- if [ $? -eq "0" ]; then
- echo "${APP_NAME} is already running. pid=${pid} ."
- else
- cd $BASE_PATH
- nohup java -jar -XX:+UseG1GC -Xms1024m -Xmx1024m -Xss256k -XX:MaxGCPauseMillis=100 -Xloggc:./gc.log -XX:+PrintGCTimeStamps -XX:+PrintGCDetails $APP_NAME > /root/test.log 2>&1 &
- echo $! > test.pid
- #tail -f test.log
- fi
- }
-
- stop() {
- is_exist
- if [ $? -eq 0 ]; then
- kill $pid
- sleep 5
- pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}'`
- while [ -n "$pid" ];
- do
- sleep 2
- pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}'`
- echo "The program exits gracefully, please wait..."
- done
- else
- echo "${APP_NAME} is not running"
- fi
- }
-
- status() {
- is_exist
- if [ $? -eq "0" ]; then
- echo "${APP_NAME} is running. Pid is ${pid}"
- else
- echo "${APP_NAME} is not running."
- fi
- }
-
- restart() {
- stop
- sleep 1
- start
- }
-
-
- case "$1" in
- "start")
- start
- ;;
- "stop")
- stop
- ;;
- "status")
- status
- ;;
- "restart")
- restart
- ;;
- *)
- usage
- ;;
- esac
以上内容,可供大家测试使用,如果问题或者更好的方法和脚本,也请各位大佬提出。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。