Linux : Docker安装Jenkins

1. 安装jenkins: docker run   -u root   -d   -p 9999:8080   -p 50000:50000   -v jenkins-data:/var/jenkins_home   -v /var/run/docker.sock:/var/run/docker.sock   jenkinsci/blueocean

2. 使用初始密码登陆, 初始密码位置: cat /var/jenkins_home/secrets/initialAdminPassword

3. 直接选择推荐插件安装

4. 安装插件:

  4.1. Ant Plugin  用来配置jdk+maven环境的插件

  4.2. LDAP Plugin  用来进行配置角色(用户组)权限的插件

  4.3 Localization: Chinese (Simplified)  中文语言插件 

  4.4 Publish Over SSH  把文件通过ssh发布到服务器的插件  (在系统配置的 Publish over SSH 中配置服务器信息 高级选项卡中配置服务器密码)

  4.5 Simple Theme Plugin  样式修改插件   (在系统配置的 Theme添加css url 如: http://192.168.1.231:9999/userContent/layout/my.css  和   http://192.168.1.231:9999/userContent/layout/title.css)

5. 项目配置:

  5.1 java后端项目: (在docker里面安装java与maven, 并修改maven源) 

    选中丢弃旧的构建

    源码管理添加仓库与用户访问凭证

    指定分支 如 */dev

    添加构建步骤 执行 shell 如      mvn clean && mvn package -DskipTests

    添加步骤 send files or execute commands over ssh    (下图中 Exec command里面 run.sh 前 少了个 / )

    

 

   5.2 前端项目 (在docker里面安装node, 并修改npm源, 安装zip) 

    执行 shell命令: 

npm install &&
npm run build:prod &&
cd dist &&
zip dist.zip * -r

    

 

 

附件:   unzip.sh脚本内容:

#!/usr/bin/env bash
source /etc/profile
cd `dirname $0`
rm -rf dist/
unzip dist.zip -d dist

run.sh脚本内容:

#!/bin/bash
source /etc/profile
# Spring-Boot 常规启动脚本,基于HotSpot Java8
# 使用方式:xx.sh [start|stop|restart|status|dump]
# 将Spring-Boot Jar包和此脚本放在同一目录下,之后配置APP_NAME/PROFILE即可

cd `dirname $0`
# 应用名(boot jar包名)
APP_NAME=demo

# Spring-Boot环境名(profiles)
PROFILE=

JAR_NAME=$APP_NAME\.jar
PID=$APP_NAME\.pid
APP_HOME=`pwd`
LOG_PATH=$APP_HOME/logs
GC_LOG_PATH=$LOG_PATH/gc
DEBUG_FLAG=$2
app_path=$(cd `dirname $0`; pwd)
app_log="${app_path}/app.log"

if [ ! -d $LOG_PATH ]; then
    mkdir $LOG_PATH
fi

if [ ! -d $GC_LOG_PATH ]; then
    mkdir $GC_LOG_PATH
fi

# DUMP父目录
DUMP_DIR=$LOG_PATH/dump
if [ ! -d $DUMP_DIR ]; then
    mkdir $DUMP_DIR
fi

# DUMP目录前缀
DUMP_DATE=`date +%Y%m%d%H%M%S`

# DUMP目录
DATE_DIR=$DUMP_DIR/$DUMP_DATE
if [ ! -d $DATE_DIR ]; then
    mkdir $DATE_DIR
fi


# GC日志参数
GC_LOG_OPTS="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:$GC_LOG_PATH/gc-%t.log"

# OOM Dump内存参数
DUMP_OPTS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_PATH"

# JVM DEBUG参数,用于调试,默认不开启

# ClassLoader和Method Compile日志,用于调试
COMPILE_LOADER_OPTS="-XX:+TraceClassLoading -XX:+TraceClassUnloading -XX:-PrintCompilation"

# 远程调试参数
REMOTE_DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"

# DEBUG参数
DEBUG_OPTS="$COMPILE_LOADER_OPTS $REMOTE_DEBUG_OPTS"

# 至于Garbage Collector,虽然Java8已经支持G1了,但是不一定必须用,CMS在默认场景下也是一个优秀的回收器
GC_OPTS="-XX:+UseConcMarkSweepGC"

OTHER_OPTS="-Djava.security.egd=file:/dev/./urandom"

# JVM 启动参数,如无特殊需求,推荐只配置堆+元空间
JVM_OPTIONS="-server -Xms2g -Xmx2g -XX:MetaspaceSize=256m $GC_OPTS $GC_LOG_OPTS $DUMP_OPTS $OTHER_OPTS"

#使用说明,用来提示输入参数
usage() {
    echo "Usage: sh [run_script].sh [start|stop|restart|status|dump]"
    exit 1
}

#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $APP_HOME/$JAR_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 "--- ${JAR_NAME} is already running PID=${pid} ---" 
  else 
      if [ "$DEBUG_FLAG" = "debug" ]; then
        JVM_OPTIONS="$JVM_OPTIONS $DEBUG_OPTS"
        echo -e "\033[33m Warning: currently running in debug mode! This mode enables remote debugging, printing, compiling, and other information \033[0m"
    fi
      echo "JVM_OPTIONS : "
      echo "$JVM_OPTIONS"
    nohup java -jar $JVM_OPTIONS -Dspring.profiles.active=$PROFILE $APP_HOME/$JAR_NAME >${app_log} 2>&1 &
    echo $! > $PID
    echo "--- start $JAR_NAME successed PID=$! ---" 
    #tail -f ${app_log}
   fi
  }


