Ubuntu安装Jenkins并配置改装

安装包

wget http://mirrors.jenkins-ci.org/debian-stable/jenkins_2.277.3_all.deb

按照官方提示安装

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

向文件/etc/apt/sources.list中加入如下行

deb https://pkg.jenkins.io/debian-stable binary/

执行升级安装

 

安装完成后 find / -name jenkins 查看jenkins默认安装目录

1、配置文件:/etc/default/jenkins

2、启动脚本:/etc/init.d/jenkins

3、监听端口:8080

4、war包位置:/usr/lib/jenkins/jenkins.war

5、缓存目录:/var/cache/jenkins

6、文档目录:/usr/share/doc/jenkins

7、日志文件:/var/log/jenkins/

目录修改配置如下:

# mkdir /opt/jenkins

# mkdir /opt/jenkins/logs && mkdir /opt/jenkins/plugins && mkdir /opt/jenkins/conf && mkdir /opt/jenkins/lib && mkdir /opt/jenkins/bin && mkdir /opt/jenkins/home && mkdir /opt/jenkins/cache

分别存放日志、插件、配置文件、war包、启动文件、工作目录、缓存

# mv /etc/default/jenkins /opt/jenkins/conf && ln -s /opt/jenkins/conf/jenkins /etc/default/jenkins

# mv /etc/init.d/jenkins /opt/jenkins/bin && ln -s /opt/jenkins/bin/jenkins /etc/init.d/jenkins

# mv /usr/share/jenkins/jenkins.war /opt/jenkins/lib

/opt/jenkins/conf/jenkins修改如下:

# defaults for Jenkins automation server

# pulled in from the init script; makes things easier.
NAME=jenkins

# arguments to pass to java

# Allow graphs etc. to work even when an X server is present
JAVA_ARGS="-Djava.awt.headless=true"

#JAVA_ARGS="-Xmx256m"

# make jenkins listen on IPv4 address
#JAVA_ARGS="-Djava.net.preferIPv4Stack=true"

PIDFILE=/var/run/$NAME/$NAME.pid

# user and group to be invoked as (default to jenkins)
JENKINS_USER=$NAME
JENKINS_GROUP=$NAME

# location of the jenkins war file
#JENKINS_WAR=/usr/share/$NAME/$NAME.war
JENKINS_WAR=/opt/$NAME/lib/$NAME.war

# jenkins home location
#JENKINS_HOME=/var/lib/$NAME
JENKINS_HOME=/opt/$NAME

# set this to false if you don't want Jenkins to run by itself
# in this set up, you are expected to provide a servlet container
# to host jenkins.
RUN_STANDALONE=true

# log location.  this may be a syslog facility.priority
JENKINS_LOG=/opt/$NAME/logs/$NAME.log
#JENKINS_LOG=daemon.info

# Whether to enable web access logging or not.
# Set to "yes" to enable logging to /var/log/$NAME/access_log
JENKINS_ENABLE_ACCESS_LOG="no"

# OS LIMITS SETUP
#   comment this out to observe /etc/security/limits.conf
#   this is on by default because http://github.com/jenkinsci/jenkins/commit/2fb288474e980d0e7ff9c4a3b768874835a3e92e
#   reported that Ubuntu's PAM configuration doesn't include pam_limits.so, and as a result the # of file
#   descriptors are forced to 1024 regardless of /etc/security/limits.conf
MAXOPENFILES=8192

# set the umask to control permission bits of files that Jenkins creates.
#   027 makes files read-only for group and inaccessible for others, which some security sensitive users
#   might consider benefitial, especially if Jenkins runs in a box that's used for multiple purposes.
#   Beware that 027 permission would interfere with sudo scripts that run on the master (JENKINS-25065.)
#
#   Note also that the particularly sensitive part of $JENKINS_HOME (such as credentials) are always
#   written without 'others' access. So the umask values only affect job configuration, build records,
#   that sort of things.
#
#   If commented out, the value from the OS is inherited,  which is normally 022 (as of Ubuntu 12.04,
#   by default umask comes from pam_umask(8) and /etc/login.defs

# UMASK=027

# port for HTTP connector (default 8080; disable with -1)
HTTP_PORT=5070


# servlet context, important if you want to use apache proxying
PREFIX=/$NAME

