From b64b57da98c3c3fb71a554ba9825de4987250961 Mon Sep 17 00:00:00 2001 From: ronantakizawa Date: Tue, 27 Aug 2024 14:24:51 -0600 Subject: [PATCH 1/2] automate binary release for every change in CHANGELOG.md --- .github/workflows/release.yml | 69 +++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..790547d04 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,69 @@ +name: Auto Release on Changelog Change + +on: + push: + branches: + - main + +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Rust + uses: dtolnay/rust-toolchain@stable + + - name: Check for changelog changes + id: changelog_check + run: | + # Get the latest tag + latest_tag=$(git describe --tags --abbrev=0) + # Get the commit hash of the latest tag + latest_commit=$(git rev-list -n 1 $latest_tag) + # Compare changelog file + if git diff --name-only $latest_commit HEAD | grep -q '^CHANGELOG.md$'; then + echo "CHANGELOG.md has been updated." + echo "::set-output name=needs_release::true" + else + echo "No changes in CHANGELOG.md." + echo "::set-output name=needs_release::false" + fi + + - name: Generate New Version Tag + if: steps.changelog_check.outputs.needs_release == 'true' + id: generate_version_tag + run: | + # Get the latest version tag + latest_tag=$(git describe --tags --abbrev=0) + # Increment version number + major=$(echo $latest_tag | cut -d. -f1 | tr -d 'v') + minor=$(echo $latest_tag | cut -d. -f2) + patch=$(echo $latest_tag | cut -d. -f3) + new_patch=$((patch + 1)) + new_version="v${major}.${minor}.${new_patch}" + echo "New version tag: $new_version" + echo "::set-output name=version_tag::$new_version" + + - name: Build + if: steps.changelog_check.outputs.needs_release == 'true' + run: cargo build --release + + - name: Install GitHub CLI + if: steps.changelog_check.outputs.needs_release == 'true' + run: sudo apt-get install gh + + - name: Authenticate GitHub CLI + if: steps.changelog_check.outputs.needs_release == 'true' + run: gh auth login --with-token < ${{ secrets.GITHUB_TOKEN }} + + - name: Create Release + if: steps.changelog_check.outputs.needs_release == 'true' + run: | + gh release create ${{ steps.generate_version_tag.outputs.version_tag }} target/release/noname --title "Release ${{ steps.generate_version_tag.outputs.version_tag }}" + + - name: Upload assets + if: steps.changelog_check.outputs.needs_release == 'true' + run: | + gh release upload ${{ steps.generate_version_tag.outputs.version_tag }} target/release/noname --clobber From a6708d53bec507b10d105682bae9d732bd1c2c22 Mon Sep 17 00:00:00 2001 From: ronantakizawa Date: Tue, 3 Sep 2024 08:09:18 -0600 Subject: [PATCH 2/2] fix: build if the newly parsed version doesnt exist --- .github/workflows/release.yml | 69 ++++++++++++++++------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 790547d04..43c1632b2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,55 +15,50 @@ jobs: - name: Set up Rust uses: dtolnay/rust-toolchain@stable - - name: Check for changelog changes - id: changelog_check + - name: Parse latest version from changelog + id: parse_version run: | - # Get the latest tag - latest_tag=$(git describe --tags --abbrev=0) - # Get the commit hash of the latest tag - latest_commit=$(git rev-list -n 1 $latest_tag) - # Compare changelog file - if git diff --name-only $latest_commit HEAD | grep -q '^CHANGELOG.md$'; then - echo "CHANGELOG.md has been updated." - echo "::set-output name=needs_release::true" - else - echo "No changes in CHANGELOG.md." - echo "::set-output name=needs_release::false" - fi + # Extract the latest version from the changelog + latest_version=$(grep -oP '## \[\K[0-9]+\.[0-9]+\.[0-9]+' CHANGELOG.md | head -n 1) + echo "Latest version from changelog: $latest_version" + echo "::set-output name=latest_version::$latest_version" - - name: Generate New Version Tag - if: steps.changelog_check.outputs.needs_release == 'true' - id: generate_version_tag + - name: Get the latest release version + id: get_latest_release run: | - # Get the latest version tag - latest_tag=$(git describe --tags --abbrev=0) - # Increment version number - major=$(echo $latest_tag | cut -d. -f1 | tr -d 'v') - minor=$(echo $latest_tag | cut -d. -f2) - patch=$(echo $latest_tag | cut -d. -f3) - new_patch=$((patch + 1)) - new_version="v${major}.${minor}.${new_patch}" - echo "New version tag: $new_version" - echo "::set-output name=version_tag::$new_version" + latest_release=$(gh release list --limit 1 | grep -oP '^v\K[0-9]+\.[0-9]+\.[0-9]+') + echo "Latest release version: $latest_release" + echo "::set-output name=latest_release::$latest_release" - - name: Build - if: steps.changelog_check.outputs.needs_release == 'true' + - name: Determine if a new release is needed + id: check_release + run: | + if [ "${{ steps.parse_version.outputs.latest_version }}" == "${{ steps.get_latest_release.outputs.latest_release }}" ]; then + echo "No new version to release." + echo "::set-output name=needs_release::false" + else + echo "New version found. Release needed." + echo "::set-output name=needs_release::true" + fi + + - name: Build the Project + if: steps.check_release.outputs.needs_release == 'true' run: cargo build --release - name: Install GitHub CLI - if: steps.changelog_check.outputs.needs_release == 'true' + if: steps.check_release.outputs.needs_release == 'true' run: sudo apt-get install gh - name: Authenticate GitHub CLI - if: steps.changelog_check.outputs.needs_release == 'true' - run: gh auth login --with-token < ${{ secrets.GITHUB_TOKEN }} + if: steps.check_release.outputs.needs_release == 'true' + run: echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token - name: Create Release - if: steps.changelog_check.outputs.needs_release == 'true' + if: steps.check_release.outputs.needs_release == 'true' run: | - gh release create ${{ steps.generate_version_tag.outputs.version_tag }} target/release/noname --title "Release ${{ steps.generate_version_tag.outputs.version_tag }}" - + gh release create v${{ steps.parse_version.outputs.latest_version }} target/release/noname --title "Release v${{ steps.parse_version.outputs.latest_version }}" + - name: Upload assets - if: steps.changelog_check.outputs.needs_release == 'true' + if: steps.check_release.outputs.needs_release == 'true' run: | - gh release upload ${{ steps.generate_version_tag.outputs.version_tag }} target/release/noname --clobber + gh release upload v${{ steps.parse_version.outputs.latest_version }} target/release/noname --clobber