stop(){
  #is_exist
  pidf=$(cat $PID)
  #echo "$pidf"  
  echo "--- app PID = $pidf begin kill $pidf ---"
  kill $pidf
  rm -rf $PID
  sleep 2
  is_exist
  if [ $? -eq "0" ]; then 
    echo "--- app 2 PID = $pid begin kill -9 $pid  ---"
    kill -9  $pid
    sleep 2
    echo "--- $JAR_NAME process stopped ---"  
  else
    echo "--- ${JAR_NAME} is not running ---"
  fi  
}


status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "--- ${JAR_NAME} is running PID is ${pid} ---"
  else
    echo "--- ${JAR_NAME} is not running ---"
  fi
}

dump(){
  is_exist
  if [ $? -eq "0" ]; then 
    echo -e "Dumping the $JAR_NAME ...\c"
    do_dump
  else 
    echo "--- ${JAR_NAME} is not running ---"
   fi
 }

#重启
restart(){
  stop
  start
}

do_dump(){
    jmap -dump:format=b,file=$DATE_DIR/jmap-dump-$pid.hprof $pid 2>&1
    echo -e ".\c"
    jstack $pid > $DATE_DIR/jstack-$pid.dump 2>&1
    echo -e ".\c"
    jinfo $pid > $DATE_DIR/jinfo-$pid.dump 2>&1
    echo -e ".\c"
    jstat -gcutil $pid > $DATE_DIR/jstat-gcutil-$pid.dump 2>&1
    echo -e ".\c"
    jstat -gccapacity $pid > $DATE_DIR/jstat-gccapacity-$pid.dump 2>&1
    echo -e ".\c"
    jmap $pid > $DATE_DIR/jmap-$pid.dump 2>&1
    echo -e ".\c"
    jmap -heap $pid > $DATE_DIR/jmap-heap-$pid.dump 2>&1
    echo -e ".\c"
    jmap -histo $pid > $DATE_DIR/jmap-histo-$pid.dump 2>&1
    echo -e ".\c"
    jmap -dump:format=b,file=jmap-dump-$pid.bin $pid
    echo -e ".\c"
    if [ -r /usr/sbin/lsof ]; then
    /usr/sbin/lsof -p $pid > $DATE_DIR/lsof-$pid.dump
    echo -e ".\c"
    fi

    if [ -r /bin/netstat ]; then
    /bin/netstat -an > $DATE_DIR/netstat.dump 2>&1
    echo -e ".\c"
    fi
    if [ -r /usr/bin/iostat ]; then
    /usr/bin/iostat > $DATE_DIR/iostat.dump 2>&1
    echo -e ".\c"
    fi
    if [ -r /usr/bin/mpstat ]; then
    /usr/bin/mpstat > $DATE_DIR/mpstat.dump 2>&1
    echo -e ".\c"
    fi
    if [ -r /usr/bin/vmstat ]; then
    /usr/bin/vmstat > $DATE_DIR/vmstat.dump 2>&1
    echo -e ".\c"
    fi
    if [ -r /usr/bin/free ]; then
    /usr/bin/free -t > $DATE_DIR/free.dump 2>&1
    echo -e ".\c"
    fi
    if [ -r /usr/bin/sar ]; then
    /usr/bin/sar > $DATE_DIR/sar.dump 2>&1
    echo -e ".\c"
    fi
    if [ -r /usr/bin/uptime ]; then
    /usr/bin/uptime > $DATE_DIR/uptime.dump 2>&1
    echo -e ".\c"
    fi

    echo "OK!"
    echo "DUMP: $DATE_DIR"
}

case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  "dump")
    dump
  ;;
  *)
    usage
    ;;
esac
exit 0

 

posted @ 2020-10-29 11:32  cccy0  阅读(623)  评论(0编辑  收藏  举报