1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-02-03 02:23:38 +08:00

Merge pull request #35 from mmdapl/feat/travis-ci-test

feat: 优化ci脚本,修复自动化流程
This commit is contained in:
mmdapl
2023-09-07 15:26:43 +08:00
committed by GitHub
24 changed files with 2179 additions and 828 deletions

View File

@@ -29,7 +29,7 @@ jobs:
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 14.20.1
node-version: 16.20.2
- name: PNPM Install
uses: pnpm/action-setup@v2
@@ -37,7 +37,7 @@ jobs:
version: 7
run_install: true
- name: Code ESlint Fix
run: pnpm lintfix
- name: Code Fix
run: ./scripts/lint --fix
- name: Build Site
run: pnpm build
run: ./scripts/bundle build

View File

@@ -41,12 +41,15 @@ jobs:
run: |
docker version
echo "-----------Docker Login-----------"
docker login --username=${{ env.UserName }} --password=${{ secrets.DOCKER_PASSWORD }} ${{env.REGISTRY}}
docker login \
--username=${{ env.UserName }} \
--password=${{ secrets.DOCKER_PASSWORD }} \
${{env.REGISTRY}}
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 14.20.1
node-version: 16.20.2
- name: PNPM Install
uses: pnpm/action-setup@v2
@@ -54,5 +57,10 @@ jobs:
version: 7
run_install: true
- name: Build And Push Docker image
run: PROXY_DOMAIN=true pnpm image:faster
## 构建支持domain
- name: Build To Dist
run: ./scripts/bundle build_proxy
- name: Push Docker image
run: ./scripts/bundle image_faster

View File

@@ -11,7 +11,6 @@ env:
REGISTRY: registry.cn-hangzhou.aliyuncs.com
UserName: mmdapl
jobs:
Docker-Image:
name: "构建Docker镜像"
@@ -32,12 +31,15 @@ jobs:
run: |
docker version
echo "-----------Docker Login-----------"
docker login --username=${{ env.UserName }} --password=${{ secrets.DOCKER_PASSWORD }} ${{env.REGISTRY}}
docker login \
--username=${{ env.UserName }} \
--password=${{ secrets.DOCKER_PASSWORD }} \
${{env.REGISTRY}}
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 14.20.1
node-version: 16.20.2
- name: PNPM Install
uses: pnpm/action-setup@v2
@@ -46,7 +48,7 @@ jobs:
run_install: true
- name: Build And Push Docker image
run: pnpm faster-image
run: ./script/bundle image
Deploy-ESC:
needs: Docker-Image

View File

@@ -22,7 +22,7 @@ jobs:
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 14.20.1
node-version: 16.20.2
- name: PNPM Install
uses: pnpm/action-setup@v2
@@ -32,8 +32,7 @@ jobs:
# 运行构建脚本
- name: Build VuePress Site
run:
PROXY_DOMAIN=true pnpm build
run: ./scripts/bundle build_proxy
- name: Deploy to GitHub Page
uses: crazy-max/ghaction-github-pages@v3

4
.npmrc
View File

@@ -1,2 +1,4 @@
## 锁定版本
engine-strict = true
shamefully-hoist=true
registry=https://registry.npmmirror.com
registry=https://registry.npmmirror.com

19
.travis.yml Normal file
View File

@@ -0,0 +1,19 @@
# 参考链接: https://docs.travis-ci.com/user/languages/javascript-with-nodejs/
language: node_js
node_js:
- 16
# 环境变量
#env:
# - EMBER_VERSION=default
# - EMBER_VERSION=release
# - EMBER_VERSION=beta
# - EMBER_VERSION=canary
# 安装依赖前先安装pnpm
before_install:
- "npm install -g npm@7"
install:
- pnpm i
script:
- pnpm lintfix
- pnpm build

24
.versionrc.js Normal file
View File

