Skip to content

Commit d0de025

Browse files
author
喜欢葡萄+芝士的妹妹
committed
feat(scripts): 脚本优化,项目结构调整
1 parent e2aeb98 commit d0de025

File tree

5 files changed

+181
-134
lines changed

5 files changed

+181
-134
lines changed

docker-compose.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: '2'
2+
services:
3+
### JavaScriptCollection服务,ip范围 172.30.0.100
4+
JavaScriptCollection:
5+
image: registry.cn-hangzhou.aliyuncs.com/142vip/cs_:latest
6+
container_name: JavaScriptCollection
7+
hostname: JavaScriptCollection
8+
ports:
9+
- "7100:80"
10+
networks:
11+
net:
12+
ipv4_address: 172.30.0.100
13+
14+
## 创建桥接网络
15+
networks:
16+
## 桥接网络名称,配合文件前缀,最后为dev_test_env_net 参考:https://www.jianshu.com/p/d70c61d45364
17+
net:
18+
driver: bridge
19+
# external: true
20+
ipam:
21+
driver: default
22+
config:
23+
- subnet: 172.30.0.0/24
24+
## 网关
25+
gateway: 172.30.0.1

scripts/build_image.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## 参考:https://blog.csdn.net/Dontla/article/details/125210694
44
## 作者:Rong姐姐好可爱
55
## 使用示例:bash xxx.sh 容器名称 版本号
6-
## - bash build_image.sh oauth_login 0.0.1
6+
## - bash build_image.sh 0.0. x
77
##
88

99

@@ -13,8 +13,9 @@ readonly errorLogger="\033[1;31m"
1313
readonly warnLogger="\033[1;33m"
1414
## 定义时间
1515
readonly currentTime=$(date "+%Y-%m-%d %H:%M:%S")
16+
readonly repoAddress="registry.cn-hangzhou.aliyuncs.com/142vip/doc_book"
1617
## 项目名称
17-
readonly projectName="JavascriptCollection"
18+
readonly projectName="JavaScriptCollection"
1819
## 版本号
1920
version=${1}
2021

