update-jar-and-release #101
This file contains 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: update-jar-and-release | |
on: | |
schedule: | |
- cron: '0 10 * * *' # Run daily at 5 AM EST | |
workflow_dispatch: # Allow manual triggering | |
jobs: | |
check-and-update-jar: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 # Fetch all history for all tags and branches | |
- name: Get current version from variables.tf | |
id: current-version | |
run: | | |
current_version=$(grep 'default.*JMusicBot-' variables.tf | sed -n 's/.*JMusicBot-\(.*\)\.jar.*/\1/p') | |
echo "Current version in variables.tf: $current_version" | |
echo "current_version=$current_version" >> $GITHUB_OUTPUT | |
- name: Check for new MusicBot release | |
id: check-release | |
run: | | |
latest_release=$(curl -s https://api.github.com/repos/jagrosh/MusicBot/releases/latest | jq -r .tag_name) | |
echo "Latest release: $latest_release" | |
echo "new_version=$latest_release" >> $GITHUB_OUTPUT | |
- name: Update JAR file | |
id: update-jar | |
run: | | |
current_version="${{ steps.current-version.outputs.current_version }}" | |
latest_version="${{ steps.check-release.outputs.new_version }}" | |
if [ "$latest_version" != "$current_version" ] && [ "$latest_version" != "null" ]; then | |
echo "New version available: $latest_version" | |
# Update variables.tf | |
sed -i 's/default\s*=\s*"JMusicBot-.*\.jar"/default = "JMusicBot-'$latest_version'.jar"/' variables.tf | |
git config user.name github-actions | |
git config user.email github-actions@github.com | |
git add variables.tf | |
git commit -m "Update JMusicBot version to $latest_version in variables.tf" | |
# Fetch the latest changes | |
git fetch origin master | |
# Attempt to rebase | |
if git rebase origin/master; then | |
# If rebase is successful, force push the changes | |
git push --force-with-lease | |
echo "updated=true" >> $GITHUB_OUTPUT | |
echo "new_version=$latest_version" >> $GITHUB_OUTPUT | |
else | |
# If rebase fails, undo the changes and exit | |
git rebase --abort | |
git reset --hard origin/master | |
echo "Failed to update due to conflicts. Please check the repository manually." | |
echo "updated=false" >> $GITHUB_OUTPUT | |
exit 1 | |
fi | |
else | |
echo "No new version available or unable to fetch latest version. Current version is up to date." | |
echo "updated=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Get next version number | |
id: get_next_version | |
if: steps.update-jar.outputs.updated == 'true' | |
run: | | |
# Get all tags, sort them, and get the latest one | |
latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") | |
# Remove 'v' prefix if present | |
latest_version=${latest_tag#v} | |
# Split the version into parts | |
IFS='.' read -r -a version_parts <<< "$latest_version" | |
# Increment the patch version | |
new_patch=$((${version_parts[2]:-0} + 1)) | |
new_version="${version_parts[0]:-0}.${version_parts[1]:-0}.$new_patch" | |
echo "Next version: v$new_version" | |
echo "next_version=v$new_version" >> $GITHUB_OUTPUT | |
- name: Create Release | |
if: steps.update-jar.outputs.updated == 'true' | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
tag_name: ${{ steps.get_next_version.outputs.next_version }} | |
release_name: Release ${{ steps.get_next_version.outputs.next_version }} | |
body: | | |
Updated to JMusicBot version ${{ steps.update-jar.outputs.new_version }} | |
draft: false | |
prerelease: false |