@@ -0,0 +1,24 @@
// commit-and-tag-version 配置参考https://github.com/conventional-changelog/standard-version
module.exports={
"header": "# Release history\n\nAll notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.\n\n<!-- #region recent-beta -->\n",
"releaseCommitMessageFormat": "chore(release): publish v{{currentTag}}",
"types": [
{ "type": "feat", "section": "Features" },
{ "type": "feature", "section": "Features" },
{ "type": "fix", "section": "Bug Fixes" },
{ "type": "perf", "section": "Performance Improvements" },
{ "type": "revert", "section": "Reverts" },
{ "type": "docs", "section": "Documentation", "hidden": true },
{ "type": "style", "section": "Styles", "hidden": true },
{ "type": "chore", "section": "Miscellaneous Chores", "hidden": true },
{ "type": "refactor", "section": "Code Refactoring", "hidden": true },
{ "type": "test", "section": "Tests", "hidden": true },
{ "type": "build", "section": "Build System", "hidden": true },
{ "type": "ci", "section": "Continuous Integration", "hidden": true }
],
"skip": {
"bump": true,
"commit": true,
"tag": true
}
}

0
CHANGELOG.md Normal file
View File

View File

@@ -7,7 +7,7 @@
#
# 注意vite构建需要支持node14以上安装node16较为稳妥
FROM registry.cn-hangzhou.aliyuncs.com/142vip/node:16.12.0-alpine AS build_base
FROM registry.cn-hangzhou.aliyuncs.com/142vip/node:16.20.2-alpine AS build_base
ARG CONTAINER_BUILD
WORKDIR /apps
COPY . .
@@ -16,10 +16,9 @@ RUN echo $CONTAINER_BUILD
## 基于容器自动构建
RUN if [ "$CONTAINER_BUILD" = "true" ]; then \
npm i pnpm@7 -g && pnpm i -D && pnpm build; \
npm i pnpm@7 -g && pnpm i -D && ./scripts/bundle build; \
fi;
FROM registry.cn-hangzhou.aliyuncs.com/142vip/nginx:1.23.0-alpine
ARG APP_VERSION

View File

@@ -1,42 +1,28 @@
{
"name": "408CSFamily",
"description": "专业代号408计算机基础知识点合集",
"version": "0.0.1-alpha.1",
"version": "0.0.1-alpha.0",
"packageManager": "pnpm@7.2.1",
"engines": {
"node": "14.x || 16.x || 18.x",
"pnpm": ">=7.2.1"
},
"author": {
"name": "Chu Fan",
"email": "fairy_408@2925.com",
"url": "https://github.com/142vip"
},
"scripts": {
"prepare": "husky install && npx husky add .husky/pre-commit \"npm run lintfix\" && chmod +x .husky/pre-commit",
"prepare": "rm -f .husky/pre-commit && husky install && npx husky add .husky/pre-commit \"./scripts/lint --fix\" && chmod +x .husky/pre-commit",
"dev": "vuepress dev docs",
"build": "vuepress build docs",
"build:proxy": "PROXY_DOMAIN=true vuepress build docs",
"image": "bash scripts/build_image.sh $npm_package_version",
"image:faster": "bash scripts/build_image.sh $npm_package_version faster",
"deploy:ali": "bash scripts/deploy.sh ali $npm_package_version",
"deploy:github": "bash scripts/page_deploy.sh",
"build": "./scripts/bundle build",
"build:proxy": "./scripts/bundle build_proxy",
"deploy:vercel": "vercel --prod",
"version:alpha": "npm version prerelease --preid alpha",
"version:patch": "npm version patch",
"lint": "eslint --ext .js,.vue,.ts --ignore-path .gitignore .",
"lintfix": "eslint --fix --ext .js,.ts,.vue --ignore-path .gitignore .",
"clean": "find . -name \"node_modules\" -type d -exec rm -rf '{}' +"
"clean": "find . -name \"node_modules\" -type d -exec rm -rf '{}' + "
},
"scripts-info": {
"prepare": "安装依赖预执行脚本",
"dev": "本地启动项目",
"build": "构建、打包",
"build:proxy": "构建、打包,支持项目名代理,使用第三方托管平台",
"image": "容器打包、构建镜像",
"image:faster": "本地build后快速构建镜像",
"deploy:ali": "部署到阿里云服务器",
"deploy:github": "部署到github平台集成ci",
"version:alpha": "alpha测试版号更新基于next分之操作",
"version:patch": "稳定版号更新基于master分支操作",
"lint": "eslint校验代码指出异常",
"lintfix": "格式化代码,自动化处理异常",
"clean": "快速删除本地依赖"
},
"devDependencies": {
@@ -44,6 +30,9 @@
"@typescript-eslint/parser": "^5.62.0",
"@vuepress/client": "2.0.0-beta.61",
"@vuepress/utils": "2.0.0-beta.61",
"bumpp": "^9.2.0",
"commit-and-tag-version": "11.2.3",
"enquirer": "^2.4.1",
"eslint": "^8.48.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.28.1",
@@ -56,7 +45,8 @@
"vue": "^3.3.4",
"vuepress": "2.0.0-beta.61",
"vuepress-plugin-search-pro": "2.0.0-beta.185",
"vuepress-theme-hope": "2.0.0-beta.185"
"vuepress-theme-hope": "2.0.0-beta.185",
"shelljs": "^0.8.5"
},
"license": "MIT"
}

