|
|
|
@@ -10,7 +10,7 @@ show_help() { |
|
|
|
echo "Usage: $0 [-s service] [-e environment]" |
|
|
|
echo |
|
|
|
echo "Options:" |
|
|
|
echo " -s Service to deploy (manage-front, manage, front, all ,default: manage-front)" |
|
|
|
echo " -s Service to deploy (manage-front, manage, front, all default: manage-front)" |
|
|
|
echo " -e Environment (e.g., dev, test, default: dev)" |
|
|
|
echo " -h Show this help message" |
|
|
|
} |
|
|
|
@@ -25,7 +25,8 @@ while getopts "s:e:h" opt; do |
|
|
|
esac |
|
|
|
done |
|
|
|
|
|
|
|
echo "deploy service: $service, env: $env" |
|
|
|
echo "Deploy service: $service, environment: $env" |
|
|
|
|
|
|
|
# 根据环境设置 IP 地址 |
|
|
|
if [ "$env" == "dev" ]; then |
|
|
|
remote_ip="172.20.32.181" |
|
|
|
@@ -37,102 +38,108 @@ else |
|
|
|
fi |
|
|
|
|
|
|
|
baseDir=/home/somuns/ci4s |
|
|
|
cd ${baseDir}/k8s/dockerfiles |
|
|
|
mkdir -p ${baseDir}/k8s/template-yaml/deploy |
|
|
|
|
|
|
|
tag=$(date +'%Y%m%d%H%M') |
|
|
|
managent=172.20.32.187/ci4s/managent:${tag} |
|
|
|
front=172.20.32.187/ci4s/ci4s-front:${tag} |
|
|
|
|
|
|
|
remote_deploy_dir=/home/deploy/manage-platform |
|
|
|
manager_yaml=k8s-7management.yaml |
|
|
|
front_yaml=k8s-12front.yaml |
|
|
|
|
|
|
|
# 构建 manage 镜像 |
|
|
|
if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then |
|
|
|
# 构建镜像函数 |
|
|
|
build_image() { |
|
|
|
local dockerfile=$1 |
|
|
|
local image=$2 |
|
|
|
cd ${baseDir}/k8s/dockerfiles |
|
|
|
docker build -t ${managent} -f managent-dockerfile . |
|
|
|
if [ "$?" -ne "0" ]; then |
|
|
|
echo "build ${managent} image fail" |
|
|
|
docker build -t ${image} -f ${dockerfile} . |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Build ${image} image fail" |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
docker push ${image} |
|
|
|
} |
|
|
|
|
|
|
|
# 复制和替换 YAML 文件函数 |
|
|
|
prepare_yaml() { |
|
|
|
local yaml_file=$1 |
|
|
|
local image=$2 |
|
|
|
|
|
|
|
placeholder="\${${yaml_file%.yaml}-image}" |
|
|
|
cd ${baseDir}/k8s/template-yaml |
|
|
|
# 复制 YAML 文件 |
|
|
|
cp -rf ${manager_yaml} deploy/ |
|
|
|
# 镜像替换 |
|
|
|
cp -rf ${yaml_file} deploy/ |
|
|
|
cd deploy/ |
|
|
|
placeholder="\${k8s-7management-image}" |
|
|
|
#sed -i "s#managenent-image#${managent}#g" ${manager_yaml} |
|
|
|
sed -i "s#$placeholder#${managent}#g" ${manager_yaml} |
|
|
|
if [ "$?" -ne "0" ];then |
|
|
|
echo "replace ${managent} image fail" |
|
|
|
exit 3 |
|
|
|
sed -i "s|${placeholder}|${image}|g" ${yaml_file} |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Replace ${image} image fail" |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
# 推送镜像 |
|
|
|
docker push ${managent} |
|
|
|
# 部署服务 |
|
|
|
ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${manager_yaml} ]; then mv ${remote_deploy_dir}/${manager_yaml} ${remote_deploy_dir}/k8s-7management.yaml.bak; fi" |
|
|
|
scp ${baseDir}/k8s/template-yaml/deploy/${manager_yaml} root@$remote_ip:${remote_deploy_dir}/${manager_yaml} |
|
|
|
|
|
|
|
# 建立远程目录并备份文件 |
|
|
|
ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${yaml_file} ]; then mv ${remote_deploy_dir}/${yaml_file} ${remote_deploy_dir}/${yaml_file}.bak; fi" |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Failed to copy ${managent} yaml file" |
|
|
|
echo "Failed to create remote directory or backup ${yaml_file}" |
|
|
|
exit 1 |
|
|
|
else |
|
|
|
echo "Successfully copied ${managent} yaml file" |
|
|
|
echo "Successfully created remote directory and backup ${yaml_file}" |
|
|
|
fi |
|
|
|
ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${manager_yaml}" |
|
|
|
|
|
|
|
scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file} |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Failed to deploy ${managent} image" |
|
|
|
echo "Failed to copy ${yaml_file}" |
|
|
|
exit 1 |
|
|
|
else |
|
|
|
echo "Successfully deployed ${managent} image" |
|
|
|
echo "Successfully copied ${yaml_file}" |
|
|
|
fi |
|
|
|
fi |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
# 构建 front 镜像 |
|
|
|
if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then |
|
|
|
cd ${baseDir}/k8s/dockerfiles |
|
|
|
docker build -t ${front} -f nginx-dockerfile . |
|
|
|
if [ "$?" -ne "0" ];then |
|
|
|
echo "build ${front} image fail" |
|
|
|
exit 2 |
|
|
|
# 部署服务函数 |
|
|
|
deploy_service() { |
|
|
|
local yaml_file=$1 |
|
|
|
ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}" |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Failed to deploy ${yaml_file}" |
|
|
|
exit 1 |
|
|
|
else |
|
|
|
echo "Successfully deployed ${yaml_file}" |
|
|
|
fi |
|
|
|
} |
|
|
|
|
|
|
|
# 复制 YAML 文件 |
|
|
|
cd ${baseDir}/k8s/template-yaml |
|
|
|
cp -rf ${front_yaml} deploy/ |
|
|
|
deploy_nacos() { |
|
|
|
local yaml_file=$1 |
|
|
|
scp ${baseDir}/k8s/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file} |
|
|
|
deploy_service ${yaml_file} |
|
|
|
} |
|
|
|
|
|
|
|
# 镜像替换 |
|
|
|
cd deploy/ |
|
|
|
placeholder="\${k8s-12front-image}" |
|
|
|
#sed -i "s#front-image#${front}#g" ${front_yaml} |
|
|
|
sed -i "s#$placeholder#${front}#g" ${front_yaml} |
|
|
|
if [ "$?" -ne "0" ];then |
|
|
|
echo "replace ${front} image fail" |
|
|
|
exit 4 |
|
|
|
fi |
|
|
|
build_and_deploy() { |
|
|
|
local dockerfile=$1 |
|
|
|
local image=$2 |
|
|
|
local yaml_file=$3 |
|
|
|
|
|
|
|
build_image ${dockerfile} ${image} |
|
|
|
prepare_yaml ${yaml_file} ${image} |
|
|
|
deploy_service ${yaml_file} |
|
|
|
} |
|
|
|
|
|
|
|
# 构建和部署 manage 服务 |
|
|
|
if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then |
|
|
|
build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml" |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
# 镜像推送 |
|
|
|
docker push ${front} |
|
|
|
# 构建和部署 front 服务 |
|
|
|
if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then |
|
|
|
build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml" |
|
|
|
fi |
|
|
|
|
|
|
|
#部署服务 |
|
|
|
ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${front_yaml} ]; then mv ${remote_deploy_dir}/${front_yaml} ${remote_deploy_dir}/k8s-12front.yaml.bak; fi" |
|
|
|
scp ${baseDir}/k8s/template-yaml/deploy/${front_yaml} root@$remote_ip:${remote_deploy_dir}/${front_yaml} |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Failed to copy ${managent} yaml file" |
|
|
|
exit 1 |
|
|
|
else |
|
|
|
echo "Successfully copied ${managent} yaml file" |
|
|
|
fi |
|
|
|
ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${front_yaml}" |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Failed to deploy ${front} image" |
|
|
|
exit 1 |
|
|
|
else |
|
|
|
echo "Successfully deployed ${front} image" |
|
|
|
fi |
|
|
|
|
|
|
|
if [ "$service" == "all" ]; then |
|
|
|
#部署前端 |
|
|
|
build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml" |
|
|
|
#部署管理平台 |
|
|
|
build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml" |
|
|
|
#部署认证中心 |
|
|
|
build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml" |
|
|
|
#部署网关 |
|
|
|
build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml" |
|
|
|
#部署系统服务 |
|
|
|
build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml" |
|
|
|
#部署配置中心 |
|
|
|
deploy_nacos "k8s-3nacos.yaml" |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
|