(一)部署jenkins(openjdk11,17)
git免密
javaopenjdk下载
javaopenjdk下载2
Jenkins官方网站下载最新的war包上传到服务器。
tomcat下载地址
wget -c https://get.jenkins.io/war-stable/2.375.3/jenkins.war --no-check-certificate
yum -y install java-11-openjdk
mkdir /usr/local/jenkins
mv jenkins.war /usr/local/jenkins
mkdir -p /data/logs/jenkins/
vim /usr/lib/systemd/system/jenkins.service
java -jar jenkins.war --ajp13Port=-1 --httpPort=8080。这个–ajp13Port=-1选项有点问题。这里使用的是http连接,去掉就行。就不排查了。
[Unit]
Description=Jenkins server daemon
After=network.target
[Service]
Type=simple
User=root
Group=root
Environment="JAVA_HOME=xxxxx"
ExecStart=/bin/bash -c "exec /usr/bin/java -jar -Xms256m -Xmx1024m -jar jenkins.war --httpPort=-1 --httpsPort=7070 --httpsKeyStore=/jenkins.jks --httpsKeyStorePassword=123456 >>jenkins.log 2>&1"
ExecReload=/bin/kill -HUP $MAINPID
#HUP重新加载进程
ExecStop=/bin/kill -SIGINT $MAINPID
#SIGINT终止进程(Ctrl-C)
KillMode=process
#杀死主进程,不影响运行中的子进程
#Restart=always
#不管什么状态,都重启
Restart=on-failure
#则当进程以非零退出代码退出,由信号终止,当操作(例如服务重新加载)超时,以及何时触发配置的监视程序超时时,将重新启动服务。
RestartSec=42s
[Install]
WantedBy=multi-user.target
将SSL密钥转换为PKCS12格式
openssl pkcs12 -export -out jenkins.p12 -inkey private.key -passout 'pass:123456' -in private.crt -certfile CA-certificate.crt -name 8.134.152.142
将PKCS12转换为JKS格式
keytool -importkeystore -srckeystore jenkins.p12 -srcstorepass '123456' -srcstoretype PKCS12 -srcalias 8.134.152.142 -deststoretype JKS -destkeystore jenkins.jks -deststorepass '123456' -destalias 8.134.152.142
证书转换参考
启动参数参考如下,将参数直接定义在启动参数中:
nohup java -Dcom.sun.akuma.Daemon=daemonized \
-Djava.awt.headless=true \
-jar jenkins.war \
--httpPort=8080 \
--httpsPort=8443 \
--httpsKeyStore=/Users/zeyang/jenkins/jenkins.jks \
--httpsKeyStorePassword=123456 \
--httpsListenAddress=0.0.0.0 \
--debug=9 \
--handlerCountMax=100 \
--handlerCountMaxIdle=20 \
--accessLoggerClassName=winstone.accesslog.SimpleAccessLogger \
--simpleAccessLogger.format=combined &
systemctl enable --now jenkins
systemctl status jenkins
访问:ip:port。验证之后安装系统推荐模块。
(二)使用jenkins自动化构建
active choices parameter: 提供下拉参数构建选项,一级二级下拉。
Extended Choice Parameters:参数化构建过程插件(重要)
Extended Read Permission Plugin:可读项目配置信息,无法修改项目配置信息权限
![在这里插入图片描述](https://img-blog.csdnimg.cn/5c1ce764a2c8477ca53a95604174f043.png
环境变量及工具安装很重要。
本章展示的为本地构建方式。
安装ansible
yum -y install ansible
编写hosts文件
cp /etc/ansible/hosts /etc/ansible/hosts-bak
cat >/etc/ansible/hosts<<-EOF
[jenkins]
172.16.0.1
[all:vars]
ansible_ssh_private_key_file=/xxx/xxx.pem
ansible_ssh_port=22
ansible_ssh_user=root
EOF
jenkins创建项目初始化生成任务(xxx.service),供参考
Groovy Script:file=[‘bash’,‘-c’,“ls ${dir}”].execute().text.readLines()
参考文档Active Choice plug-in
#!/bin/bash
PTDIR="/data/projects/"
LOGSDIR="/data/logs/"
CONFDIR="/data/confs/"
#DAEMCENT="""[Unit]
Create_PtDir() {
if [ -d ${PTDIR}${PTNAME} ];then
echo "项目目录${PTDIR}${PTNAME}已存在>>>>"
else
mkdir -p ${PTDIR}${PTNAME} && echo "项目目录${PTDIR}${PTNAME}创建成功>>>>"
fi
}
Create_PtDm() {
if [ -d ${CONFDIR}${PTNAME} ];then
echo "文件${CONFDIR}${PTNAME}.service已存在>>>"
else
mkdir -p ${CONFDIR}${PTNAME}
cat>${CONFDIR}${PTNAME}/${PTNAME}.service<<-EOF
[Unit]
Description=${PTNAME} server daemon
After=network.target
[Service]
Type=simple
ExecStart=/bin/bash -c "exec /usr/bin/java -jar ${PTDIR}${PTNAME}/${PTNAME}.${PGTYPE} >>${LOGSDIR}${PTNAME}/${PTNAME}.log 2>&1"
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
KillMode=process
#Restart=always
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
EOF
echo -e "启动文件${CONFDIR}${PTNAME}/${PTNAME}.service创建成功>>>" && cat ${CONFDIR}${PTNAME}/${PTNAME}.service
fi
}
List_Pt() {
ls /data/*/
}
Create_Pt() {
Create_PtDir && Create_PtDm
}
Clean_Pt() {
if [ -d ${PTDIR}${PTNAME} ];then
rm -rf /data/*/${PTNAME} && echo "项目${PTNAME}已清理"
else
echo "项目${PTNAME}不存在(注意大小写)"
fi
}
case $Environment in
create)
Create_Pt
;;
clean)
Clean_Pt
;;
ptlist)
List_Pt
;;
*)
echo "选项"create""
;;
esac
创建对应项目deploy、rollback
任务名称和上边创建时候输入的名称一样。
#!/bin/bash
PTDIR="/data/projects/"
PTDIRBAK="${PTDIR}${JOB_NAME}/oldversion"
LOGSDIR="/data/logs/"
CONFDIR="/data/confs/"
SVCFILE="/usr/lib/systemd/system/"
PAGE="${JOB_NAME}.${TYPE}"
echo "部署主机如下>>>"
ansible ${JOB_NAME} --list
echo ">>>>>>>>>>>>>"
Deploy_Mk() {
cd ${PTDIR}${JOB_NAME}
#这里git pull,前边的估计还要改。
mvn clean package
}
Deploy_PtDir() {
ansible ${JOB_NAME} -m shell -a "[ -d ${PTDIR}${JOB_NAME} ] || mkdir -p ${PTDIR}${JOB_NAME}"
ansible ${JOB_NAME} -m shell -a "[ -d ${LOGSDIR}${JOB_NAME} ] || mkdir -p ${LOGSDIR}${JOB_NAME}"
ansible ${JOB_NAME} -m shell -a "[ -d ${PTDIRBAK} ] || mkdir -p ${PTDIRBAK}"
echo ">>>>>>>日志目录${LOGSDIR}${JOB_NAME},项目目录${PTDIR}${JOB_NAME},备份目录${PTDIRBAK}已存在<<<<<<<"
}
Deploy_PtCy() {
cd ${PTDIR}${JOB_NAME}
TIME=`date +%Y%m%d%H%M%S`
ansible ${JOB_NAME} -m copy -a "src=${PTDIR}${JOB_NAME}/${PAGE} dest=${PTDIRBAK}/${PAGE}-${TIME}"
echo "${PTDIRBAK}/${PAGE}-${TIME}"
NEWPAGE="${PAGE}-${TIME}"
echo ">>>>>>>>>>>>>>>>>>>>>>${NEWPAGE}已发送到${JOB_NAME}主机<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
}
Deploy_PtLn() {
ansible ${JOB_NAME} -m shell -a "ln -sf ${PTDIRBAK}/${NEWPAGE} ${PTDIR}${JOB_NAME}/${PAGE}"
ansible ${JOB_NAME} -m shell -a "systemctl restart ${JOB_NAME} && systemctl status ${JOB_NAME}"
echo ">>>>>>>>>>>>>>>>>>>>项目${JOB_NAME}已重启<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
}
Deploy_PtDf() {
ansible ${JOB_NAME} -m shell -a "[ -f ${SVCFILE}${JOB_NAME}.service ]"
if [ $? -eq 0 ];then
echo -e ">>>>>>>>>>>>>>>>>>>>>>>service文件已存在<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
ansible ${JOB_NAME} -m shell -a "systemctl status ${JOB_NAME}"
echo ">>>>>>>>>>>>>>>>>>>>项目${JOB_NAME}当前版本<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
ansible ${JOB_NAME} -m shell -a "ls -l ${PTDIR}${JOB_NAME}/${PAGE}"
else
ansible ${JOB_NAME} -m copy -a "src=${CONFDIR}${JOB_NAME}/${JOB_NAME}.service dest=${SVCFILE}${JOB_NAME}.service"
ansible ${JOB_NAME} -m shell -a "systemctl daemon-reload && systemctl enable --now ${JOB_NAME} && systemctl status ${JOB_NAME}"
fi
}
Rollback_List() {
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>当前版本<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
ansible ${JOB_NAME} -m shell -a "ls -l ${PTDIR}${JOB_NAME}/${PAGE}"
echo "历史版本如下,请选择要回滚的版本>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
ansible ${JOB_NAME} -m shell -a "ls ${PTDIRBAK}"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
}
Rollback_Pt() {
ansible ${JOB_NAME} -m shell -a "ln -sf ${PTDIRBAK}/${VERSION} ${PTDIR}${JOB_NAME}/${PAGE}"
ansible ${JOB_NAME} -m shell -a "systemctl restart ${JOB_NAME} && systemctl status ${JOB_NAME}"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>回滚成功,当前版本<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
ansible ${JOB_NAME} -m shell -a "ls -l ${PTDIR}${JOB_NAME}/${PAGE}"
echo -e ">>>>>>>>>>>>>>>>>>>>>>期望版本>>>\n${VERSION}<<<<<<<<<<<<<<<<<<<<<<<<"
}
case $Environment in
deploy)
Deploy_Mk && Deploy_PtDir && Deploy_PtCy && Deploy_PtLn && Deploy_PtDf
;;
listversion)
Rollback_List
;;
rollback)
Rollback_Pt
;;
esac
FROM openjdk:8
RUN mkdir /opt/demo
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN /bin/echo 'Asia/Shanghai' >/etc/timezone
WORKDIR /opt/demo
EXPOSE ${PORT}
ADD *.jar ./${JECT}.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "-Xmx1024M", "-Xms256M", "${JECT}.jar", "--server.port=${PORT}"]
docker build -t ${DOCKERREPO}/${ENV}/${i}:${TIME} .
docker run -d -p ${PORT}:${PORT} -v ${LOGDIR}:${CTNLOGDIR} --restart=always --name ${i} ${DOCKERREPO}/${ENV}/${i}:${TIME}
到此结束。项目脚本还需要修改,就是说源码怎么pull在哪打包。这里是用已有的包测试了下功能。
参考文档
Jenkins——多环境部署项目
Jenkins的部署和使用
这篇也值得看看:
Jenkins自动化部署