1966
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

60
scripts/.exec Executable file
View File

@@ -0,0 +1,60 @@
#!/usr/bin/env node
const {join} = require('path')
const cwd = join(__dirname, '..')
process.env.PATH = `${join(cwd, 'node_modules', '.bin')}:${process.env.PATH}`
const {exec,exit} = require('shelljs');
/**
* 监听进程
* - 退出进程
*/
process.on('exit', () => {
exit()
})
/**
* 执行shell指令
* @param commands
*/
exports.execShell = async commands => {
let runCommands=[]
if(typeof commands ==='string'){
runCommands.push(commands)
}
// 批量执行
if(Array.isArray(commands)){
runCommands=commands
}
for (let index=0;index<runCommands.length;index++) {
const command=runCommands[index]
const count=index+1
console.log(`>>>command(${count}):\n${command} \n<<<command(${count})--start === \n`)
// await syncExec(command)
const execResult=await exec(command)
console.log(`\n<<<command(${count})--ending === `)
// 指令异常,不执行后续指令
if(execResult.code!==0){
break;
}
}
}
/**
* 脚本基础设置
* @type {{currentTime: string, errorLogger: string, successLogger: string, warnLogger: string}}
*/
exports.BaseSetting={
successLogger:"\033[36m",
errorLogger:"\033[1;31m",
warnLogger:"\033[1;33m",
// 定义时间
currentTime:'$(date "+%Y-%m-%d %H:%M:%S")'
}

View File

@@ -1,4 +0,0 @@
#!/bin/bash
## 登录阿里云hub
docker login --username=mmdapl registry.cn-hangzhou.aliyuncs.com

View File

@@ -1,85 +0,0 @@
#!/bin/bash
## 功能本地前后端项目构建、打包镜像上传docker仓库
## 参考https://blog.csdn.net/Dontla/article/details/125210694
## 使用示例bash xxx.sh 版本号 faster[可选]
## - bash build_image.sh 0.0.1 faster
## 作者:储凡
##
## 日志颜色定义
readonly successLogger="\033[36m"
readonly errorLogger="\033[1;31m"
## 定义时间
readonly currentTime=$(date "+%Y-%m-%d %H:%M:%S")
## 项目名称
readonly projectName="408CSFamily"
## 仓库地址
readonly repoAddress="registry.cn-hangzhou.aliyuncs.com/142vip/doc_book"
## 版本号
version=${1}
## 是否先本地构建执行npm run build操作
isFaster=${2}
## 镜像名称
imageTagName=${repoAddress}:${projectName}-${version}
## 预检查
prepare_check(){
if test -z "${version}";then
echo -e "${errorLogger}${currentTime}:参数错误 版本号不能为空。参考版本: 0.0.x"
exit 0
fi
}
## 运行命令
run(){
echo -e "${successLogger}---------------- shell ${projectName} start ---------------- "
if [ "${isFaster}" == "faster" ];then
## 本地构建、快速制作镜像
pnpm build && docker build \
--build-arg APP_VERSION="${version}" \
--build-arg CONTAINER_BUILD=false \
-t "${imageTagName}" .
else
## ci流程容器构建打包
docker build \
--build-arg APP_VERSION="${version}" \
--build-arg CONTAINER_BUILD=true \
-t "${imageTagName}" .
echo 12312
echo docker build \
--build-arg APP_VERSION="${version}" \
--build-arg CONTAINER_BUILD=true \
-t "${imageTagName}" .
fi
echo -e "${successLogger}---------------- shell ${projectName} end ---------------- "
push_docker_image
}
## 推送镜像
push_docker_image(){
if [[ "$(docker images -q "${imageTagName}" 2> /dev/null)" != "" ]];
then
## 推送
docker push "${imageTagName}"
echo -e "${successLogger}---------------- 上传镜像成功,删除本地镜像 ---------------- "
docker rmi "${imageTagName}"
else
echo -e "${errorLogger}${currentTime}[镜像] ${imageTagName}不存在"
fi
exit 0
}
prepare_check
run

