Merge pull request #11 from cancel-cloud/copilot/fix-10 #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto Version and Release | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| jobs: | |
| version-and-release: | |
| 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 pull --rebase origin main | |
| 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 }} |