Skip to content

Commit b1a12ae

Browse files
authored
feat: enhance docker workflow with PR validation (#16)
* feat: enhance docker workflow with PR validation * fix: correct workflow conditions for PR builds * refactor: simplify release workflow and version control * fix: add missing steps in docker job * chore: bump version to 1.0.4
1 parent 9076ad1 commit b1a12ae

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

.github/workflows/docker-publish.yml

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,21 @@ jobs:
2323
fetch-depth: 0
2424
token: ${{ secrets.PAT }}
2525

26-
- name: Setup Git
27-
run: |
28-
git config --global user.name 'github-actions[bot]'
29-
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
30-
31-
- name: Get latest version
32-
id: get_version
33-
run: |
34-
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
35-
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV
36-
37-
# 从package.json中获取当前版本
38-
if [ -f "package.json" ]; then
39-
CURRENT_VERSION=$(node -p "require('./package.json').version" 2>/dev/null || echo "0.0.0")
40-
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV
41-
else
42-
echo "CURRENT_VERSION=0.0.0" >> $GITHUB_ENV
43-
fi
44-
4526
- name: Check version
4627
id: check_version
4728
run: |
29+
# 从package.json获取版本
30+
CURRENT_VERSION=$(node -p "require('./package.json').version")
31+
32+
# 获取最新tag
33+
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
4834
LATEST_VERSION=${LATEST_TAG#v}
35+
4936
if [ "$LATEST_VERSION" = "$CURRENT_VERSION" ]; then
37+
echo "版本号未变化,跳过发布"
5038
echo "SKIP_RELEASE=true" >> $GITHUB_ENV
51-
echo "Versions are identical ($LATEST_VERSION), skipping release process"
5239
else
40+
echo "检测到新版本,将创建release"
5341
echo "SKIP_RELEASE=false" >> $GITHUB_ENV
5442
fi
5543
@@ -64,36 +52,63 @@ jobs:
6452
env:
6553
GITHUB_TOKEN: ${{ secrets.PAT }}
6654

67-
build:
55+
docker:
6856
runs-on: ubuntu-latest
69-
needs: release
70-
if: github.event_name == 'pull_request'
57+
needs: [release]
58+
if: |
59+
github.event_name == 'push' && github.ref == 'refs/heads/main' && needs.release.outputs.SKIP_RELEASE != 'true'
7160
steps:
7261
- name: Checkout code
7362
uses: actions/checkout@v4
7463

7564
- name: Set up Docker Buildx
7665
uses: docker/setup-buildx-action@v3
7766

78-
- name: Build only
67+
- name: Get version from package.json
68+
id: package-version
69+
run: |
70+
VERSION=$(node -p "require('./package.json').version")
71+
echo "version=$VERSION" >> $GITHUB_OUTPUT
72+
73+
- name: Docker meta
74+
id: meta
75+
uses: docker/metadata-action@v5
76+
with:
77+
images: im594/mininote-md
78+
tags: |
79+
type=raw,value=latest
80+
type=raw,value=${{ steps.package-version.outputs.version }}
81+
type=sha,prefix=sha-
82+
83+
- name: Login to Docker Hub
84+
uses: docker/login-action@v3
85+
with:
86+
username: ${{ secrets.DOCKERHUB_USERNAME }}
87+
password: ${{ secrets.DOCKERHUB_TOKEN }}
88+
89+
- name: Build and push
7990
uses: docker/build-push-action@v5
8091
with:
8192
context: .
82-
push: false
93+
push: true
94+
tags: ${{ steps.meta.outputs.tags }}
95+
labels: ${{ steps.meta.outputs.labels }}
8396
cache-from: type=registry,ref=im594/mininote-md:buildcache
84-
85-
publish:
97+
cache-to: type=registry,ref=im594/mininote-md:buildcache,mode=max
98+
99+
pr_docker:
86100
runs-on: ubuntu-latest
87-
needs: release
88-
if: |
89-
github.event_name == 'push' &&
90-
github.ref == 'refs/heads/main' &&
91-
needs.release.outputs.SKIP_RELEASE != 'true'
101+
if: github.event_name == 'pull_request'
92102
steps:
103+
- name: Set PR status
104+
run: |
105+
echo "Running in PR mode - will build and test with temporary tag"
106+
93107
- name: Checkout code
94108
uses: actions/checkout@v4
95-
with:
96-
token: ${{ secrets.GITHUB_TOKEN }}
109+
110+
- name: Set up Docker Buildx
111+
uses: docker/setup-buildx-action@v3
97112

98113
- name: Get version from package.json
99114
id: package-version
@@ -107,12 +122,7 @@ jobs:
107122
with:
108123
images: im594/mininote-md
109124
tags: |
110-
type=raw,value=latest
111-
type=raw,value={{version}}
112-
type=sha,prefix=sha-
113-
114-
- name: Set up Docker Buildx
115-
uses: docker/setup-buildx-action@v3
125+
type=raw,value=pr-${{ github.event.pull_request.number }}
116126
117127
- name: Login to Docker Hub
118128
uses: docker/login-action@v3
@@ -128,4 +138,9 @@ jobs:
128138
tags: ${{ steps.meta.outputs.tags }}
129139
labels: ${{ steps.meta.outputs.labels }}
130140
cache-from: type=registry,ref=im594/mininote-md:buildcache
131-
cache-to: type=registry,ref=im594/mininote-md:buildcache,mode=max
141+
cache-to: type=registry,ref=im594/mininote-md:buildcache,mode=max
142+
143+
- name: Cleanup PR image
144+
run: |
145+
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${{ secrets.DOCKERHUB_USERNAME }}'", "password": "'${{ secrets.DOCKERHUB_TOKEN }}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
146+
curl -X DELETE -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/im594/mininote-md/tags/pr-${{ github.event.pull_request.number }}/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mininote-md",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"main": "backend/src/server.js",
55
"scripts": {
66
"start": "node backend/src/server.js",

0 commit comments

Comments
 (0)