138
scripts/bundle Executable file
View File

@@ -0,0 +1,138 @@
#!/usr/bin/env node
/**
*
* 将应用程序打包成可部署的包、文件、镜像
* 例如:
* - ./scripts/bundle build 基础部署打包
* - ./scripts/bundle build_proxy 用于三方平台部署打包
* - ./scripts/bundle image 构建容器镜像
* - ./scripts/bundle image_faster 本地build快速构建容器镜像
* - ./scripts/bundle xxx 其他参数默认执行build命令
* - ./scripts/bundle 交互式选择执行的命令
*/
const { execShell } = require('./.exec')
const { Select } = require('enquirer')
const packageVersion = require('../package.json').version
const projectName = '408CSFamily'
// 仓库地址
const repoAddress = 'registry.cn-hangzhou.aliyuncs.com/142vip/doc_book'
// 镜像地址
const imageName = `${repoAddress}:${projectName}-${packageVersion}`
/**
* 获取构建镜像的脚本
* @param containerBuild
* @param preBuild
* @param needProxy
* @returns {string[]}
*/
function getBuildImageScript({ containerBuild, preBuild, needProxy = false }) {
// 基础构建脚本
let baseBuildScript = ''
if (preBuild) {
baseBuildScript = needProxy ? './scripts/bundle build_proxy' : './scripts/bundle build'
}
return [
// 构建镜像
`
${baseBuildScript}
docker build \
--build-arg APP_VERSION=${packageVersion} \
--build-arg CONTAINER_BUILD=${containerBuild} \
-t ${imageName} .
`,
// 推送镜像
`
if [[ "$(docker images -q ${imageName} 2> /dev/null)" != "" ]];then
## 推送
docker push ${imageName}
echo "---------------- 上传镜像成功,删除本地镜像 ---------------- "
docker rmi ${imageName}
exit 0;
else
echo "镜像: ${imageName} , 不存在"
exit 1;
fi
`
]
}
/**
* 支持的脚本命令
*/
const SupportScripts = {
build: 'vuepress build docs',
build_proxy: 'PROXY_DOMAIN=true vuepress build docs',
image: getBuildImageScript({
containerBuild: true,
needProxy: false
}),
image_proxy: getBuildImageScript({
containerBuild: true,
needProxy: true
}),
// 直接从本地拿dist文件生成镜像
image_faster: getBuildImageScript({
containerBuild: false,
needProxy: false
})
}
async function getScriptCommand() {
const scriptName = process.argv[2]
let scriptCommand = SupportScripts.build
if (scriptName == null) {
const prompt = new Select({
header: '======================== 408CSFamily Cli For Building ========================',
footer: '======================== 408CSFamily Cli For Building ========================',
name: 'color',
message: 'What script will you want to run ',
choices: [
{
message: 'build',
name: SupportScripts.build,
value: '#00ffff'
},
{
message: 'build for fixing proxy',
name: SupportScripts.build_proxy,
value: '#000000'
},
{
message: 'build to docker image',
name: SupportScripts.image,
value: '#0000ff'
},
{
message: 'build to docker image with proxy',
name: SupportScripts.image_proxy,
value: '#0000ff'
},
{
message: 'build to docker image faster',
name: SupportScripts.image_faster,
value: '#0000ff'
}
]
})
scriptCommand = await prompt.run()
} else {
// 命中支持的脚本
if (Object.keys(SupportScripts).includes(scriptName)) { scriptCommand = SupportScripts[scriptName] }
}
return scriptCommand
}
;(async() => {
const scriptCommand = await getScriptCommand()
await execShell(scriptCommand)
})()

