-
Notifications
You must be signed in to change notification settings - Fork 0
131 lines (114 loc) · 4.65 KB
/
Copy pathversion-release.yml
File metadata and controls
131 lines (114 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: Version Release
on:
push:
branches:
- master
- dev
paths:
- 'VERSION'
workflow_dispatch:
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Read version
id: version
run: |
VERSION=$(head -1 VERSION | tr -d '[:space:]')
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
if echo "$VERSION" | grep -qE '\-dev'; then
echo "is_dev=true" >> "$GITHUB_OUTPUT"
else
echo "is_dev=false" >> "$GITHUB_OUTPUT"
if grep -qE "^## \[$VERSION\]" CHANGELOG.md; then
echo "changelog_match=true" >> "$GITHUB_OUTPUT"
else
echo "changelog_match=false" >> "$GITHUB_OUTPUT"
echo "::warning::CHANGELOG.md 中未找到版本 $VERSION 的更新日志,跳过 Release 和 Docker 构建"
fi
fi
- name: Create tag
if: steps.version.outputs.is_dev == 'false' && steps.version.outputs.changelog_match == 'true'
run: |
VERSION="${{ steps.version.outputs.version }}"
TAG="v${VERSION}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "标签 $TAG 已存在,跳过创建"
else
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
echo "标签 $TAG 已创建并推送"
fi
- name: Extract release notes
if: steps.version.outputs.is_dev == 'false' && steps.version.outputs.changelog_match == 'true'
env:
RELEASE_VERSION: ${{ steps.version.outputs.version }}
run: |
python3 << 'PYEOF'
import re, os
with open('CHANGELOG.md', encoding='utf-8') as f:
content = f.read()
version = os.environ['RELEASE_VERSION']
pattern = rf'## \[{re.escape(version)}\].*?(?=\n## \[|\Z)'
match = re.search(pattern, content, re.DOTALL)
if match:
notes = match.group(0).strip()
notes = notes.split('\n', 1)[1] if '\n' in notes else ''
notes = re.sub(r'\n---\s*$', '', notes).strip()
with open('release_notes.md', 'w', encoding='utf-8') as f:
f.write(notes)
PYEOF
cat release_notes.md || echo "## [${{ steps.version.outputs.version }}]" > release_notes.md
- name: Create GitHub Release
if: steps.version.outputs.is_dev == 'false' && steps.version.outputs.changelog_match == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.version.outputs.version }}
name: v${{ steps.version.outputs.version }}
body_path: release_notes.md
draft: false
prerelease: false
- name: Set up QEMU
if: steps.version.outputs.is_dev == 'true' || steps.version.outputs.changelog_match == 'true'
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
if: steps.version.outputs.is_dev == 'true' || steps.version.outputs.changelog_match == 'true'
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
if: steps.version.outputs.is_dev == 'true' || steps.version.outputs.changelog_match == 'true'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate Docker tags
if: steps.version.outputs.is_dev == 'true' || steps.version.outputs.changelog_match == 'true'
id: docker-tags
run: |
VERSION="${{ steps.version.outputs.version }}"
REPO="ghcr.io/$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')"
if [ "${{ steps.version.outputs.is_dev }}" = "true" ]; then
TAGS="${REPO}:${VERSION},${REPO}:dev"
else
TAGS="${REPO}:${VERSION},${REPO}:latest"
fi
echo "tags=$TAGS" >> "$GITHUB_OUTPUT"
- name: Build and push Docker image
if: steps.version.outputs.is_dev == 'true' || steps.version.outputs.changelog_match == 'true'
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.docker-tags.outputs.tags }}
labels: |
org.opencontainers.image.version=${{ steps.version.outputs.version }}