@@ -28,24 +29,24 @@ prepare_check(){
2829

2930
run(){
3031
echo -e "${successLogger}---------------- shell doc_book start ---------------- "
31-
docker build -t registry.cn-hangzhou.aliyuncs.com/142vip/doc_book:"${projectName}_${version}" .
32+
docker build -t "${repoAddress}:${projectName}_${version}" .
3233
echo -e "${successLogger}---------------- shell doc_book end ---------------- "
3334
push_docker_image
3435
}
3536

3637

3738

3839

39-
40+
## 推送镜像
4041
push_docker_image(){
41-
if [[ "$(docker images -q registry.cn-hangzhou.aliyuncs.com/142vip/doc_book:"${projectName}_${version}" 2> /dev/null)" != "" ]];
42+
if [[ "$(docker images -q "${repoAddress}:${projectName}_${version}" 2> /dev/null)" != "" ]];
4243
then
4344
## 推送
44-
docker push registry.cn-hangzhou.aliyuncs.com/142vip/doc_book:"${projectName}_${version}"
45+
docker push "${repoAddress}:${projectName}_${version}"
4546
echo -e "${successLogger}---------------- 上传镜像成功,删除本地镜像 ---------------- "
46-
docker rmi registry.cn-hangzhou.aliyuncs.com/142vip/doc_book:"${projectName}_${version}"
47+
docker rmi "${repoAddress}:${projectName}_${version}"
4748
else
48-
echo -e "${errorLogger}${currentTime}:镜像:registry.cn-hangzhou.aliyuncs.com/142vip/doc_book:${projectName}_${version}不存在"
49+
echo -e "${errorLogger}${currentTime}:镜像:${repoAddress}:${projectName}_${version}不存在"
4950
fi
5051
exit 0
5152
}

scripts/deploy.bash

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/bin/bash
2+
3+
## 功能:清除容器,删除旧镜像,创建新的容器
4+
## 参考:https://blog.csdn.net/Dontla/article/details/125210694
5+
## 作者:Rong姐姐好可爱
6+
## 使用示例:bash xxx.sh 容器名称 镜像地址
7+
##
8+
9+
## 日志颜色定义
10+
readonly successLogger="\033[36m"
11+
readonly errorLogger="\033[1;31m"
12+
readonly warnLogger="\033[1;33m"
13+
## 定义时间
14+
readonly currentTime=$(date "+%Y-%m-%d %H:%M:%S")
15+
readonly repoAddress="registry.cn-hangzhou.aliyuncs.com/142vip/doc_book"
16+
readonly containerName="JavaScriptCollection"
17+
readonly networkName="service_env_net"
18+
## 定义参数
19+
operationName=${1}
20+
version=${2}
21+
22+
23+
24+
25+
## 参数检查
26+
prepare_check(){
27+
if test -z "${containerName}"
28+
then
29+
echo -e "${errorLogger}${currentTime}:参数错误 部署平台不能为空."
30+
exit 0
31+
fi
32+
33+
}
34+
35+
## 判断容器存在即删除
36+
## - 一个参数,容器名称
37+
delete_container(){
38+
docker inspect "${1}" -f '{{.Name}}' > /dev/null
39+
if [ $? -eq 0 ] ;then
40+
echo -e "${warnLogger}${currentTime}容器:${1} 存在,现进行删除"
41+
docker rm -f "${1}"
42+
fi
43+
}
44+
45+
## 判断镜像存在,即删除
46+
## - 一个参数,镜像地址
47+
delete_image(){
48+
## 判断版本号
49+
if test -z "${version}";then
50+
echo -e "${errorLogger}${currentTime}:参数错误 镜像版本号不能为空."
51+
exit 0
52+
fi
53+
54+
if [[ "$(docker images -q "${1}" 2> /dev/null)" != "" ]];
55+
then
56+
echo -e "${warnLogger}${currentTime}镜像:${1}存在,现进行删除"
57+
docker rmi "${1}"
58+
fi
59+
}
60+
61+
62+
## 环境初始成功
63+
run(){
64+
if [ "${operationName}" == "gitee" ];then
65+
## 查看所有
66+
deploy_to_gitee
67+
exit 0;
68+
elif [ "${operationName}" == "github" ]; then
69+
deploy_to_github
70+
exit 0;
71+
elif [ "${operationName}" == "ali" ]; then
72+
## 删除容器
73+
delete_container "${containerName}"
74+
## 删除镜像
75+
delete_image "${repoAddress}:${containerName}_${version}"
76+
## 部署
77+
deploy_to_ali
78+
exit 0;
79+
else
80+
echo -e "${errorLogger}${currentTime}当前操作不支持,目前只支持:ali/gitee/github"
81+
exit 0
82+
fi
83+
}
84+
85+
86+
## 部署到阿里服务器
87+
deploy_to_ali(){
88+
echo -e "${successLogger}---------------- deploy ${containerName} ali start ---------------- "
89+
90+
docker run -d --name "${containerName}" \
91+
-p 7100:80 \
92+
--network="${networkName}" \
93+
--restart=unless-stopped \
94+
--ip=172.30.0.100 \
95+
"${repoAddress}:${containerName}_${version}"
96+
97+
echo -e "${successLogger}---------------- deploy ${containerName} ali end ------------------ "
98+
docker ps
99+
}
100+
101+
## 部署到github静态资源托管
102+
deploy_to_github(){
103+
echo -e "${successLogger}---------------- deploy ${containerName} github start ---------------- "
104+
105+
# 当命令以非零状态退出时,则退出shell
106+
set -e
107+
108+
# 进入上级目录,并编译
109+
npm run build && cd docs/.vuepress/dist
110+
git init && git add --all
111+
## 如果没有输入commit信息,则采用默认
112+
if [ "${commitInfo}" -eq "" ]; then
113+
commitInfo="JavaScriptCollection Init"
114+
fi
115+
git commit -m "refactor:${commitInfo}"
116+
117+
## 配置个人信息
118+
git config user.name "喜欢吃芝士葡萄的妹妹"
119+
git config user.email "fairy0115@2925.com"
120+
121+
git config --list
122+
123+
124+
# if you are deploying to https://<USERNAME>.github.io
125+
# git push -f git@github.com:<USERNAME>/<USERNAME>.github.io.git master
126+
# if you are deploying to https://<USERNAME>.github.io/<REPO>
127+
git push -f https://github.com/mmdapl/JavaScriptCollection.git master:pages/github
128+
cd -
129+
echo -e "${successLogger}---------------- deploy ${containerName} github end ------------------ "
130+
}
131+
132+
133+
## 部署到gitee静态资源托管
134+
deploy_to_gitee(){
135+
exit 0
136+
}
137+
138+
139+
140+
141+
prepare_check
142+
143+
run
144+
145+

scripts/docker_network.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ init_network(){
7878
"${defaultNetworkName}"
7979
echo -e "${successLogger}---------------- 创建后 ---------------------"
8080
docker network ls
81-
docker netwrok inspect "${networkName}"
81+
echo -e "${successLogger}---------------- 网络信息 ---------------------"
82+
docker network inspect "${defaultNetworkName}"
8283
fi
8384
}
8485

scripts/quick_deploy.bash

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)