89
scripts/deploy Executable file
View File

@@ -0,0 +1,89 @@
#!/usr/bin/env node
/**
*
* 例如:
* - ./scripts/deploy ali
* - ./scripts/deploy github
*/
const { execShell } = require('./.exec')
const packageVersion = require('../package.json').version
const dockerDeployInfo = {
repoAddress: 'registry.cn-hangzhou.aliyuncs.com/142vip/doc_book',
containerName: '408CSFamily',
networkName: 'service_env_net'
}
const imageName = `${dockerDeployInfo.repoAddress}:${dockerDeployInfo.containerName}-${packageVersion}`
// 支持的命令
const SupportScripts = {
Github: `
set -e
./scripts/build proxy && cd docs/.vuepress/dist
git init && git add -A
## 配置信息
git config user.name 'chu fan'
git config user.email 'fairy_408@2925.com'
git config --list
git commit -m "release v${packageVersion}"
## 部署到github pages
git push -f https://github.com/mmdapl/408CSFamily.git master:pages/github
cd -
`,
Ali: [
// 容器存在即删除
`
if docker inspect --format='{{.State.Running}}' ${dockerDeployInfo.containerName} >/dev/null 2>&1;then
docker rm -f ${dockerDeployInfo.containerName}
exit 0;
else
echo "容器:${dockerDeployInfo.containerName},不存在"
exit 1;
fi
`,
// 镜像存在即删除
`
if [[ "$(docker images -q ${imageName} 2> /dev/null)" != "" ]];then
docker rmi ${imageName}
exit 0;
else
echo -e "镜像:${imageName},不存在"
exit 1;
fi
`,
// 运行容器
`
docker run -d --name ${dockerDeployInfo.containerName} \
-p 7000:80 \
--network=${dockerDeployInfo.networkName} \
--restart=unless-stopped \
--ip=172.30.0.100 \
${imageName}
`
]
}
const deployName = process.argv[2]
function getDeployCommand() {
let deployCommand = SupportScripts.Ali
// 部署到阿里云服务器
if (deployName === 'ali') {
deployCommand = SupportScripts.Ali
}
// 部署到Github
if (deployName === 'github') {
deployCommand = SupportScripts.Github
}
return deployCommand
}
// 执行
;(async() => {
const deployCommand = getDeployCommand()
// console.log(deployCommand)
await execShell(deployCommand)
})()

View File

