jenkins install

jenkins 安装

  1. 基础软件安装

    1
    2
    3
    [root@jenkins ~]# yum -y install epel-release
    [root@jenkins ~]# yum -y install gcc gcc-c++ make cmake bison libtool autoconf automake zip unzip bzip2 zlib zlib-devel openssl openssl-devel pcre pcre-devel bison-devel ncurses-devel tcl tcl-devel perl-Digest-SHA1 GeoIP GeoIP-devel gperftools gperftools-devel libatomic_ops-devel gtest gtest-devel glibc-devel unixODBC-devel fop libperl libpython readline readline-devel python2-pip readline readline-devel readline-static openssl openssl-devel openssl-static sqlite-devel bzip2-devel bzip2-libs openldap-devel curl-devel expat-devel gettext-devel perl-ExtUtils-MakeMaker
    [root@jenkins ~]# yum -y install git lftp ntpdate vim wget telnet dstat tree lrzsz net-tools nmap-ncat nmap sysstat
  2. 创建一个普通用户

    1
    [root@jenkins ~]# useradd -s /sbin/nologin jenkins
  3. java 安装

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [root@jenkins app]# tar xzf jdk-8u131-linux-x64.tar.gz
    [root@jenkins app]# mv jdk1.8.0_131 /mnt/app/java
    [root@jenkins app]# chown -R jenkins.jenkins /mnt/app/java

    [root@jenkins app]# echo 'export JAVA_HOME=/mnt/app/java'|tee /etc/profile.d/java.sh
    [root@jenkins app]# echo 'export JRE_HOME=${JAVA_HOME}/jre'|tee -a /etc/profile.d/java.sh
    [root@jenkins app]# echo 'export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib'|tee -a /etc/profile.d/java.sh
    [root@jenkins app]# echo 'export PATH=${JAVA_HOME}/bin:${PATH}'|tee -a /etc/profile.d/java.sh
    [root@jenkins app]# source /etc/profile

    [root@jenkins ~]# java -version
    java version "1.8.0_131"
    Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
  4. maven安装

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [root@jenkins app]# tar xzf apache-maven-3.5.0-bin.tar.gz
    [root@jenkins app]# mv apache-maven-3.5.0 /mnt/app/maven
    [root@jenkins app]# chown -R jenkins.jenkins /mnt/app/maven
    [root@jenkins app]# echo 'MAVEN_HOME=/mnt/app/maven' | tee -a /etc/profile.d/maven.sh
    [root@jenkins app]# echo 'export PATH=${PATH}:${MAVEN_HOME}/bin' | tee -a /etc/profile.d/maven.sh
    [root@jenkins app]# source /etc/profile

    [root@jenkins app]# mvn --version
    Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
    Maven home: /mnt/app/maven
    Java version: 1.8.0_131, vendor: Oracle Corporation
    Java home: /mnt/app/java/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.10.0-123.el7.x86_64", arch: "amd64", family: "unix"
  5. Tomcat 安装

    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
    [root@jenkins app]# tar xzf apache-tomcat-8.5.16.tar.gz
    [root@jenkins app]# mv apache-tomcat-8.5.16 /mnt/app/jenkins
    [root@jenkins app]# chown -R jenkins.jenkins /mnt/app/jenkins

    [root@jenkins ~]# mkdir /mnt/data/jenkins
    [root@jenkins ~]# chown -R jenkins.jenkins /mnt/data/jenkins
    [root@jenkins ~]# mkdir -p /mnt/log/jenkins
    [root@jenkins ~]# chown -R jenkins.jenkins /mnt/log/jenkins

    [root@jenkins ~]# vim /mnt/app/jenkins/conf/server.xml
    <Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />

    <Host name="localhost" appBase="/mnt/data/jenkins"
    unpackWARs="true" autoDeploy="true">

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/mnt/log/jenkins"
    prefix="jenkins_access_log" suffix=".txt"
    pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>

    [root@jenkins ~]# vim /mnt/app/jenkins/conf/logging.properties
    1catalina.org.apache.juli.AsyncFileHandler.directory = /mnt/log/jenkins
    2localhost.org.apache.juli.AsyncFileHandler.directory = /mnt/log/jenkins
    3manager.org.apache.juli.AsyncFileHandler.directory = /mnt/log/jenkins
    4host-manager.org.apache.juli.AsyncFileHandler.directory = /mnt/log/jenkins

    [root@jenkins ~]# vim /mnt/app/jenkins/bin/catalina.sh
    JAVA_OPTS="-server -Xmx14g -Xms8g -Xmn1024m -Xss512k -XX:SurvivorRatio=6 -XX:ParallelGCThreads=8 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC"
  6. jenkins 安装

    1
    2
    [root@jenkins app]# cp jenkins.war /mnt/data/jenkins/
    [root@jenkins app]# chown -R jenkins.jenkins /mnt/data/jenkins
  7. jenkins启动脚本

    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
    [root@jenkins ~]# cat > /etc/init.d/jenkins <<EOF
    #!/bin/bash

    if [ $# != 1 ];then
    echo "Please input:
    /etc/init.d/$0 start
    /etc/init.d/$0 stop
    /etc/init.d/$0 status
    "
    exit 0
    fi

    STATUS=$1
    CATALINA="/mnt/app/jenkins/bin/catalina.sh "
    ROLE="jenkins"

    if [ $STATUS == "start" ];then
    $CATALINA start
    fi

    if [ $STATUS == "stop" ];then
    NUM=$(ps -ef|grep -w ${ROLE}|grep -w '/mnt/app/jenkins'|grep -v grep|awk '{print $2}'|wc -l)
    if [ $NUM != 1 ];then
    echo "Please check $ROLE, There are $NUM processes."
    fi

    PID=$(ps -ef|grep -w ${ROLE}|grep -w '/mnt/app/jenkins'|grep -v grep|awk '{print $2}')
    kill -9 ${PID}
    if [ $? == 0 ];then
    echo "$ROLE stoped success."
    else
    echo "$ROLE stoped fail."
    fi
    fi

    if [ $STATUS == "status" ];then
    NUM=$(ps -ef|grep -w ${ROLE}|grep -w '/mnt/app/jenkins'|grep -v grep|awk '{print $2}'|wc -l)

    if [ $NUM == 0 ];then
    echo "$ROLE is stopped."
    exit 0
    fi

    if [ $NUM != 1 ];then
    echo "Please check $ROLE, There are $NUM processes."
    fi

    PID=$(ps -ef|grep -w ${ROLE}|grep -w '/mnt/app/jenkins'|grep -v grep|awk '{print $2}')
    echo "$ROLE is running. PID: $PID"
    fi
    EOF
    [root@jenkins ~]# chmod +x /etc/init.d/jenkins
  8. jenkins systemd脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@jenkins ~]# cat > /usr/lib/systemd/system/jenkins.service <<EOF
    [Unit]
    Description=Apache Tomcat/8.5.16
    After=syslog.target network.target remote-fs.target nss-lookup.target

    [Service]
    Type=forking
    ExecStart=/mnt/app/jenkins/bin/catalina.sh start
    ExecReload=kill -s HUP $MAINPID
    ExecStop=kill -s QUIT $MAINPID
    PrivateTmp=true
    User=jenkins
    Group=jenkins

    [Install]
    WantedBy=multi-user.target
    EOF
    [root@jenkins ~]# systemctl daemon-reload
    [root@jenkins ~]# systemctl start jenkins
    [root@jenkins ~]# systemctl enable jenkins
  9. jenkins 启动

    1
    2
    3
    4
    5
    6
    7
    [root@jenkins ~]# su - jenkins
    [jenkins@jenkins ~]$ /mnt/app/jenkins/bin/catalina.sh configtest
    [jenkins@jenkins ~]$ /mnt/app/jenkins/bin/catalina.sh start
    or:
    [jenkins@jenkins ~]$ /etc/init.d/jenkins start
    [jenkins@jenkins ~]$ /etc/init.d/jenkins stop
    [jenkins@jenkins ~]$ /etc/init.d/jenkins status
  10. jenkins nginx反向代理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [root@192 vhost]# cat jenkins.conf
    # jenkins
    upstream switchjenkins{
    server 192.168.13.214:8080 max_fails=3 fail_timeout=30s;
    }

    server {
    listen 80;
    server_name jenkins.domain.cn;

    location / {
    rewrite ^/ http://jenkins.domain.cn/jenkins;
    }

    location /jenkins {
    proxy_pass http://switchjenkins;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    access_log /mnt/log/nginx/jenkins.domain.cn.log main;
    }
  11. 访问jenkins,并初始化

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    //1.访问web界面
    http://jenkins.domain.cn
    //2.输入密码:
    [jenkins@jenkins ~]$ cat ~/.jenkins/secrets/initialAdminPassword
    357b1ccc008d4406ba3eca72a741aa7e
    //3.安装插件(等待安装)
    我们先选择推荐插件进行安装"Install suggested plugins"
    //4.创建管理员账号
    admin/admin123
    //5.启动jenkins

    //日常操作
    http://jenkins.domain.cn/jenkins/stop 关闭jenkins
    http://jenkins.domain.cn/jenkins/start 开启jenkins
    http://jenkins.domain.cn/jenkins/restart 重启jenkins
    http://jenkins.domain.cn/jenkins/reload 重新加载jenkins
  12. 安装常用插件

    1
    2
    * Deploy to Container Plugin   支持将代码部署到tomcat容器
    * GIT plugin