# arguments to pass to jenkins.
# --javahome=$JAVA_HOME
# --httpListenAddress=$HTTP_HOST (default 0.0.0.0)
# --httpPort=$HTTP_PORT (default 8080; disable with -1)
# --httpsPort=$HTTP_PORT
# --argumentsRealm.passwd.$ADMIN_USER=[password]
# --argumentsRealm.roles.$ADMIN_USER=admin
# --webroot=~/.jenkins/war
# --prefix=$PREFIX

JENKINS_ARGS="--webroot=/opt/$NAME/cache/war --httpPort=$HTTP_PORT"

 

/opt/jenkins/bin/jenkins修改如下:

#!/bin/bash
# /etc/init.d/jenkins
# debian-compatible jenkins startup script.
# Amelia A Lewis
#
### BEGIN INIT INFO
# Provides:          jenkins
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Jenkins at boot time
# Description:       Controls Jenkins Automation Server
### END INIT INFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/opt/jdk1.8.0_261/bin

DESC="Jenkins Automation Server"
NAME=jenkins
SCRIPTNAME=/opt/jenkins/bin/$NAME

[ -r /opt/jenkins/conf/$NAME ] && . /opt/jenkins/conf/$NAME

#DAEMON=$JENKINS_SH
DAEMON=/usr/bin/daemon
DAEMON_ARGS="--name=$NAME --inherit --env=JENKINS_HOME=$JENKINS_HOME --output=$JENKINS_LOG --pidfile=$PIDFILE"
JAVA=`type -p java`

if [ -n "$UMASK" ]; then
    DAEMON_ARGS="$DAEMON_ARGS --umask=$UMASK"
fi
if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then
    JENKINS_ARGS="$JENKINS_ARGS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file
=/var/log/$NAME/access_log"
fi

SU=/bin/su

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then
    echo "daemon package not installed" >&2
    exit 1
fi

# Exit if not supposed to run standalone
if [ "$RUN_STANDALONE" = "false" ]; then
    echo "Not configured to run standalone" >&2
    exit 1
fi

# Make sure there exists a java executable, it may not be always the case
if [ -z "$JAVA" ]; then
    echo "ERROR: No Java executable found in current PATH: $PATH" >&2
    echo "If you actually have java installed on the system make sure the executable is in the aforementioned path and that 'type -p java' returns the java
executable path" >&2
    exit 1
fi

# Which Java versions can be used to run Jenkins
JAVA_ALLOWED_VERSIONS=( "1.8")
# Work out the JAVA version we are working with:
JAVA_VERSION=$($JAVA -version 2>&1 | sed -n ';s/.* version "\([0-9]\{2,\}\|[0-9]\.[0-9]\)\..*".*/\1/p;')

if [[ ${JAVA_ALLOWED_VERSIONS[*]} =~ "$JAVA_VERSION" ]]; then
    echo "Correct java version found" >&2
else
    echo "Found an incorrect Java version" >&2
    echo "Java version found:" >&2
    echo $($JAVA -version) >&2
    echo "Aborting" >&2
    exit 1
fi

# load environments
if [ -r /etc/default/locale ]; then
  . /etc/default/locale
  export LANG LANGUAGE
elif [ -r /etc/environment ]; then
  . /etc/environment
  export LANG LANGUAGE
fi

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# Make sure we run as root, since setting the max open files through
# ulimit requires root access
if [ `id -u` -ne 0 ]; then
    echo "The $NAME init script can only be run as root"
    exit 1
fi


check_tcp_port() {
    local service=$1
    local assigned=$2
    local default=$3
    local assigned_address=$4
    local default_address=$5

    if [ -n "$assigned" ]; then
        port=$assigned
    else
        port=$default
    fi

    if [ -n "$assigned_address" ]; then
        address=$assigned_address
    else
        address=$default_address
    fi

    count=`netstat --listen --numeric-ports | grep $address\:$port[[:space:]] | grep -c . `

    if [ $count -ne 0 ]; then
        echo "The selected $service port ($port) on address $address seems to be in use by another program "
        echo "Please select another address/port combination to use for $NAME"
        return 1
    fi
}