@@ -1,144 +0,0 @@
#!/bin/bash
## 功能:清除容器,删除旧镜像,创建新的容器
## 参考https://blog.csdn.net/Dontla/article/details/125210694
## 作者:储凡
## 使用示例bash xxx.sh 容器名称 镜像地址
##
## 日志颜色定义
readonly successLogger="\033[36m"
readonly errorLogger="\033[1;31m"
readonly warnLogger="\033[1;33m"
## 定义时间
readonly currentTime=$(date "+%Y-%m-%d %H:%M:%S")
readonly repoAddress="registry.cn-hangzhou.aliyuncs.com/142vip/doc_book"
readonly containerName="408CSFamily"
readonly networkName="service_env_net"
## 定义参数
operationName=${1}
version=${2}
## 参数检查
prepare_check(){
if test -z "${containerName}"
then
echo -e "${errorLogger}${currentTime}:参数错误 部署平台不能为空."
exit 0
fi
}
## 判断容器存在即删除
## - 一个参数,容器名称
delete_container(){
docker inspect "${1}" -f '{{.Name}}' > /dev/null
if [ $? -eq 0 ] ;then
echo -e "${warnLogger}${currentTime}容器:${1} 存在,现进行删除"
docker rm -f "${1}"
fi
}
## 判断镜像存在,即删除
## - 一个参数,镜像地址
delete_image(){
## 判断版本号
if test -z "${version}";then
echo -e "${errorLogger}${currentTime}:参数错误 镜像版本号不能为空."
exit 0
fi
if [[ "$(docker images -q "${1}" 2> /dev/null)" != "" ]];
then
echo -e "${warnLogger}${currentTime}镜像:${1}存在,现进行删除"
docker rmi "${1}"
fi
}
## 环境初始成功
run(){
if [ "${operationName}" == "gitee" ];then
## 查看所有
deploy_to_gitee
exit 0;
elif [ "${operationName}" == "github" ]; then
deploy_to_github
exit 0;
elif [ "${operationName}" == "ali" ]; then
## 删除容器
delete_container "${containerName}"
## 删除镜像
delete_image "${repoAddress}:${containerName}-${version}"
## 部署
deploy_to_ali
exit 0;
else
echo -e "${errorLogger}${currentTime}当前操作不支持,目前只支持:ali/gitee/github"
exit 0
fi
}
## 部署到阿里服务器
deploy_to_ali(){
echo -e "${successLogger}---------------- deploy ${containerName} ali start ---------------- "
docker run -d --name "${containerName}" \
-p 7000:80 \
--network="${networkName}" \
--restart=unless-stopped \
--ip=172.30.0.100 \
"${repoAddress}:${containerName}-${version}"
echo -e "${successLogger}---------------- deploy ${containerName} ali end ------------------ "
docker ps
}
## 部署到github静态资源托管
deploy_to_github(){
echo -e "${successLogger}---------------- deploy ${containerName} github start ---------------- "
# 当命令以非零状态退出时则退出shell
set -e
# 进入上级目录,并编译
npm run build && cd docs/.vuepress/dist
git init && git add --all
## 如果没有输入commit信息则采用默认
if [ "${commitInfo}" -eq "" ]; then
commitInfo="408CSFamily Init"
fi
git commit -m "refactor:${commitInfo}"
## 配置个人信息
git config user.name "晚上吃芝士+葡萄的妹妹"
git config user.email "fairy0115@2925.com"
git config --list
# if you are deploying to https://<USERNAME>.github.io
# git push -f git@github.com:<USERNAME>/<USERNAME>.github.io.git master
# if you are deploying to https://<USERNAME>.github.io/<REPO>
git push -f https://github.com/mmdapl/408CSFamily.git master:pages/github
cd -
echo -e "${successLogger}---------------- deploy ${containerName} github end ------------------ "
}
## 部署到gitee静态资源托管
deploy_to_gitee(){
exit 0
}
prepare_check
run

10
scripts/dev Executable file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env node
/**
*
* 本地启动
* 例如:
* - ./scripts/dev
*/
const { execShell } = require('./.exec');
(async() => await execShell('vuepress dev docs'))()

178
scripts/docker Executable file
View File

