调整脚本内容

develop
old易 2024-09-29 16:19:39 +08:00
parent 21b51214e1
commit b4754c6299
2 changed files with 90 additions and 51 deletions

View File

@ -1,36 +1,39 @@
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone RUN echo 'Asia/Shanghai' >/etc/timezone
WORKDIR /source
#RUN apk add --no-cache ca-certificates python3 bash openssh git openssl-dev uwsgi uwsgi-python3 # 设置 Git 凭据为可传递的构建参数
#RUN apk add --no-cache --virtual .build-deps python3-dev gcc musl-dev libffi-dev make \ ARG GIT_USERNAME
#&& pip3 install --no-cache-dir --trusted-host mirrors.aliyun.com -i http://mirrors.aliyun.com/pypi/simple/ \ ARG GIT_PERSONAL_ACCESS_TOKEN
#pymysql==0.8.1 \
#Flask==1.0.2 \ # 更新系统和安装 Git 和证书
#Flask-RESTful==0.3.6 \ RUN apt-get update && apt-get install -y git
#Flask-Script==2.0.6 \
#Flask-SQLAlchemy==2.3.2 \ # 配置 Git 以进行稀疏检出,使用全局设置
#Flask-WTF==0.14.2 \ RUN git config --global core.sparseCheckout true
#SQLAlchemy==1.2.7 \
#simplejson==3.16.0 \ # 初始化 Git 仓库
#six==1.11.0 \ RUN git init
#celery==4.2.1 \ RUN git remote add origin http://$GIT_USERNAME:$GIT_PERSONAL_ACCESS_TOKEN@nas.jinzejk.com:3000/yly/NewGaoKaoApi.git
#xlrd==1.1.0 \
#xlwt==1.3.0 \ # 启用稀疏检出并指定要拉取的目录
#msgpack==0.5.0 \ RUN echo "Admin.NET" >> .git/info/sparse-checkout
#&& apk del .build-deps RUN git pull --depth 1 origin devlop
# RUN git checkout develop
#RUN git clone https://github.com/Supervisor/supervisor.git \
#&& cd supervisor \ # 打印 Git 状态和日志以验证代码拉取情况
#&& python3 setup.py install \ RUN git status
#&& cd .. \ RUN git log -1
#&& rm -rf supervisor \
#&& cd /etc/ \ # 进入项目所在目录并恢复和发布项目
#&& echo_supervisord_conf > supervisord.conf \ WORKDIR /source/newgaokao/New_College.Api
#&& echo '[include]' >> supervisord.conf \ RUN dotnet restore
#&& echo 'files = /code/supervisor/*.ini' >> supervisord.conf \ RUN dotnet publish -c release -o /app --no-restore -f net6.0
#&& supervisord -c /etc/supervisord.conf
# 运行镜像
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app WORKDIR /app
COPY . . COPY --from=build /app ./
EXPOSE 8082 EXPOSE 8082
ENTRYPOINT ["dotnet", "New_College.Api.dll","-b","0.0.0.0"] ENTRYPOINT ["dotnet", "New_College.Api.dll","-b","0.0.0.0"]

View File

