1.创建maven项目
2.配置git、maven
3.阿里控制台>容器镜像服务>镜像仓库>创建镜像仓库
4.执行shell脚本(推送镜像到阿里云镜像仓库)
使用到登录阿里云仓库命令
#!/bin/bash
# 服务名称
SERVER_NAME=planetflix-app
# 镜像tag
IMAGE_TAG=1.0.0-SNAPSHOT
# 镜像名称
IMAGE_NAME=$SERVER_NAME:$IMAGE_TAG
# jenkins下的目录
JENKINS_HOME=/var/jenkins_home/workspace/push116_planetflix_images/planetflix-api
cd $JENKINS_HOME
echo "检查docker能不能用"
docker -v
echo "------ 开始构建镜像:${SERVER_NAME} ------"
docker build -t ${IMAGE_NAME} -f Dockerfile . --no-cache
echo "------ 镜像构建结束:${IMAGE_NAME} ------"
if [[ -n $(docker ps -q -f "name=^${SERVER_NAME}$") ]];then
echo "------ 容器正在运行:${SERVER_NAME} ------"
echo "------ 停止容器:$SERVER_NAME ------"
docker stop $SERVER_NAME
echo "------ 删除容器:$SERVER_NAME ------"
docker rm $SERVER_NAME
else
echo "------ 容器未在运行:${SERVER_NAME} ------"
echo "------ 删除容器:$SERVER_NAME ------"
docker rm $SERVER_NAME
fi
echo "登录阿里云镜像仓库"
docker login --username=xxx --password=xxx registry.cn-hongkong.aliyuncs.com
echo "建立映射中..."
docker tag $IMAGE_NAME registry.cn-hongkong.aliyuncs.com/yygm/devops_planetflix_app_test:$SERVER_NAME-$IMAGE_TAG
echo "镜像推送至阿里云仓库中..."
docker push registry.cn-hongkong.aliyuncs.com/yygm/devops_planetflix_app_test:$SERVER_NAME-$IMAGE_TAG
echo "------ 清理虚悬镜像 ------"
if [[ -n $(docker images | grep "none" | awk '{print $3}') ]];then
docker rmi -f $(docker images | grep "none" | awk '{print $3}')
fi
5.创建Pipline流水线,配置流水线脚本命令(推送部署脚本到远程服务器)
1.作为上面maven项目的钩子执行
2.pipline流水线脚本
env.fileName = "devops116.sh"
env.serverName = "planetflix_app"
env.deployePath = ""
env.sourceFile = "/var/jenkins_home/workspace/shell/devops116.sh"
env.targetPath = "/data/project/java"
pipeline{
agent any
//定义流水线的加工流程
stages {
stage('推送部署脚本到海外影视app服务器') {
steps {
sh """
cp -f ${env.sourceFile} ./
"""
sshPublisher(publishers: [sshPublisherDesc(configName: "${env.serverName}", transfers: [sshTransfer(cleanRemote: false, excludes: '',
execCommand: 'echo "成功推送deploye.sh脚本文件..."',
execTimeout: 120000, flatten: false,
makeEmptyDirs: false, noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: "${env.deployePath}",
remoteDirectorySDF: false, removePrefix: '',
sourceFiles: "${env.fileName}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '推送成功'
}
}
stage('运行部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: "${env.serverName}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand:
"sh ${env.targetPath}/deploye_run.sh", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo "部署完成"
}
}
}
}
3.plpline远程运行部署的deploye_run.sh脚本内容
#!/bin/bash
# 服务名称
SERVER_NAME=prod-shortplay-web
# 镜像tag
IMAGE_TAG=1.0.0-SNAPSHOT
# 镜像名称
IMAGE_NAME=$SERVER_NAME:$IMAGE_TAG
echo "登录阿里云镜像仓库"
docker login --username=xxx--password=xxx registry.cn-hongkong.aliyuncs.com
docker pull registry.cn-hongkong.aliyuncs.com/yygm/private_deploye_ku:$SERVER_NAME-$IMAGE_TAG
if [[ -n $(docker ps -q -f "name=^${SERVER_NAME}$") ]];then
echo "------ 容器正在运行:${SERVER_NAME} ------"
echo "------ 停止容器:$SERVER_NAME ------"
docker stop $SERVER_NAME
echo "------ 删除容器:$SERVER_NAME ------"
docker rm $SERVER_NAME
else
echo "------ 容器未在运行:${SERVER_NAME} ------"
echo "------ 删除容器:$SERVER_NAME ------"
docker rm $SERVER_NAME
fi
echo "------ 开始运行容器:$SERVER_NAME ------"
# 运行镜像,这里的8077是项目服务端口
docker run -t --network=compose_mid_network -v /data/project/java/logs:/data/project/java/logs -d -p 8077:8077 --name $SERVER_NAME registry.cn-hongkong.aliyuncs.com/yygm/private_deploye_ku:$SERVER_NAME-$IMAGE_TAG
6.启动并查看服务
使用docker image查看镜像是否生成
使用docker ps 查看服务是否启动
查看日志docker logs -f -n 500 容器名