Software Developer

Bash Script to Release WordPress Plugin to GitHub

This is the bash script that I currently use to release WordPress plugins to GitHub so that users can get automatic updates in their WordPress admin.

This bash script will create a new release on GitHub for your WordPress plugin. It creates an actual release, not just a “tag.” This script also extracts the changelog from your standard WordPress readme.txt file. It will use that changelog text as the notes for the release on GitHub.

The extraction of the changelog from the readme.txt only works if the readme only has the header section and the changelog section of a standard WordPress readme.txt, like in this example. So, it will not work if your readme.txt has other sections, such as the Description, Installation, etc.

In addition, this script also makes a separate backup of the whole thing, including hidden files and phpunit tests. Anything inside the current directory. You can specify the path where you want to place this backup development .zip on line 13.

This works “as is” if your main plugin file slug is the same as the plugin folder name. And, if the git repo also has the same name. Otherwise, you can just modify the variables at the top of the script. For example, maybe you need to add a REPO_NAME variable if your GitHub repository has a different name than the plugin slug. Then, you would use REPO_NAME instead of SLUG on line 47.

You must add your GitHub user name and access token on lines 14 and 15.

#!/bin/bash
# Create a release on GitHub.
# Extracts the most recent changelog section from readme.txt
# if readme.txt is in standard WordPress format.
# Also makes a separate backup of the whole thing including hidden files and phpunit tests.
# Prerequisite: Main plugin file slug must be the same as the plugin folder name.
# Prerequisite: Existing git repo with its remote origin set up on GitHub. Both repo names must match the plugin slug, exactly.
# Configure the first few variables.

set -e

#config
BACKUPDIR="${HOME}/Documents/LOG/" # destination folder for the backup development .zip
GITUSER="username" # GitHub user name
ACCESSTOKEN="000000000000000000000000000000000" #GitHub access token

SLUG=${PWD##*/}
CURRENTDIR=`pwd`
MAINFILE="${SLUG}.php"
timestamp=$(date +%Y%m%d_%H%M%S) # +%Y%m%d_%H%M%S

# Get version from main plugin file
NEWVERSION=`grep "^Version" "$CURRENTDIR/${SLUG}.php" | awk -F' ' '{print $2}' | sed 's/[[:space:]]//g'`
if [[ -z "$NEWVERSION" ]]; then echo "ERROR: Cannot find version. Exiting early...."; exit 1; fi

STABLEVERSION=`grep "^Stable tag" "$CURRENTDIR/readme.txt" | awk -F' ' '{print $3}' | sed 's/[[:space:]]//g'`

echo "readme version: $STABLEVERSION"
echo "$MAINFILE version: $NEWVERSION"

if [ "$STABLEVERSION" != "$NEWVERSION" ]; then echo "Versions don't match. Exiting...."; exit 1; fi

echo "Versions match in readme.txt and PHP file. Let's proceed..."

# Create a Release on GitHub

echo "Creating a new release on GitHub"
# Get changelog text from readme
BEGINLINE=`awk '/Changelog/{ print NR; exit }' "$CURRENTDIR/readme.txt"`
BEGINLINE=$((BEGINLINE+1)) 
ENDLINE=`grep -n '= ' "$CURRENTDIR/readme.txt"| sed -n '4 s/:.*//p'`
ENDLINE=$((ENDLINE-1)) 
CHANGELOG=`sed -n -e "${BEGINLINE},${ENDLINE}p" "$CURRENTDIR/readme.txt"`
CHANGELOG_JSON="${CHANGELOG//$'\n'/'\n'}"

API_JSON=$(printf '{"tag_name": "%s","target_commitish": "master","name": "%s","body": "%s","draft": false,"prerelease": false}' $STABLEVERSION $STABLEVERSION "$CHANGELOG_JSON")
curl --data "$API_JSON" https://api.github.com/repos/${GITUSER}/${SLUG}/releases?access_token=${ACCESSTOKEN}

# zip a backup of everything

cd ../
zip -r ${SLUG}.${timestamp}.zip $SLUG
echo "Moving the backup out to $BACKUPDIR"
mv ${SLUG}.${timestamp}.zip $BACKUPDIR

echo "*** FIN ***"

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]