1+ name : Auto Version and Release
2+
3+ on :
4+ push :
5+ branches : [ "main" ]
6+ pull_request :
7+ types : [ closed ]
8+ branches : [ "main" ]
9+
10+ jobs :
11+ version-and-release :
12+ if : github.event_name == 'push' || (github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main')
13+ runs-on : ubuntu-latest
14+ permissions :
15+ contents : write
16+ packages : write
17+
18+ steps :
19+ - name : Checkout repository
20+ uses : actions/checkout@v4
21+ with :
22+ fetch-depth : 0
23+ token : ${{ secrets.GITHUB_TOKEN }}
24+
25+ - name : Set up Python
26+ uses : actions/setup-python@v4
27+ with :
28+ python-version : ' 3.11'
29+
30+ - name : Get current version
31+ id : current_version
32+ run : |
33+ VERSION=$(python3 -c "from version import get_version; print(get_version())")
34+ echo "current_version=$VERSION" >> $GITHUB_OUTPUT
35+ echo "Current version: $VERSION"
36+
37+ - name : Increment version
38+ id : new_version
39+ run : |
40+ # Get the current version and increment patch number
41+ CURRENT_VERSION="${{ steps.current_version.outputs.current_version }}"
42+ IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
43+ MAJOR=${VERSION_PARTS[0]}
44+ MINOR=${VERSION_PARTS[1]}
45+ PATCH=${VERSION_PARTS[2]}
46+ NEW_PATCH=$((PATCH + 1))
47+ NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
48+ echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
49+ echo "New version: $NEW_VERSION"
50+
51+ - name : Update version in code
52+ run : |
53+ NEW_VERSION="${{ steps.new_version.outputs.new_version }}"
54+ sed -i "s/__version__ = \".*\"/__version__ = \"$NEW_VERSION\"/" version.py
55+ echo "Updated version.py with version $NEW_VERSION"
56+
57+ - name : Generate changelog
58+ id : changelog
59+ run : |
60+ # Get the latest tag
61+ LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
62+ if [ -z "$LATEST_TAG" ]; then
63+ echo "No previous tag found, getting all commits"
64+ COMMITS=$(git log --oneline --pretty=format:"- %s" HEAD)
65+ else
66+ echo "Getting commits since $LATEST_TAG"
67+ COMMITS=$(git log --oneline --pretty=format:"- %s" $LATEST_TAG..HEAD)
68+ fi
69+
70+ # Create changelog
71+ {
72+ echo "## Changes in v${{ steps.new_version.outputs.new_version }}"
73+ echo ""
74+ if [ -n "$COMMITS" ]; then
75+ echo "$COMMITS"
76+ else
77+ echo "- Minor updates and improvements"
78+ fi
79+ } > CHANGELOG.md
80+
81+ # Set output for release notes
82+ {
83+ echo 'changelog<<EOF'
84+ cat CHANGELOG.md
85+ echo EOF
86+ } >> $GITHUB_OUTPUT
87+
88+ - name : Commit version update
89+ run : |
90+ git config --local user.email "action@github.com"
91+ git config --local user.name "GitHub Action"
92+ git add version.py
93+ git commit -m "Bump version to v${{ steps.new_version.outputs.new_version }}" || echo "No changes to commit"
94+ git push
95+
96+ - name : Create Git Tag
97+ run : |
98+ git tag "v${{ steps.new_version.outputs.new_version }}"
99+ git push origin "v${{ steps.new_version.outputs.new_version }}"
100+
101+ - name : Create GitHub Release
102+ uses : ncipollo/release-action@v1
103+ with :
104+ tag : v${{ steps.new_version.outputs.new_version }}
105+ name : Release v${{ steps.new_version.outputs.new_version }}
106+ body : ${{ steps.changelog.outputs.changelog }}
107+ draft : false
108+ prerelease : false
109+ token : ${{ secrets.GITHUB_TOKEN }}
110+
111+ - name : Log in to the GitHub Container Registry
112+ uses : docker/login-action@v3
113+ with :
114+ registry : ghcr.io
115+ username : ${{ github.actor }}
116+ password : ${{ secrets.GITHUB_TOKEN }}
117+
118+ - name : Extract metadata (tags, labels) for Docker
119+ id : meta
120+ uses : docker/metadata-action@v5
121+ with :
122+ images : ghcr.io/${{ github.repository }}
123+ tags : |
124+ type=raw,value=latest
125+ type=raw,value=v${{ steps.new_version.outputs.new_version }}
126+
127+ - name : Build and push Docker image
128+ uses : docker/build-push-action@v5
129+ with :
130+ context : .
131+ push : true
132+ tags : ${{ steps.meta.outputs.tags }}
133+ labels : ${{ steps.meta.outputs.labels }}
0 commit comments