主要内容 jenkins-2.235.3-1.1 安装 使用
更新历史 2020-08-04 增加jenkins的清华镜像设置 2020-11-14 国内镜像源设置 2025-03-22 增加脚本样例
安装 官方指南
前提 JDK1.8以上 安装
RedHat系 YUM安装 centOS 8 安装稳定版本如下:
1 2 3 4 wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key yum install jenkins
如果网络慢, 可以手工安装 清华镜像, 如下 安装版本jenkins-xxxx.noarch.rpm
1 2 wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-xxx.noarch.rpm yum install -y jenkins-xxxx.noarch.rpm
安装结果
daemon : /etc/init.d/jenkins
user : jenkins (查看passwd,获得工作路径)
Log file : /var/log/jenkins/jenkins.log
configuration : /etc/sysconfig/jenkins (建议修改JENKINS_HOME到空间较大的路径)
初始密码 :查看 /var/log/jenkins/jenkins.log
启动/停止 1 2 systemctl start/stop/restart jenkins systemctl enable jenkins ##开机自动启动
ps: 别忘了关闭防火墙或者设置防火墙
选推荐插件
插件管理 升级站点 设置 由于长城的原因, 建议把插件站点设置成国内的。
特别注意 如果是操作系统是CentOS 8
执行如下命令, 重启 jenkins
1 update-crypto-policies --set LEGACY
配置
JDK 一般yum安装的 JAVA_HOME 为 /usr/lib/jvm/java
maven 安装maven yum安装的 M2_HOME 为 /usr/share/maven
插件 这些必须安装:
1 2 3 4 Git plugin Git client plugin GitLab Plugin maven plugin
配置 JDK, Jenkins Location, Git plugin, Gitlab
ssh-key 如果连接 Source Code Management 采用 ssh 连接, 需要先生成 ssh-key
ssh-keygen -t rsa -C "your_email@your_email.com" -b 4096
注意生成目录, jenkins ssh-key的默认目录为: /var/lib/jenkins/
下的 .ssh/
, 以 jenkins 用户的home目录 为准。
New Item mavne project
root project Maven project name Description
Discard Old Builds Source Code Management
Build Root POM:pom.xml Goals and options: clean deploy -N
sub project 其他 参照上方, Post Steps –> Excute shell (启动tomcat等Web server) shell可能长的像下面这样
1 2 3 4 5 6 7 8 9 10 #!/bin/bash -ex JAVA_OPTS="-Xms768m -Xmx1000m -XX:PermSize=512m -XX:MaxPermSize=1024m" cd /data/apache-tomcat/bin/catalina.sh stop 35 -force rm -rf /data/apache-tomcat/webapps/xxxxxxxx.war /data/apache-tomcat/webapps/xxxxxxxxcp $WORKSPACE /target/xxxxxxxx.war /data/apache-tomcat/webappsBUILD_ID=please_do_not_kill_me sleep 20sbin/catalina.sh start
项目权限 项目权限
样例
1 2 3 4 5 6 7 8 9 #!/bin/bash -ex PID=`ps -ef|grep jenkins|grep /bin/java|grep -v grep|awk '{print $2}' ` echo $PID if [ "" != "$PID " ]; then echo "killing $PID " kill -9 $PID else echo "leyun not running!" fi
1 2 3 4 5 6 7 8 9 10 11 12 13 #!/bin/bash -ex cd /data/leyun/apache-tomcat-8.5.60/rm -rf /data/leyun/apache-tomcat-8.5.60/webapps/ROOT.war /data/leyun/apache-tomcat-8.5.60/webapps/ROOT cp $WORKSPACE /target/leyun-server-center-service.war /data/leyun/apache-tomcat-8.5.60/webapps/ROOT.warBUILD_ID=please_do_not_kill_me sleep 10sbin/catalina.sh start
完整shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #!/bin/bash -ex VER="yaoquansu-infra-biz-1.0.0-SNAPSHOT" PORT=9301 jdwp_PORT=19301 TARGET_DIR="/data/yqs/yqs2/yaoquansu-infra-biz" PID=$(jps | grep "$VER " | awk '{print $1}' ) || true if [ -z "$PID " ]; then echo "yaoquansu-infra-biz not running!" else echo "Found process PID: $PID ." echo "killing $PID " kill -9 $PID fi if [ -d "$TARGET_DIR " ]; then echo "目录已存在: $TARGET_DIR " else echo "目录不存在,正在创建: $TARGET_DIR " mkdir -p "$TARGET_DIR " if [ $? -ne 0 ]; then echo "错误: 目录创建失败!" exit 1 fi fi rm -rf "$TARGET_DIR " /libmv $WORKSPACE /yaoquansu-infra-biz/target/lib "$TARGET_DIR " /mv $WORKSPACE /yaoquansu-infra-biz/target/"${VER} " .jar "$TARGET_DIR /" BUILD_ID=please_do_not_kill_me cd "$TARGET_DIR /" pwd if [ -f "output.log" ]; then rm output.log fi sleep 1snohup java \ -Xmx1000M \ -XX:MetaspaceSize=512M \ -XX:+UseG1GC \ -XX:+HeapDumpOnOutOfMemoryError \ "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:${jdwp_PORT} " \ -Dloader.path="./lib" \ -jar "${VER} .jar" \ --server.port="${PORT} " \ --spring.profiles.active=uat \ > output.log 2>&1 & PID=$(lsof -ti :"$PORT " -sTCP:LISTEN) || true if [ -z "$PID " ]; then echo "yaoquansu-infra-biz running!" else echo "Found process fail" fi
问题 第一次ssh连接 gitServer Source Code Management 中 git 第一次ssh连接, 会有 host error
解决方法:
jenkins 用户登录, /etc/passwd
中 jenkins用户的默认shell 需要修改成 bin/bash
执行命令 ssh git@gitServerIP
, 接受yes
还原 jenkins用户的默认shell为 bin/false
ssh 非默认22端口 vim /var/lib/jenkins/.ssh/config
1 2 Host gitlab.xianzhongwang.com Port 34
1 chmod 644 /var/lib/jenkins/.ssh/config
网络导致日志疯狂增长 一旦jenkins所在的服务器的网络出现问题, jenkins的日志文件(/var/log/jenkins/jenkins.log)大小会疯狂快速增长,直到撑爆硬盘 日志内容大概为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Jul 29, 2015 5:04:59 AM javax.jmdns.impl.constants.DNSRecordClass classForIndex WARNING: Could not find record class for index: -1 Jul 29, 2015 5:04:59 AM javax.jmdns.impl.DNSIncoming$MessageInputStream readName SEVERE: bad domain name: possible circular name detected. Bad offset: 0xffffffff at 0xb6 Jul 29, 2015 5:04:59 AM javax.jmdns.impl.constants.DNSRecordType typeForIndex SEVERE: Could not find record type for index: -1 Jul 29, 2015 5:04:59 AM javax.jmdns.impl.DNSIncoming readQuestion SEVERE: Could not find record type : dns[query,108.61.99.169:53227, length=184, id =0x5c78, flags=0x3030, questions=6 questions: [DNSQuestion@84075318 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: 0\x00\x01\x00\x00\x00\x00\x00\x00\x09\x5F\x73\x6.\x72\x76\x69\x63\x65\x73\x07\x5F\x64\x6E\x73\x2D\x73\.4\x04\x5F\x75\x64\x70\x05\x6C\x6F\x63\x61\x6C\x00\x00\.C\x00\x01ϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿ.] [DNSQuestion@9629900 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] [DNSQuestion@788455775 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] [DNSQuestion@1602108435 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] [DNSQuestion@1486500959 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] [DNSQuestion@1944352362 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]] question: [DNSQuestion@84075318 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: 0\x00\x01\x00\x00\x00\x00\x00\x00\x09\x5F\x73\x6.\x72\x76\x69\x63\x65\x73\x07\x5F\x64\x6E\x73\x2D\x73\.4\x04\x5F\x75\x64\x70\x05\x6C\x6F\x63\x61\x6C\x00\x00\.C\x00\x01ϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿϿ.] question: [DNSQuestion@9629900 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] question: [DNSQuestion@788455775 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] question: [DNSQuestion@1602108435 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] question: [DNSQuestion@1486500959 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] question: [DNSQuestion@1944352362 type : TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ] 0: 5c7830305c783030 5c7830305c783030 5c7830305c783031 5c7830305c783030 \x00\x00 \x00\x00 \x00\x01 \x00\x00 20: 5c7830305c783030 5c7830305c783030 5c7830395c783546 5c7837335c783635 \x00\x00 \x00\x00 \x09\x5F \x73\x65 40: 5c7837325c783736 5c7836395c783633 5c7836355c783733 5c7830375c783546 \x72\x76 \x69\x63 \x65\x73 \x07\x5F 60: 5c7836345c783645 5c7837335c783244 5c7837335c783634 5c7830345c783546 \x64\x6E \x73\x2D \x73\x64 \x04\x5F 80: 5c7837355c783634 5c7837305c783035 5c7836435c783646 5c7836335c783631 \x75\x64 \x70\x05 \x6C\x6F \x63\x61 a0: 5c7836435c783030 5c7830305c783043 5c7830305c783031 \x6C\x00 \x00\x0C \x00\x01
jenkins官方的issue,目前尚未fixed
1 https://issues.jenkins-ci.org/browse/JENKINS-10160
临时解决方案: 修改jenkins的java启动参数,编辑 /etc/sysconfig/jenkins, 看看效果
1 JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.DNSMultiCast.disabled=true -Dhudson.udp=-1"