@ -1,39 +1,75 @@
#!/bin/bash #!/bin/bash
# 设置Docker镜像的仓库名和标签前缀 # 清理闲置资源
echo "Cleaning up unused Docker resources..."
yes | docker system prune -f
# 定义 Docker 镜像名称、Tag 前缀和当前时间戳
REPO_NAME="newgaokao" REPO_NAME="newgaokao"
TAG_PREFIX="build_" TAG_PREFIX="build_"
# 获取当前时间戳作为标签后缀
TIMESTAMP=$(date +%Y%m%d%H%M%S) TIMESTAMP=$(date +%Y%m%d%H%M%S)
TAG="${REPO_NAME}:${TAG_PREFIX}${TIMESTAMP}" TAG="${REPO_NAME}:${TAG_PREFIX}${TIMESTAMP}"
CONTAINER_NAME="${REPO_NAME}_container_${TIMESTAMP}" CONTAINER_NAME="${REPO_NAME}_container_${TIMESTAMP}"
# 尝试构建新的Docker镜像 # 钉钉 Webhook URL
docker build -t "${TAG}" . && { DINGDING_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=fca104958fea6273c9c7ef3f08b3d552645c214f929066785e8caf6e1885a5a6"
# 如果构建成功,则停止并删除具有相同前缀的旧容器
# 输入 Git 用户名和密码(如果没有配置 Git 凭据管理器的话)
GIT_USERNAME="yly"
GIT_PERSONAL_ACCESS_TOKEN="53789ac2bb0ef13556e0551024f92f2872ff0918"
#清理所有docker资源
#docker system prune -a --volumes
# 构建 Docker 镜像并传递 Git 凭据
docker build --no-cache --build-arg GIT_USERNAME="${GIT_USERNAME}" --build-arg GIT_PERSONAL_ACCESS_TOKEN="${GIT_PERSONAL_ACCESS_TOKEN}" -t "${TAG}" . && {
# 构建成功后停止并删除之前的容器
echo "Stopping and removing old containers..." echo "Stopping and removing old containers..."
docker ps -aqf "name=${REPO_NAME}_container_*" | xargs docker stop docker ps -aqf "name=${REPO_NAME}_container_*" | xargs docker stop
docker ps -aqf "name=${REPO_NAME}_container_*" | xargs docker rm docker ps -aqf "name=${REPO_NAME}_container_*" | xargs docker rm
# 删除具有相同前缀的旧镜像(排除最新的) # 删除旧的镜像(保留最新的)
echo "Removing old images..." echo "Removing old images..."
docker images --format "{{.Repository}}:{{.Tag}}" | grep "${REPO_NAME}:${TAG_PREFIX}" | grep -v "${TAG}" | xargs docker rmi docker images --format "{{.Repository}}:{{.Tag}}" | grep "${REPO_NAME}:${TAG_PREFIX}" | grep -v "${TAG}" | xargs docker rmi
# 显示构建完成的镜像信息 # 显示成功构建的镜像信息
echo "New image built successfully: ${TAG}" echo "New image built successfully: ${TAG}"
docker images | grep "${TAG}" docker images | grep "${TAG}"
# 启动新的容器(这里只是示例参数,你可能需要根据你的应用进行调整) # 启动新的容器
echo "Starting new container ${CONTAINER_NAME}..." echo "Starting new container ${CONTAINER_NAME}..."
docker run \ docker run \
-p 8082:8082 \ -p 8082:8082 \
-v /var/www/wwwroot:/app/wwwroot \
--restart unless-stopped \ --restart unless-stopped \
--name "${CONTAINER_NAME}" \ --name "${CONTAINER_NAME}" \
-d "${TAG}" -d "${TAG}"
# 显示正在运行的容器信息
# 显示成功启动的容器信息
echo "Container started successfully: ${CONTAINER_NAME}" echo "Container started successfully: ${CONTAINER_NAME}"
docker ps | grep "${CONTAINER_NAME}" docker ps | grep "${CONTAINER_NAME}"
} || {
# 如果构建失败,则打印错误信息 # 钉钉消息推送
echo "Failed to build the Docker image." curl "$DINGDING_WEBHOOK" \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "text",
"text": {
"content": "SSO服务Docker 构建完成,镜像: '"${TAG}"' 容器: '"${CONTAINER_NAME}"' 已成功启动。"
}
}'
echo "Notification sent to DingDing."
} || {
# 构建失败时显示错误信息
echo "Failed to build the Docker image."
# 钉钉消息推送
curl "$DINGDING_WEBHOOK" \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "text",
"text": {
"content": "SSO服务 Docker 构建失败,请检查错误日志。"
}
}'
echo "Error notification sent to DingDing."
} }