Skip to content

Automate Versioning and Release #164

Automate Versioning and Release

Automate Versioning and Release #164

Workflow file for this run

name: Automate Versioning and Release
on:
schedule:
- cron: '0 0 * * *' # This runs every day at midnight
jobs:
check-for-changes: # check for any changes to "main" branch
runs-on: ubuntu-latest
outputs:
has_changes: ${{ steps.check_changes.outputs.has_changes }}
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
token: ${{ secrets.MACHINE_USER_PAT }}
- name: Check for changes since last release
id: check_changes
run: |
LATEST_RELEASE_DATE=$(curl --silent "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/latest" | jq -r .created_at)
git fetch origin main
LAST_COMMIT_DATE=$(git log -1 --pretty=format:"%cI" origin/main)
if [[ "$LAST_COMMIT_DATE" > "$LATEST_RELEASE_DATE" ]]; then
echo "::set-output name=has_changes::true"
else
echo "::set-output name=has_changes::false"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
release: # create a nightly release
needs: check-for-changes
if: needs.check-for-changes.outputs.has_changes == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
token: ${{ secrets.MACHINE_USER_PAT }}
- name: Install jq tool
run: |
sudo apt-get update
sudo apt-get install -y jq
- name: Bump version using Bash
run: |
# Extract base_version from the JSON
BASE_VERSION=$(jq -r '.base_version' package-info.json)
# Get today's date in the format YYMMDD
DATE=$(date -u +"%y%m%d")
# Extract the date and count from the version key
VERSION_DATE=$(jq -r '.version' package-info.json | cut -d'.' -f4)
VERSION_COUNT=$(jq -r '.version' package-info.json | cut -d'.' -f5)
# Determine the count
if [[ $DATE -gt $VERSION_DATE ]]; then
COUNT=1
else
COUNT=$((VERSION_COUNT+1))
fi
# Construct the new version
NEW_VERSION="${BASE_VERSION}.${DATE}.${COUNT}"
# Update the version in the JSON using jq
jq --arg v "$NEW_VERSION" '.version = $v' package-info.json > temp.json && mv temp.json package-info.json
# Extract the updated version
VERSION=$(jq -r '.version' package-info.json)
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Commit updated package-info.json
run: |
git config user.name "ppooll-machine"
git config user.email "ppoollmachine@gmail.com"
git add package-info.json
git commit -m "Bump version"
git remote set-url origin https://machine_user_name:${{ secrets.MACHINE_USER_PAT }}@github.com/ppooll-dev/ppooll.git
git push origin main
- name: Create ZIP Archive
run: |
# Create a temporary directory called 'ppooll'
mkdir ppooll
# Copy all repository files into 'ppooll' directory
rsync -av --progress --exclude=ppooll . ppooll/
# Create a ZIP archive
zip -r ppooll.zip ppooll/
- name: Install GitHub CLI
run: |
curl -sSL https://github.com/cli/cli/releases/download/v2.3.0/gh_2.3.0_linux_amd64.tar.gz | tar xz -C /tmp
sudo mv /tmp/gh_2.3.0_linux_amd64/bin/gh /usr/local/bin/
gh --version
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create $VERSION ppooll.zip --title "$VERSION" --notes "Automated release for $VERSION"
- name: Notify Discord
run: |
# Get the URL of the latest release
RELEASE_URL="https://github.com/$GITHUB_REPOSITORY/releases/latest"
# Construct the content for the Discord message
CONTENT="There's a new release! Check out version $VERSION [here]($RELEASE_URL)."
# Send a POST request to the Discord webhook
curl -H "Content-Type: application/json" \
-X POST \
-d "{\"content\": \"$CONTENT\"}" \
${{ secrets.DISCORD_WEBHOOK_URL }}