From 85469212fdf0daa855d3018dd91fc2051d72a611 Mon Sep 17 00:00:00 2001 From: Yuri V Date: Wed, 30 Oct 2024 17:22:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Add=20auto-release=20workflow=20?= =?UTF-8?q?for=20GitHub=20Actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✨ Implemented a new GitHub Actions workflow for automatic releases: - 📅 Triggers on push of version tags (v*) - 🏷️ Creates/updates a base version tag and release - 📝 Generates changelog from commits since last tag - 🔄 Updates existing base release or creates a new one - 📦 Creates a full version release with detailed changelog - 🔒 Configures appropriate permissions for release creation --- .github/workflows/auto-release.yml | 124 +++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 .github/workflows/auto-release.yml diff --git a/.github/workflows/auto-release.yml b/.github/workflows/auto-release.yml new file mode 100644 index 0000000..2e5aff4 --- /dev/null +++ b/.github/workflows/auto-release.yml @@ -0,0 +1,124 @@ +name: Auto Release + +on: + push: + tags: + - 'v*' + +permissions: + contents: write + +jobs: + create-release: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Get tag name + id: get_tag + shell: bash + run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT + + - name: Get base version + id: get_base_version + shell: bash + run: | + BASE_VERSION=$(echo "${{ steps.get_tag.outputs.TAG }}" | cut -d. -f1) + echo "BASE_VERSION=${BASE_VERSION}" >> $GITHUB_OUTPUT + + - name: Get previous tag + id: get_previous_tag + shell: bash + run: | + PREVIOUS_TAG=$(git describe --tags --abbrev=0 "${{ steps.get_tag.outputs.TAG }}"^ || echo "") + echo "PREVIOUS_TAG=${PREVIOUS_TAG}" >> $GITHUB_OUTPUT + + - name: Generate changelog + id: generate_changelog + shell: bash + run: | + if [ -n "${{ steps.get_previous_tag.outputs.PREVIOUS_TAG }}" ]; then + CHANGELOG=$(git log --pretty=format:"- %B" "${{ steps.get_previous_tag.outputs.PREVIOUS_TAG }}..${{ steps.get_tag.outputs.TAG }}") + else + CHANGELOG=$(git log --pretty=format:"- %B" "${{ steps.get_tag.outputs.TAG }}") + fi + # Remove any trailing newlines and add one at the end + CHANGELOG=$(echo "$CHANGELOG" | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n\+$/\n/') + echo "CHANGELOG<> $GITHUB_OUTPUT + echo "$CHANGELOG" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Create or update base version tag + shell: bash + run: | + git config user.name "${{ github.actor }}" + git config user.email "${{ github.actor }}@users.noreply.github.com" + git tag -f "${{ steps.get_base_version.outputs.BASE_VERSION }}" "${GITHUB_SHA}" + git push origin --force "${{ steps.get_base_version.outputs.BASE_VERSION }}" + + - name: Create or Update Base Release + uses: actions/github-script@v6 + env: + CHANGELOG: ${{ toJSON(steps.generate_changelog.outputs.CHANGELOG) }} + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const baseVersion = `${{ steps.get_base_version.outputs.BASE_VERSION }}`; + const fullVersion = `${{ steps.get_tag.outputs.TAG }}`; + const changelog = JSON.parse(process.env.CHANGELOG); + const owner = context.repo.owner; + const repo = context.repo.repo; + const tagName = baseVersion; + + try { + // Try to get the existing release + const { data: release } = await github.rest.repos.getReleaseByTag({ + owner, + repo, + tag: tagName + }); + + // If it exists, update it + await github.rest.repos.updateRelease({ + owner, + repo, + release_id: release.id, + tag_name: tagName, + name: `Base Release ${baseVersion}`, + body: `This is the base release for ${baseVersion}. Latest version: ${fullVersion}\n\nChangelog:\n${changelog}`, + draft: false, + prerelease: false + }); + } catch (error) { + if (error.status === 404) { + // If it doesn't exist, create it + await github.rest.repos.createRelease({ + owner, + repo, + tag_name: tagName, + name: `Base Release ${baseVersion}`, + body: `This is the base release for ${baseVersion}. Latest version: ${fullVersion}\n\nChangelog:\n${changelog}`, + draft: false, + prerelease: false + }); + } else { + throw error; + } + } + + - name: Create Full Version Release + uses: softprops/action-gh-release@v1 + with: + tag_name: ${{ steps.get_tag.outputs.TAG }} + name: Release ${{ steps.get_tag.outputs.TAG }} + body: | + Changes in this Release: + ${{ steps.generate_changelog.outputs.CHANGELOG }} + draft: false + prerelease: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}