@@ -0,0 +1,178 @@
#!/usr/bin/env node
/**
* docker cli工具
* 例如:
* - ./scripts/docker container xxx 容器相关
* - ./scripts/network image xxx 镜像相关
* - ./scripts/docker network xxx 网络相关
*/
const { execShell, BaseSetting } = require('./.exec')
const scriptName = process.argv[2]
/**
* 网络基础信息
* - 网络名称
* - 子网掩码
* - 网关地址
*/
const dockerNetworkInfo = {
defaultName: 'service_env_net',
subnet: '172.30.0.0/24',
gateway: '172.30.0.1'
}
/**
* docker containers 容器相关指令
*/
const SupportScriptsInContainer = {
run: '',
rm: '',
ps: 'docker ps'
}
/**
* docker images 相关脚本指令
*/
const SupportScriptsInImage = {
rm: 'docker rmi **',
ps: 'docker images'
}
/**
* docker network 相关脚本指令
*/
const SupportScriptsInNetWork = {
ls: 'docker network ls',
create: [
// 创建网关
`
docker network create \
--subnet=${dockerNetworkInfo.subnet} \
--gateway=${dockerNetworkInfo.gateway} \
${dockerNetworkInfo.defaultName}
`,
// 查看创建后基本信息
`
docker network inspect ${dockerNetworkInfo.defaultName}
`
],
rm: [
// 参数校验
`
if test -z "${dockerNetworkInfo.defaultName}";then
echo "参数错误 网络名称不能为空。脚本执行eg bash xxx.sh rm 网络名称"
exit 1;
fi
`,
// 判断网络是否存在
`
docker network ls | grep -w "${dockerNetworkInfo.defaultName}"
if [ $? -eq 1 ] ;then
echo "容器网络:${dockerNetworkInfo.defaultName} 不存在,删除无效"
exit 1;
fi
`,
// 删除网络
`
docker network rm "${dockerNetworkInfo.defaultName}"
`
],
inspect:
`
docker network inspect ${dockerNetworkInfo.defaultName}
`
}
function getContainerCommand() {
const name = process.argv[3]
if (name in SupportScriptsInContainer) {
return SupportScriptsInContainer[name]
}
// 默认查看所有容器
return SupportScriptsInContainer.ps
}
function getImageCommand() {
const name = process.argv[3]
if (name in SupportScriptsInImage) {
return SupportScriptsInImage[name]
}
return SupportScriptsInImage.ps
}
function getNetworkCommand() {
const name = process.argv[3]
if (name in SupportScriptsInNetWork) {
return SupportScriptsInNetWork[scriptName]
}
return SupportScriptsInNetWork.ls
}
// 支持的命令
const SupportScripts = {
ls: 'docker network ls',
create: [
// 创建网关
`
docker network create \
--subnet=${dockerNetworkInfo.subnet} \
--gateway=${dockerNetworkInfo.gateway} \
${dockerNetworkInfo.defaultName}
`,
// 查看创建后基本信息
`
docker network inspect ${dockerNetworkInfo.defaultName}
`
],
rm: [
// 参数校验
`
if test -z "${dockerNetworkInfo.defaultName}";then
echo "参数错误 网络名称不能为空。脚本执行eg bash xxx.sh rm 网络名称"
exit 1;
fi
`,
// 判断网络是否存在
`
docker network ls | grep -w "${dockerNetworkInfo.defaultName}"
if [ $? -eq 1 ] ;then
echo "容器网络:${dockerNetworkInfo.defaultName} 不存在,删除无效"
exit 1;
fi
`,
// 删除网络
`
docker network rm "${dockerNetworkInfo.defaultName}"
`
],
inspect:
`
docker network inspect ${dockerNetworkInfo.defaultName}
`
}
function getCommand() {
const scriptName = process.argv[3]
switch (scriptName) {
case 'network':
return getNetworkCommand()
case 'container':
return getContainerCommand()
case 'image':
return getImageCommand()
}
}
// 执行
;(async() => {
const command = getCommand(scriptName)
await execShell(command)
})()

View File

