Skip to content

Refactor

Refactor #192

Workflow file for this run

name: Create Release
on:
push:
# Trigger on pushes to main or master branch
branches: [ main, master ]
# Allow manual triggering
workflow_dispatch:
permissions:
contents: write
jobs:
check-and-release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Get latest version
id: get_version
run: |
VERSION=$(grep -oP "Version: \K[0-9]+\.[0-9]+\.[0-9]+" enginescript-site-optimizer.php)
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Found version: $VERSION"
- name: Check if release exists
id: check_release
run: |
RELEASE_EXISTS=$(curl -s -o /dev/null -w "%{http_code}" https://api.github.com/repos/${{ github.repository }}/releases/tags/v${{ steps.get_version.outputs.version }})
if [[ "$RELEASE_EXISTS" == "200" ]]; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "Release v${{ steps.get_version.outputs.version }} already exists"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "Release v${{ steps.get_version.outputs.version }} does not exist yet"
fi
- name: Create zip file
if: steps.check_release.outputs.exists == 'false'
run: |
PLUGIN_SLUG="enginescript-site-optimizer"
rm -rf "$PLUGIN_SLUG"
mkdir -p "$PLUGIN_SLUG"
cp enginescript-site-optimizer.php "$PLUGIN_SLUG/"
cp -r includes "$PLUGIN_SLUG/"
cp readme.txt "$PLUGIN_SLUG/"
cp README.md "$PLUGIN_SLUG/"
cp CHANGELOG.md "$PLUGIN_SLUG/"
cp LICENSE "$PLUGIN_SLUG/" || echo "No LICENSE file found"
if [ -d languages ]; then
cp -r languages "$PLUGIN_SLUG/"
fi
zip -r "${PLUGIN_SLUG}-${{ steps.get_version.outputs.version }}.zip" "$PLUGIN_SLUG"
- name: Get changelog entry
if: steps.check_release.outputs.exists == 'false'
id: get_changelog
run: |
CHANGELOG_ENTRY=$(awk -v ver="${{ steps.get_version.outputs.version }}" 'BEGIN{flag=0; target="^## \\[?" ver "\\]?([[:space:]-]|$)"} $0 ~ target {flag=1; print; next} /^## \[?[0-9]+\.[0-9]+\.[0-9]+\]?([[:space:]-]|$)/ {flag=0} flag {print}' CHANGELOG.md | tail -n +2)
echo "changelog<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG_ENTRY" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Release
if: steps.check_release.outputs.exists == 'false'
uses: softprops/action-gh-release@v3
with:
tag_name: v${{ steps.get_version.outputs.version }}
name: Release v${{ steps.get_version.outputs.version }}
body: |
${{ steps.get_changelog.outputs.changelog }}
## Installation
1. Download the zip file
2. Upload to your WordPress site through the Plugins > Add New > Upload menu
3. Activate the plugin
[Full Documentation](https://github.com/${{ github.repository }})
files: enginescript-site-optimizer-${{ steps.get_version.outputs.version }}.zip
draft: false
prerelease: false
generate_release_notes: false
- name: Update README version
if: steps.check_release.outputs.exists == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ steps.get_version.outputs.version }}
run: |
README_FILE="README.md"
TMP_FILE=$(mktemp)
# Update the version badge with the new version and logo
sed -E 's#\[!\[Version\]\(https://img\.shields\.io/badge/Version-[0-9]+\.[0-9]+\.[0-9]+-orange\.svg\?logo=github\)\]\(https://github\.com/EngineScript/enginescript-site-optimizer/releases/download/v[0-9]+\.[0-9]+\.[0-9]+/enginescript-site-optimizer-[0-9]+\.[0-9]+\.[0-9]+\.zip\)#[![Version](https://img.shields.io/badge/Version-'"$VERSION"'-orange.svg?logo=github)](https://github.com/EngineScript/enginescript-site-optimizer/releases/download/v'"$VERSION"'/enginescript-site-optimizer-'"$VERSION"'.zip)#g' "$README_FILE" > "$TMP_FILE"
# Replace file if changes were made
if ! cmp -s "$README_FILE" "$TMP_FILE"; then
cp "$TMP_FILE" "$README_FILE"
echo "Updated README.md with version $VERSION"
# Configure git
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# Commit and push the changes
git add "$README_FILE"
git commit -m "docs: update README.md version to $VERSION [skip ci]"
git push https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git
echo "::notice::README.md updated with latest version $VERSION"
else
echo "::notice::README.md already has the correct version"
fi
# Clean up temporary file
rm "$TMP_FILE"