Jenkins部署后端服务

准备环境

1、安装容器环境:docker和docker-compose
2、安装Jenkins依赖的组件:maven、jdk、git

安装Jenkins

1、docker pull jenkins/jenkins:2.414.3-lts-jdk17
2、创建文件 docker-compose.yml内容如下:

version: '3.5'
services:
  jenkins:
    restart: always
    image: jenkins/jenkins:2.414.3-lts-jdk17
    container_name: jenkins
    ports:
      # 发布端口
      - 9095:8080
    environment:
      TZ: Asia/Shanghai
    volumes:
      - /home/jenkins_home:/var/jenkins_home
      - /usr/local/apache-maven-3.9.7:/var/maven
      - /usr/local/java/jdk-17.0.11:/var/jdk
      - /usr/bin/git:/var/git

常用命令

Docker常用命令:
docker images:列出本地主机上所有的镜像。
docker search jenkins:搜索远程仓库中的镜像。
docker pull:从远程仓库下载镜像到本地。
docker ps:列出正在运行的容器。
docker stop:停止容器。
docker rm:删除容器。

Docker Compose常用命令:
docker-compose -f docker-compose.yml up -d:启动服务,后面可以带名称指定如:Jenkins。ps:-f 指定文件
docker-compose restart jenkins:Jenkins服务重启
docker-compose up --detach --build jenkins:Jenkins服务重启
docker logs -f jenkins:查看服务容器的输出日志。

Jenkins安装插件

Locale plugin:汉化
Config File Provider Plugin:支持配置全局文件,配置maven的setting文件
Pipeline Maven Integration Plugin:流水线方式maven打包需要
SSH Pipeline Steps:流水线方式ssh登录服务器需要

全局工具配置&配置tools

需要安装插件Config File Provider Plugin,按需使用,安装maven的时候也可以修改好配置文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

新建任务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
脚本

def lastCommitMessage
def projectDirectory = 'my-server'
def projectJarPackageName = 'my-server'
def projectWorksName = 'my-server'
def projectRemoteGitUrl = 'https://code,.'
def buildBranch = 'dev'
def remote = [:]
remote.name = '测试服务器'
remote.host = '192.168.0.0'
remote.user = 'root'
remote.password = '123456'
remote.allowAnyHosts = true

pipeline {
    agent any

    stages {
        stage('拉取代码') {
            steps {
                git branch: "${buildBranch}", credentialsId: 'zy', url: "${projectRemoteGitUrl}"
                echo pwd()
            }
        }
        stage('获取最后一次提交的消息') {
            steps {            
                script {
                    lastCommitMessage = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
                    echo "Last Commit Message: ${lastCommitMessage}"
                }
            }
        }
        stage('构建') {
            steps {
                withMaven(globalMavenSettingsConfig: '', jdk: 'jdk17', maven: 'maven3.9.7') {
                    sh "java -version"
                    sh 'mvn clean install -Dmaven.test.skip=true'
                }
                echo "环境变量信息:项目名${projectWorksName}"
            }
        }
        stage('发布到测试服务器') {
            steps {
                sshCommand remote: remote, command: "rm -rf /root/application/${projectDirectory}/${projectJarPackageName}.jar"
                dir("./${projectWorksName}/target/") {
                  sshCommand remote: remote, command: "mv /home/jenkins_home/workspace/my-service/${projectDirectory}/target/${projectJarPackageName}.jar /root/application/${projectDirectory}/"
                }
                sshCommand remote: remote, command: "/root/application/${projectDirectory}/app.sh restart"
            }
        }
    }
}

credentialsId: 'zy’需要配置凭证
在这里插入图片描述
app.sh

#!/bin/bash

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

APP_NAME=my-server
PROJECT_PATH=/root/application/my-server/
LOGDIR="${PROJECT_PATH}logs"

start() {
	if [ ! -d "$LOGDIR" ]; then
		mkdir -p "$LOGDIR"
	fi
	# rm -f tpid
	BUILD_ID=dontKillMe nohup /usr/local/java/jdk-17.0.11/bin/java -Dfile.encoding=UTF-8 -Dmq.customer=true -Dspring.profiles.active=dev -jar -Xms512m -Xmx1024m -XX:MaxDirectMemorySize=256m ${PROJECT_PATH}${APP_NAME}.jar > ${PROJECT_PATH}logs/info.log 2>&1 &
	# echo $! > tpid
	echo Start Success!
}

stop() {
	# tpid=`cat ${PROJECT_PATH}tpid | awk '{print $1}'`
	# tpid=`ps -aef | grep $tpid | awk '{print $2}' | grep $tpid`
    tpid=$(ps -ef | grep $PROJECT_PATH$APP_NAME | grep -v "grep" | awk '{print $2}')

	echo $tpid
	if [ ${tpid} ]; then
		kill $tpid
		echo "tpid ${tpid} killed"
	fi

	for variable in {1..5}
	do
		sleep 1
		echo "perform the [$variable] inspection ..."
		tpid=`ps -ef|grep $APP_NAME | grep -v grep | grep -v kill | awk '{print $2}'`
		if [ ${tpid} ]; then
			echo "check that the program is running, try to stop it for the [$variable]^_^!"
			echo "stop execute success!"
		else
			echo "check that the program is not running the script exit() bye!"
			break;
		fi
	done

	tpid=`ps -ef|grep $APP_NAME | grep -v grep | grep -v kill | awk '{print $2}'`
	if [ ${tpid} ]; then
		echo "that the program fails to stop repeatedly"
		echo "if you need to force stop, please use the command [kill -9 ${tpid}]"
		 kill -9 $tpid
	fi
}

restart() {
	stop
	start
	exit 0
}

status() {
	tpid=`cat ${PROJECT_PATH}tpid | awk '{print $1}'`
	tpid=`ps -aef | grep $tpid | awk '{print $2}' | grep $tpid`
	echo $tpid
	if [ ${tpid} ]; then
		echo "$APP_NAME is Running. PID: $tpid"
	fi
	tpid=`ps -ef|grep $APP_NAME | grep -v grep | grep -v kill | awk '{print $2}'`
	if [ ${tpid} ]; then
		echo "DBlCheck: $APP_NAME is running. PID: $tpid"
	else
		echo "$APP_NAME is not running"
	fi
	exit 1
}

case "$1" in
	'start')
		start
		;;
	'stop')
		stop
		;;
	'restart')
		restart
		;;
	'status')
		status
		;;
	*)
		echo "app.sh {start | stop | status | restart}"
		exit 1
		;;
esac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值