调整脚本内容

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

View File

@ -1,39 +1,75 @@
#!/bin/bash
# 设置Docker镜像的仓库名和标签前缀
# 清理闲置资源
echo "Cleaning up unused Docker resources..."
yes | docker system prune -f
# 定义 Docker 镜像名称、Tag 前缀和当前时间戳
REPO_NAME="newgaokao"
TAG_PREFIX="build_"
# 获取当前时间戳作为标签后缀
TIMESTAMP=$(date +%Y%m%d%H%M%S)
TAG="${REPO_NAME}:${TAG_PREFIX}${TIMESTAMP}"
CONTAINER_NAME="${REPO_NAME}_container_${TIMESTAMP}"
# 尝试构建新的Docker镜像
docker build -t "${TAG}" . && {
# 如果构建成功,则停止并删除具有相同前缀的旧容器
# 钉钉 Webhook URL
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..."
docker ps -aqf "name=${REPO_NAME}_container_*" | xargs docker stop
docker ps -aqf "name=${REPO_NAME}_container_*" | xargs docker rm
# 删除具有相同前缀的旧镜像(排除最新的)
# 删除旧的镜像(保留最新的)
echo "Removing old images..."
docker images --format "{{.Repository}}:{{.Tag}}" | grep "${REPO_NAME}:${TAG_PREFIX}" | grep -v "${TAG}" | xargs docker rmi
# 显示构建完成的镜像信息
# 显示成功构建的镜像信息
echo "New image built successfully: ${TAG}"
docker images | grep "${TAG}"
# 启动新的容器(这里只是示例参数,你可能需要根据你的应用进行调整)
# 启动新的容器
echo "Starting new container ${CONTAINER_NAME}..."
docker run \
-p 8082:8082 \
--restart unless-stopped \
--name "${CONTAINER_NAME}" \
-d "${TAG}"
# 显示正在运行的容器信息
docker run \
-p 8082:8082 \
-v /var/www/wwwroot:/app/wwwroot \
--restart unless-stopped \
--name "${CONTAINER_NAME}" \
-d "${TAG}"
# 显示成功启动的容器信息
echo "Container started successfully: ${CONTAINER_NAME}"
docker ps | grep "${CONTAINER_NAME}"
docker ps | grep "${CONTAINER_NAME}"
# 钉钉消息推送
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."
}