#
# Function that starts the daemon/service
#
do_start()
{
    # the default location is /var/run/jenkins/jenkins.pid but the parent directory needs to be created
    mkdir `dirname $PIDFILE` > /dev/null 2>&1 || true
    chown $JENKINS_USER `dirname $PIDFILE`
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    $DAEMON $DAEMON_ARGS --running && return 1

    # Verify if there is a jenkins process already running without a daemon
    get_running || return 2

    # Verify that the jenkins port is not already in use, winstone does not exit
    # even for BindException
    check_tcp_port "http" "$HTTP_PORT" "8080" "$HTTP_HOST" "0.0.0.0" || return 2

    # If the var MAXOPENFILES is enabled in /etc/default/jenkins then set the max open files to the
    # proper value
    if [ -n "$MAXOPENFILES" ]; then
        [ "$VERBOSE" != no ] && echo Setting up max open files limit to $MAXOPENFILES
        ulimit -n $MAXOPENFILES
    fi

    # notify of explicit umask
    if [ -n "$UMASK" ]; then
        [ "$VERBOSE" != no ] && echo Setting umask to $UMASK
    fi

    # --user in daemon doesn't prepare environment variables like HOME, USER, LOGNAME or USERNAME,
    # so we let su do so for us now
    $SU -l $JENKINS_USER --shell=/bin/bash -c "$DAEMON $DAEMON_ARGS -- $JAVA $JAVA_ARGS -jar $JENKINS_WAR $JENKINS_ARGS" || return 2
}


#
# Verify that all jenkins processes have been shutdown
#
get_running()
{
    return `pgrep -U $JENKINS_USER -f jenkins.war | grep -c .`
}
#
# killall jenkins processes that have not been shutdown
#
force_stop()
{
    get_running
    if [ $? -ne 0 ]; then
        killall -u $JENKINS_USER java daemon || return 3
         # wait for the process to really terminate
         for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
             sleep 1
             get_running && return 0
         done
         return 2
    fi
}

# Get the status of the daemon process
get_daemon_status()
{
    $DAEMON $DAEMON_ARGS --running || return 1
}


#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    get_daemon_status
    case "$?" in
        0)
            $DAEMON $DAEMON_ARGS --stop || return 2
        # wait for the process to really terminate
        for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
            sleep 1
            $DAEMON $DAEMON_ARGS --running || break
        done
        if get_daemon_status; then
                force_stop || return "$?"
        fi
            ;;
        *)
            force_stop || return "$?"
            ;;
    esac

    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return 0
}

# Verify the process did in fact start successfully and didn't just bomb out
do_check_started_ok() {
    sleep 1
    if [ "$1" -ne "0" ]; then return $1; fi
    get_running
    if [ "$?" -eq "0" ]; then
        return 2
    else
        return 0
    fi
}
case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    START_STATUS="$?"
    do_check_started_ok "$START_STATUS"
    case "$?" in
        0|1) log_end_msg 0 ;;
        2) log_end_msg 1 ; exit 7 ;;
    esac
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
        0|1) log_end_msg 0 ;;
        2|3) log_end_msg 1 ; exit 100 ;;
    esac
    ;;
  restart|force-reload)
    #
    # If the "reload" option is implemented then remove the
    # 'force-reload' alias
    #
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        do_start
        START_STATUS="$?"
        sleep 1
        do_check_started_ok "$START_STATUS"
        case "$?" in
          0) log_end_msg 0 ;;
          1) log_end_msg 1 ; exit 100 ;; # Old process is still running
          *) log_end_msg 1 ; exit 100 ;; # Failed to start
        esac
        ;;
      *)
        # Failed to stop
        log_end_msg 1
        ;;
    esac
    ;;
  status)
        get_daemon_status
        case "$?" in
         0)
                echo "$DESC is running with the pid `cat $PIDFILE`"
                rc=0
                ;;
        *)
                get_running
                procs=$?
                if [ $procs -eq 0 ]; then
                        echo -n "$DESC is not running"
                        if [ -f $PIDFILE ]; then
                                echo ", but the pidfile ($PIDFILE) still exists"
                                rc=1
                        else
                                echo
                                rc=3
                        fi

                else
                        echo "$procs instances of jenkins are running at the moment"
                        echo "but the pidfile $PIDFILE is missing"
                        rc=0
                fi

                exit $rc
                ;;
        esac
        ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
    ;;
esac

exit 0

 

最后一步,修改/opt/jenkins目录所有者授权:

# userdel -rf jenkins && useradd -g root -m jenkins

