-
Notifications
You must be signed in to change notification settings - Fork 1
133 lines (117 loc) · 4.36 KB
/
Copy pathversion-and-release.yml
File metadata and controls
133 lines (117 loc) · 4.36 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
132
133
name: Auto Version and Release
on:
push:
branches: [ "main" ]
pull_request:
types: [ closed ]
branches: [ "main" ]
jobs:
version-and-release:
if: github.event_name == 'push' || (github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main')
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Get current version
id: current_version
run: |
VERSION=$(python3 -c "from version import get_version; print(get_version())")
echo "current_version=$VERSION" >> $GITHUB_OUTPUT
echo "Current version: $VERSION"
- name: Increment version
id: new_version
run: |
# Get the current version and increment patch number
CURRENT_VERSION="${{ steps.current_version.outputs.current_version }}"
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "New version: $NEW_VERSION"
- name: Update version in code
run: |
NEW_VERSION="${{ steps.new_version.outputs.new_version }}"
sed -i "s/__version__ = \".*\"/__version__ = \"$NEW_VERSION\"/" version.py
echo "Updated version.py with version $NEW_VERSION"
- name: Generate changelog
id: changelog
run: |
# Get the latest tag
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LATEST_TAG" ]; then
echo "No previous tag found, getting all commits"
COMMITS=$(git log --oneline --pretty=format:"- %s" HEAD)
else
echo "Getting commits since $LATEST_TAG"
COMMITS=$(git log --oneline --pretty=format:"- %s" $LATEST_TAG..HEAD)
fi
# Create changelog
{
echo "## Changes in v${{ steps.new_version.outputs.new_version }}"
echo ""
if [ -n "$COMMITS" ]; then
echo "$COMMITS"
else
echo "- Minor updates and improvements"
fi
} > CHANGELOG.md
# Set output for release notes
{
echo 'changelog<<EOF'
cat CHANGELOG.md
echo EOF
} >> $GITHUB_OUTPUT
- name: Commit version update
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add version.py
git commit -m "Bump version to v${{ steps.new_version.outputs.new_version }}" || echo "No changes to commit"
git push
- name: Create Git Tag
run: |
git tag "v${{ steps.new_version.outputs.new_version }}"
git push origin "v${{ steps.new_version.outputs.new_version }}"
- name: Create GitHub Release
uses: ncipollo/release-action@v1
with:
tag: v${{ steps.new_version.outputs.new_version }}
name: Release v${{ steps.new_version.outputs.new_version }}
body: ${{ steps.changelog.outputs.changelog }}
draft: false
prerelease: false
token: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to the GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=raw,value=latest
type=raw,value=v${{ steps.new_version.outputs.new_version }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}