Skip to content

Instantly share code, notes, and snippets.

@mackenly
Last active September 3, 2024 20:58
Show Gist options
  • Select an option

  • Save mackenly/0290612f0d705742ee77b4b419808470 to your computer and use it in GitHub Desktop.

Select an option

Save mackenly/0290612f0d705742ee77b4b419808470 to your computer and use it in GitHub Desktop.
GitHub Action for WordPress Themes

Overview

GitHub Action yml for WordPress themes that increments versions and creates zipped releases.

Config

  • Edit zip name in Zip Repo Contents
  • Edit zip name to match in Create Release
  • Edit THEME_VERSION to the name of your version constant. Assumes that there's a version constant within the functions.php file (used for things like script versioning).
  • Remove any functionality you don't need, such as the read me editor that adds change log notes.

Extending

  • Use a better system for determining the new version number
  • This assumes that the theme isn't nested in a directory
name: Theme Release Workflow
on:
push:
branches:
- main
jobs:
build-and-release:
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- name: Increment Version Number
run: |
STYLE_CSS="style.css"
FUNCTIONS_PHP="functions.php"
README_TXT="readme.txt"
README_MD="README.md"
STYLE_VERSION_LINE=$(grep -e 'Version:' $STYLE_CSS)
CURRENT_VERSION=$(echo $STYLE_VERSION_LINE | grep -oP '\d+\.\d+\.\d+')
FUNCTIONS_VERSION_LINE=$(grep -e "define('HELLO_ELEMENTOR_CHILD_VERSION'," $FUNCTIONS_PHP)
CURRENT_FUNCTIONS_VERSION=$(echo $FUNCTIONS_VERSION_LINE | grep -oP '\d+\.\d+\.\d+')
README_VERSION_LINE=$(grep -e 'Stable tag:' $README_TXT)
README_CURRENT_VERSION=$(echo $README_VERSION_LINE | grep -oP '\d+\.\d+\.\d+')
if [ "$CURRENT_VERSION" != "$CURRENT_FUNCTIONS_VERSION" ] || [ "$CURRENT_VERSION" != "$README_CURRENT_VERSION" ]; then
echo "Version mismatch between style.css, functions.php, and readme.txt"
exit 1
fi
IFS='.' read -ra VERSION <<< "$CURRENT_VERSION"
NEW_VERSION="${VERSION[0]}.$((VERSION[1]+1)).${VERSION[2]}"
sed -i "s/Version: $CURRENT_VERSION/Version: $NEW_VERSION/" $STYLE_CSS
sed -i "s/define('THEME_VERSION', '$CURRENT_FUNCTIONS_VERSION')/define('THEME_VERSION', '$NEW_VERSION')/" $FUNCTIONS_PHP
sed -i "s/Stable tag: $README_CURRENT_VERSION/Stable tag: $NEW_VERSION/" $README_TXT
sed -i "s/Version: $README_CURRENT_VERSION/Version: $NEW_VERSION/" $README_TXT
# Do Change Log
LATEST_TAG=$(git describe --tags --abbrev=0)
RELEASE_DATE=$(date +%Y-%m-%d)
COMMITS=$(git log $LATEST_TAG..HEAD --pretty=format:"* %s")
CHANGELOG_ENTRY="\ \n= $NEW_VERSION - $RELEASE_DATE =\n$COMMITS"
sed -i "/== Changelog ==/a $CHANGELOG_ENTRY" $README_TXT
git config user.name Version Bot
git config user.email version-bot@github.com
git add $STYLE_CSS $FUNCTIONS_PHP
git commit -m "Increment theme version number to $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
- name: Push Changes
run: |
git push origin main
- name: Zip Repo Contents
run: |
zip -r theme.zip . -x "*.git*" "*.gitattributes*" "*.github*" "*.gitignore*" "*.DS_Store*" "*.editorconfig*" "*.gitmodules*" "*.vscode*" "*.idea" "README.md"
- name: Push Tag
run: |
git tag ${{ env.NEW_VERSION }}
git push origin ${{ env.NEW_VERSION }}
- name: Create Release
uses: ncipollo/release-action@v1
with:
artifacts: theme.zip
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ env.NEW_VERSION }}
name: Release ${{ env.NEW_VERSION }}
body: |
Release ${{ env.NEW_VERSION }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment