Centos下jenkins的安装

非docker安装

通过设置jenkins官方repo仓库下载

1
2
3
4
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install jenkins
cat /etc/rc.d/init.d/jenkins

安装后,jenkins.war默认地址为
JENKINS_WAR=”/usr/lib/jenkins/jenkins.war”

jenkins的config地址为
JENKINS_CONFIG=/etc/sysconfig/jenkins

jenkins服务的启动/停止/重启脚本为
/etc/init.d/jenkins

下载rpm安装

1
2
wget https://pkg.jenkins.io/redhat-stable/jenkins-2.176.1-1.1.noarch.rpm
rpm -ivh jenkins-2.176.1-1.1.noarch.rpm

下载war包

1
2
3
wget https://mirrors.huaweicloud.com/jenkins/war-stable/latest/jenkins.war
#wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
java -jar jenkins.war

常用的启动参数为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java -Duser.timezone=GMT+08 \
-Djava.util.logging.config.file=/var/jenkins_home/jenkins.logging.properties \
-Dgroovy.grape.report.downloads=true -Divy.message.logger.level=4 \
-Dhudson.model.ParametersAction.keepUndefinedParameters=true \
-Dhudson.security.ArtifactsPermission=true -Djava.awt.headless=true \
-Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Shanghai \
-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 \
-Dhudson.slaves.NodeProvisioner.MARGIN=50 \
-Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 \
-Dhudson.slaves.NodeProvisioner.initialDelay=0 \
-Dhudson.slaves.NodeProvisioner.recurrencePeriod=2 \
-Dhudson.model.LoadStatistics.clock=2 \
-Dhudson.model.LoadStatistics.decay=0.1 \
-Dpermissive-script-security.enabled=true \
-server -Xms10g -Xmx32g -XX:MaxPermSize=4g -XX:+HeapDumpOnOutOfMemoryError \
-Xloggc:/var/jenkins_home/jenkins_gc.log \
-DJENKINS_HOME=/var/jenkins_home \
-jar jenkins.war --logfile=/var/jenkins_home/jenkins.log

jenkins.logging.properties 的内容参见说明
permissive-script-security.enabled=true表示默认允许执行unsecured脚本,不再提示需要审核

虚拟机中启动jenkins后不能访问

  1. 关闭防火墙

    1
    2
    3
    systemctl status firewalld
    systemctl stop firewalld
    systemctl disable firewalld
  2. 配置防火墙开放jinkins使用的端口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 检查当前防火墙开放的端口
    [root@localhost ~]# firewall-cmd --list-ports

    # 配置防火墙开放端口
    [root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=8080/tcp
    success
    [root@localhost ~]# firewall-cmd --list-ports

    # 重启防火墙
    [root@localhost ~]# systemctl reload firewalld
    [root@localhost ~]# firewall-cmd --list-ports
    8080/tcp

docker安装

docker hub中有两种jenkins的镜像,jenkins官方推荐使用 jenkinsci/blueocean 镜像,该镜像中包含了blueocean插件,该镜像会在blueocean发布新版本时同步发布。还有一个镜像 jenkins/jenkins, 为jenkins的纯净版本。

jenkins/jenkinsgithub地址

容器化安装时,支持配置下面三个环境变量,来定义jenkins的配置

  • JAVA_OPTS

  • JENKINS_HOME

  • JENKINS_OPTS

  • JENKINS_SLAVE_AGENT_PORT

1
2
3
4
5
6
7
8
9
10
11
12
docker run \
--restart=always \
-d \
-p 80:8080 \
-p 31281:31281 \
-e JENKINS_SLAVE_AGENT_PORT=31281 \
-e JENKINS_HOME="/var/jenkins_home" \
-e JAVA_OPTS="-Duser.timezone=GMT+08 -Dgroovy.grape.report.downloads=true -Divy.message.logger.level=4 -Djava.util.logging.config.file=/var/jenkins_home/jenkins.logging.properties -Dhudson.model.ParametersAction.keepUndefinedParameters=true -Dhudson.security.ArtifactsPermission=true -Djava.awt.headless=true -Dorg.apache.commons.jelly.tags.fmt.timeZone=Asia/Shanghai -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.recurrencePeriod=2 -Dhudson.model.LoadStatistics.clock=2 -Dhudson.model.LoadStatistics.decay=0.1 -server -Xms10g -Xmx32g -XX:MaxPermSize=4g -XX:+HeapDumpOnOutOfMemoryError -Xloggc:/var/jenkins_home/jenkins_gc.log" \
-e JENKINS_OPTS="--logfile=/var/jenkins_home/logs/jenkins.log" \
-v /data/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkins/jenkins

jenkins.logging.properties 的内容参见说明

docker运行jenkins时提示权限错误

1
2
touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

因为容器启动时默认采用jenkins用户,uid和gid分别时 1000 和 1000,需要将挂载到容器里面的JENKINS_HOME对应的宿主机上的目录的所有者修改下

1
chown -R 1000:1000 /data/jenkins_home