Skip to content

Commit 17a9ac3

Browse files
committed
[update]增加历史标签自动删除功能
1 parent 917dd02 commit 17a9ac3

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

.github/workflows/dev_deploy.yml

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@ jobs:
2525
run: |
2626
sudo apt-get update
2727
sudo apt-get install -y docker-compose
28+
29+
# 生成时间戳镜像标签
30+
- name: Generate image tag
31+
run: echo "IMAGE_TAG=dev-$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV
2832

2933
# 构建 Docker 镜像
3034
- name: Build Docker image
31-
run: docker-compose -f docker-compose-dev.yml build
35+
run: IMAGE_TAG=${{ env.IMAGE_TAG }} docker-compose -f docker-compose.yml build
3236

33-
# 检查镜像是否能成功启动一个容器
37+
# 启动容器
3438
- name: Run Docker container
35-
run: docker-compose -f docker-compose-dev.yml up -d
39+
run: IMAGE_TAG=${{ env.IMAGE_TAG }} docker-compose -f docker-compose.yml up -d
3640

3741
# 检查容器状态
3842
- name: Check container status
3943
id: container_status
4044
run: |
41-
CONTAINER_NAME=$(docker-compose -f docker-compose-dev.yml ps -q | head -n 1)
45+
CONTAINER_NAME=$(IMAGE_TAG=${{ env.IMAGE_TAG }} docker-compose -f docker-compose.yml ps -q | head -n 1)
4246
if [ -z "$CONTAINER_NAME" ]; then
4347
echo "No containers found, failing the step."
4448
exit 1
@@ -57,9 +61,36 @@ jobs:
5761
# 推送镜像到 kworlds's Docker Hub
5862
- name: Push Docker image to Kworlds's Docker Hub
5963
if: success()
60-
run: docker push dockerhub.kworlds.cn/pure-drf-api:dev
64+
run: docker push dockerhub.kworlds.cn/pure-drf-api:${{ env.IMAGE_TAG }}
65+
66+
# 获取dev标签旧镜像
67+
- name: Delete old dev tag from Docker Hub
68+
run: |
69+
OLD_DIGEST=$(curl -sI -u ${{ vars.DOCKER_HUB_USERNAME }}:${{ secrets.DOCKER_HUB_PASSWORD }} \
70+
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
71+
https://dockerhub.kworlds.cn/v2/pure-drf-api/manifests/dev | \
72+
awk -F': ' '/^[Dd]ocker-[Cc]ontent-[Dd]igest:/ {gsub(/\r/, "", $2); print $2}')
73+
74+
# 将新镜像标签修改为dev并推送
75+
- name: Tag and push new image with dev tag
76+
if: success()
77+
run: |
78+
docker tag dockerhub.kworlds.cn/pure-drf-api:${{ env.IMAGE_TAG }} dockerhub.kworlds.cn/pure-drf-api:dev
79+
docker push dockerhub.kworlds.cn/pure-drf-api:dev
6180
62-
# 更新开发环境镜像并重启容器
81+
# 删除旧dev镜像
82+
- name: Delete old dev tag from Docker Hub
83+
if: success()
84+
run: |
85+
f [ -n "$OLD_DIGEST" ]; then
86+
curl -X DELETE -u ${{ vars.DOCKER_HUB_USERNAME }}:${{ secrets.DOCKER_HUB_PASSWORD }} \
87+
https://dockerhub.kworlds.cn/v2/pure-drf-api/manifests/$OLD_DIGEST
88+
else
89+
echo "No old digest found to delete."
90+
exit 1
91+
fi
92+
93+
# 更新开发环境镜像并重启容器,清理垃圾镜像
6394
- name: Deploy to remote server
6495
if: success()
6596
run: |
@@ -69,4 +100,5 @@ jobs:
69100
docker-compose pull
70101
docker-compose down
71102
docker-compose up -d
103+
docker exec -it dockerhub registry garbage-collect /etc/docker/registry/config.yml
72104
EOF

docker-compose-prod.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
services:
22
api:
3-
image: dockerhub.kworlds.cn/pure-drf-api:dev # 镜像名称 hub路径/镜像名称:版本号(tag)
4-
build: .
3+
image: dockerhub.kworlds.cn/pure-drf-api:${IMAGE_TAG} # 使用环境变量 IMAGE_TAG
4+
build:
5+
context: .
56
ports:
67
- "8000:8000"
78
volumes:
89
- ./logs:/app/logs # 映射 logs 目录
910
- ./.env:/app/.env # 映射 .env 文件
1011
- ./db.sqlite3:/app/db.sqlite3 # 映射数据库文件
1112
- ./gunicorn.conf.py:/app/gunicorn.conf.py # 映射 Gunicorn 配置文件
13+
environment:
14+
IMAGE_TAG: "${IMAGE_TAG}" # 显式传递 IMAGE_TAG 环境变量

0 commit comments

Comments
 (0)