代码拉取完成,页面将自动刷新
# runner 会挂载主机的以下目录:
# /root/deploy: 部署主目录
# /var/run/docker.sock: dind共享
# /var/lib/docker: 镜像共享
# /usr/local/bin/docker-compose: compose共享
variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
services:
- docker:dind
# 定义不同分支的端口,部署路径
build:
stage: test
tags:
- web
before_script:
- |
case "$CI_COMMIT_BRANCH" in
"old_main")
DEPLOY_NAME="oldh5"
;;
"new_version_main")
DEPLOY_NAME="h5"
;;
*)
echo "Branch '$CI_COMMIT_BRANCH' does not match any predefined configurations."
exit 1
;;
esac
escape_markdown() {
echo "$1" | sed 's/\\/\\\\/g;s/_/\\_/g;s/`/\\`/g;s/\./\\./g;s/-/\\-/g;s/+/\\+/g;s/!/\\!/g;s/(/\\(/g;s/)/\\)/g;s/\[/\\[/g;s/\]/\\]/g;s/{/\\{/g;s/}/\\}/g;s/~/\\~/g;s/\^/\\^/g;s/|/\\|/g;s/:/\\:/g;s/ /\\ /g;'
}
COMMIT_BRANCH=$(escape_markdown "$CI_COMMIT_BRANCH")
COMMIT_MESSAGE_STR=$(escape_markdown "$(git log -1 --pretty=format:%s)")
COMMIT_AUTHOR=$(escape_markdown "$(git log -1 --pretty=format:%an)")
COMMIT_TIMESTAMP=$(git log -1 --pretty=format:%ct)
COMMIT_TIME_STRING0=$(TZ="Asia/Shanghai" date -d @$COMMIT_TIMESTAMP +"%Y-%m-%d %H:%M:%S")
COMMIT_TIME_STRING=$(escape_markdown "$COMMIT_TIME_STRING0")
echo "COMMIT_MESSAGE_STR=$COMMIT_MESSAGE_STR" > /tmp/commit_info.txt
echo "COMMIT_AUTHOR=$COMMIT_AUTHOR" >> /tmp/commit_info.txt
echo "COMMIT_TIME_STRING=\"$COMMIT_TIME_STRING\"" >> /tmp/commit_info.txt
script:
- |
DEPLOY_DST="/root/deploy/$DEPLOY_NAME"
mkdir -p $DEPLOY_DST
# 清理异常构建容器,并启动构建容器
if docker ps -q -f name=temp0 > /dev/null; then docker stop temp0 && docker rm temp0; fi
docker run -d --name temp0 node:22.1.0 bash -c "tail -f /dev/null"
# # 替换静态资源# 在 src 目录下执行替换操作
# find src -type f -exec sed -i "s|\"/imgs/|\"/$DEPLOY_NAME/imgs/|g" {} +
# find src -type f -exec sed -i "s|\"/pwa/|\"/$DEPLOY_NAME/pwa/|g" {} +
# find src -type f -exec sed -i "s|\"/svga/|\"/$DEPLOY_NAME/svga/|g" {} +
# find src -type f -exec sed -i "s|'\/imgs/|'\/$DEPLOY_NAME/imgs/|g" {} +
# find src -type f -exec sed -i "s|'\/pwa/|'\/$DEPLOY_NAME/pwa/|g" {} +
# find src -type f -exec sed -i "s|'\/svga/|'\/$DEPLOY_NAME/svga/|g" {} +
# # 在 public 目录下执行替换操作
# find public -type f -exec sed -i "s|\"/imgs/|\"/$DEPLOY_NAME/imgs/|g" {} +
# find public -type f -exec sed -i "s|\"/pwa/|\"/$DEPLOY_NAME/pwa/|g" {} +
# find public -type f -exec sed -i "s|\"/svga/|\"/$DEPLOY_NAME/svga/|g" {} +
# find public -type f -exec sed -i "s|'\/imgs/|'\/$DEPLOY_NAME/imgs/|g" {} +
# find public -type f -exec sed -i "s|'\/pwa/|'\/$DEPLOY_NAME/pwa/|g" {} +
# find public -type f -exec sed -i "s|'\/svga/|'\/$DEPLOY_NAME/svga/|g" {} +
# 拷贝代码到容器并开始构建
docker cp ./ temp0:/data
docker exec temp0 bash -c "cd /data && yarn && VITE_PUBLIC_PATH=$DEPLOY_NAME npm run build"
docker cp temp0:/data/dist dist
cp -rf dist/* $DEPLOY_DST
# 删除临时构建容器
docker stop temp0 && docker rm temp0
docker run --rm botw python bot.py "$(echo -e "Status:\ Deploy Success🎉🎉🎉\nBranch:\ ${COMMIT_BRANCH}\nMessage:\ ${COMMIT_MESSAGE_STR}\nAuthor:\ ${COMMIT_AUTHOR}\nTime:\ ${COMMIT_TIME_STRING}")"
rules:
# 新版本使用rules控制条件
- if: '$CI_COMMIT_BRANCH == "old_main"'
when: always
- if: '$CI_COMMIT_BRANCH == "new_version_main"'
when: always
after_script:
- |
if [ "$CI_JOB_STATUS" == "failed" ]; then
source /tmp/commit_info.txt
docker run --rm botw python bot.py "$(echo -e "Status:\ Deploy Failed❌\nBranch:\ ${COMMIT_BRANCH}\nMessage:\ ${COMMIT_MESSAGE_STR}\nAuthor:\ ${COMMIT_AUTHOR}\nTime:\ ${COMMIT_TIME_STRING}")"
fi
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。