# chown -R jenkins:root /opt/jenkins

否则jenkins启动不成功

# systemctl enable jenkins.service

# systemctl start jenkins.service

最后,启动jenkins成功,日志显示如下:

Running from: /opt/jenkins/lib/jenkins.war
2021-04-23 02:35:43.408+0000 [id=1]     INFO    org.eclipse.jetty.util.log.Log#initialized: Logging initialized @832ms to org.eclipse.jetty.util.log.JavaUti
lLog
2021-04-23 02:35:43.674+0000 [id=1]     INFO    winstone.Logger#logInternal: Beginning extraction from war file
2021-04-23 02:35:45.513+0000 [id=1]     WARNING o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2021-04-23 02:35:45.599+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: jetty-9.4.39.v20210325; built: 2021-03-25T14:42:11.471Z; git: 9fc7c
a5a922f2a37b84ec9dbc26a5168cee7e667; jvm 1.8.0_261-b12
2021-04-23 02:35:46.093+0000 [id=1]     INFO    o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.J
ettyJspServlet
2021-04-23 02:35:46.176+0000 [id=1]     INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0
2021-04-23 02:35:46.177+0000 [id=1]     INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: No SessionScavenger set, using defaults
2021-04-23 02:35:46.179+0000 [id=1]     INFO    o.e.j.server.session.HouseKeeper#startScavenging: node0 Scavenging every 660000ms
2021-04-23 02:35:46.903+0000 [id=1]     INFO    hudson.WebAppMain#contextInitialized: Jenkins home directory: /opt/jenkins found at: EnvVars.masterEnvVars.g
et("JENKINS_HOME")
2021-04-23 02:35:47.143+0000 [id=1]     INFO    o.e.j.s.handler.ContextHandler#doStart: Started w.@66d23e4a{Jenkins v2.277.3,/,file:///opt/jenkins/cache/war
/,AVAILABLE}{/opt/jenkins/cache/war}
2021-04-23 02:35:47.183+0000 [id=1]     INFO    o.e.j.server.AbstractConnector#doStart: Started ServerConnector@d2de489{HTTP/1.1, (http/1.1)}{0.0.0.0:5070}
2021-04-23 02:35:47.184+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: Started @4609ms
2021-04-23 02:35:47.185+0000 [id=22]    INFO    winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled
2021-04-23 02:35:48.945+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Started initialization
2021-04-23 02:35:48.989+0000 [id=31]    INFO    jenkins.InitReactorRunner$1#onAttained: Listed all plugins
2021-04-23 02:35:51.353+0000 [id=27]    INFO    jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
2021-04-23 02:35:51.363+0000 [id=34]    INFO    jenkins.InitReactorRunner$1#onAttained: Started all plugins
2021-04-23 02:35:51.372+0000 [id=31]    INFO    jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
2021-04-23 02:35:52.548+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: System config loaded
2021-04-23 02:35:52.549+0000 [id=32]    INFO    jenkins.InitReactorRunner$1#onAttained: System config adapted
2021-04-23 02:35:52.550+0000 [id=32]    INFO    jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
2021-04-23 02:35:52.553+0000 [id=28]    INFO    jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
2021-04-23 02:35:52.594+0000 [id=47]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$0: Started Download metadata
2021-04-23 02:35:52.612+0000 [id=47]    INFO    hudson.util.Retrier#start: Attempt #1 to do the action check updates server
2021-04-23 02:35:53.125+0000 [id=31]    INFO    jenkins.install.SetupWizard#init:

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

aedf0b397f184f33a79ae7a88e3f1232

This may also be found at: /opt/jenkins/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

2021-04-23 02:36:11.447+0000 [id=32]    INFO    jenkins.InitReactorRunner$1#onAttained: Completed initialization
2021-04-23 02:36:11.525+0000 [id=21]    INFO    hudson.WebAppMain$3#run: Jenkins is fully up and running
2021-04-23 02:36:13.227+0000 [id=47]    INFO    h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
2021-04-23 02:36:13.228+0000 [id=47]    INFO    hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
2021-04-23 02:36:13.231+0000 [id=47]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$0: Finished Download metadata. 20,636 ms

浏览器打开:http://localhost:5070 进行初始化和安装配置

这样就可以实现将/opt/jenkins目录打包方便移植

你可能感兴趣的:(jenkins)