@@ -1,109 +0,0 @@
#!/bin/bash
## 功能设置docker自定义network并指定网关、IP范围
## 参考https://blog.csdn.net/Dontla/article/details/125210694
## 作者:储凡
## 使用示例bash xxx.sh 容器名称 镜像地址
##
## 日志颜色定义
readonly successLogger="\033[36m"
readonly errorLogger="\033[1;31m"
readonly warnLogger="\033[1;33m"
## 定义时间
readonly currentTime=$(date "+%Y-%m-%d %H:%M:%S")
operationName=${1}
networkName=${2}
## 网络名称
readonly defaultNetworkName="service_env_net"
## 子网范围 [/24 子网掩码]
readonly subnet="172.30.0.0/24"
## 网关
readonly gateway="172.30.0.1"
prepare_check(){
if test -z "$operationName"
then
echo -e "${errorLogger}${currentTime}:参数错误 操作类型不能为空。脚本执行eg bash xxx.sh [ls/init/rm]"
exit 0
fi
}
run(){
if [ "$operationName" == "ls" ];then
## 查看所有
echo -e "${successLogger}---------------- shell ls start ---------------- "
docker network ls
echo -e "${successLogger}---------------- shell ls end ------------------ "
exit 0;
elif [ "$operationName" == "init" ]; then
echo -e "${successLogger}---------------- shell init start ---------------- "
## 初始化
init_network
echo -e "${successLogger}---------------- shell init start ---------------- "
exit 0;
elif [ "$operationName" == "rm" ]; then
## 移除
echo -e "${successLogger}---------------- shell rm start ---------------- "
remove_network
echo -e "${successLogger}---------------- shell rm start ---------------- "
exit 0;
else
echo -e "${errorLogger}当前操作不支持,目前只支持:ls/init/rm"
exit 0
fi
}
## 创建网络
init_network(){
## 查询network -w【全匹配】
docker network ls | grep -w "${defaultNetworkName}"
if [ $? -eq 0 ] ;then
echo -e "${warnLogger}---------------- 容器:${defaultNetworkName} 已存在,无需创建 ---------------- "
docker network ls
exit ;
else
echo -e "${successLogger}---------------- 网络${defaultNetworkName},现进行初始化 ---------------- "
docker network ls
docker network create \
--subnet="${subnet}" \
--gateway="${gateway}" \
"${defaultNetworkName}"
echo -e "${successLogger}---------------- 创建后 ---------------------"
docker network ls
echo -e "${successLogger}---------------- 网络信息 ---------------------"
docker netwrok inspect "${defaultNetworkName}"
fi
}
## 删除网络
remove_network(){
if test -z "$networkName";then
echo -e "${errorLogger}${currentTime}:参数错误 网络名称不能为空。脚本执行eg bash xxx.sh rm 网络名称"
exit 0
fi
## 判断是否存在 -w【全匹配】
docker network ls | grep -w "${networkName}"
if [ $? -eq 1 ] ;then
echo -e "${errorLogger}${currentTime}:容器网络:${networkName} 不存在,删除无效 "
exit 0;
fi
echo -e "${successLogger}----------------删除前---------------------"
docker network ls
docker network rm "${networkName}"
echo -e "${successLogger}----------------删除后---------------------"
docker network ls
}
prepare_check
run

16
scripts/lint Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env node
/**
*
* 格式化代码
* 例如:
* - ./scripts/lint
* - ./scripts/lint --fix
*/
const { execShell } = require('./.exec')
const scriptName = process.argv[2]
const fixed = scriptName != null ? '--fix' : '';
// 可以在--fix后指定目录
(async() => await execShell(`eslint ${fixed} --ext .js,.ts,.vue --ignore-path .gitignore .`))()

View File

@@ -1,27 +0,0 @@
#!/usr/bin/env sh
commitInfo=${1}
set -e
npm run build-proxy && cd docs/.vuepress/dist
git init && git add -A
## 如果没有输入commit信息则采用默认
if [ "${commitInfo}" -eq "" ]; then
commitInfo="408CSFamily page init"
fi
## 配置个人信息
git config user.name 'chu fan' && git config user.email 'fairy_408@2925.com' && git config --list
git commit -m "refactor:${commitInfo}"
# git push -f https://github.com/mmdapl/408CSFamily.git main
## 部署到github pages
git push -f https://github.com/mmdapl/408CSFamily.git main:pages/github
cd -

17
scripts/release Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
##
## 版本发布脚本
## 链接https://jstools.dev/version-bump-prompt/
## 使用: ./scripts/release
##
## 利用commit-and-tag-version生成changelog文档并跳过commit、tag操作
readonly generateChangeLog='pnpm commit-and-tag-version && git add CHANGELOG.md'
## git提交信息
readonly commitInfo='chore(release): publish v%s'
bumpp --preid alpha --execute="$generateChangeLog" --commit "$commitInfo" --all --tag --push

35
scripts/test Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env node
/**
* 脚本测试
* 链接https://github.com/enquirer/enquirer
*/
// const {execShell} = require("./.exec");
(async() => {
const { AutoComplete } = require('enquirer')
const prompt = new AutoComplete({
name: 'flavor',
message: 'Pick your favorite flavor',
limit: 10,
initial: 2,
choices: [
'Almond',
'Apple',
'Banana',
'Blackberry',
'Blueberry',
'Cherry',
'Chocolate',
'Cinnamon',
'Coconut',
'Cranberry',
'Grape'
]
})
prompt.run()
.then(answer => console.log('Answer:', answer))
.catch(console.error)
})()