update-jar-and-release #14
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 5 * * *' # Run daily at 5 AM UTC | |
workflow_dispatch: # Allow manual triggers | |
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: Check for existing JAR and get current version | |
id: current-jar | |
run: | | |
mkdir -p jdiscordmusicbot | |
current_jar=$(ls jdiscordmusicbot/JMusicBot-*.jar 2>/dev/null | sort -V | tail -n 1 || echo "") | |
if [ -z "$current_jar" ]; then | |
echo "No existing JAR found." | |
echo "current_version=0.0.0" >> $GITHUB_OUTPUT | |
else | |
current_version=$(echo "$current_jar" | sed -n 's/.*JMusicBot-\(.*\)\.jar/\1/p') | |
echo "Current JAR version: $current_version" | |
echo "current_version=$current_version" >> $GITHUB_OUTPUT | |
fi | |
- 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-jar.outputs.current_version }}" | |
latest_version="${{ steps.check-release.outputs.new_version }}" | |
if [ "$latest_version" != "$current_version" ]; then | |
echo "New version available: $latest_version" | |
wget https://github.com/jagrosh/MusicBot/releases/download/$latest_version/JMusicBot-$latest_version.jar -O jdiscordmusicbot/JMusicBot-$latest_version.jar | |
git config user.name github-actions | |
git config user.email github-actions@github.com | |
git add jdiscordmusicbot/JMusicBot-$latest_version.jar | |
git commit -m "Add JMusicBot version $latest_version" | |
git push | |
echo "updated=true" >> $GITHUB_OUTPUT | |
echo "new_version=$latest_version" >> $GITHUB_OUTPUT | |
else | |
echo "No new version available. Current version is up to date." | |
echo "updated=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Update variables.tf | |
if: steps.update-jar.outputs.updated == 'true' | |
run: | | |
if [ -f variables.tf ]; then | |
if grep -q "default\s*=\s*\"JMusicBot-${{ steps.check-release.outputs.new_version }}.jar\"" variables.tf; then | |
echo "variables.tf already contains the latest version. No update needed." | |
else | |
sed -i 's/default\s*=\s*"JMusicBot-.*\.jar"/default = "JMusicBot-${{ steps.check-release.outputs.new_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 default JAR version in variables.tf" | |
git push | |
echo "variables.tf updated successfully." | |
fi | |
else | |
echo "variables.tf not found. Skipping update." | |
fi | |
- name: Exit if no new release found | |
if: steps.update-jar.outputs.updated == 'false' | |
run: | | |
echo "No new JMusicBot version found. Exiting workflow." | |
exit 0 | |
- name: Get latest release version | |
id: get_latest_release | |
run: | | |
latest_release=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .tag_name || echo "v0.0.0") | |
latest_release=${latest_release#v} # Remove 'v' prefix if present | |
IFS='.' read -r -a version_parts <<< "$latest_release" | |
new_patch=$((version_parts[2] + 1)) | |
new_version="${version_parts[0]}.${version_parts[1]}.$new_patch" | |
echo "new_version=$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: v${{ steps.get_latest_release.outputs.new_version }} | |
release_name: Release ${{ steps.get_latest_release.outputs.new_version }} | |
body: | | |
Updated to JMusicBot version ${{ steps.check-release.outputs.new_version }} | |
draft: false | |
prerelease: false |