Drafting a release with a script

Continuing the discussion from Link to latest plugin release:

This is the script I use to create the zip file and draft a release on GitHub.
Don’t consider it good, safe or anything else, it’s just something working good for me…

#!/usr/bin/env bash
set -e

dir=`pwd`

slug=`basename $dir`

phpfile="${slug}.php"

version=`wp --allow-root eval '$v = get_plugin_data( "'${phpfile}'" ); echo $v["Version"];'`

readmeversion=`wp --allow-root eval '$v = get_plugin_data( "readme.txt" ); echo $v["Version"];'`

downloadlink=`cat readme.txt | grep -E '^Download link:' | \
	grep -oE 'download/[^/]+/' | \
	grep -oE '/[^/]+/'`

downloadversion="${downloadlink:1:${#downloadlink}-2}"

echo "Download link version : ${downloadversion}"
echo "Readme.txt version    : v${readmeversion}"
echo "Going to release      : v${version}"

read -n 1 -s -r -p "If OK, press any key to continue (CTRL-C to exit)."
echo

git archive -o "../${slug}.zip" HEAD

hub release create -d -a "../${slug}.zip" -m "v${version}" "v${version}"

rm "../${slug}.zip"

Edit:
If you don’t want some files to go in the zip file (GitHub things,…) you can create a file named .gitattributes.
Example content:

.github export-ignore
.gitattributes export-ignore
5 Likes

You’ll want to add something like this to the beginning of the script:

cd "$(dirname "$0")"

Or if your script lives inside a subfolder of your plugin, like bin/build-plugin.sh:

cd "$(dirname "$0")"
cd ..

This will ensure that the script can be run from any directory without causing errors.

The next thing you will probably want to do is check, at the beginning of the script, for any required programs. This will avoid aborted script runs with unhelpful error messages later on in the process:

for p in wp git hub; do
    if ! type -t $p > /dev/null; then
        echo "Program/function \`$p\` not found, make sure it is installed!" >&2
        exit 1
    fi
done

Finally I would recommend not trying to make scripts like this generic for any plugin. It usually ends up a lot simpler if you just write the script specifically for your current plugin. As one example of why, it looks like this script requires the plugin’s folder to be part of a live site, but this probably shouldn’t be required in order to build a plugin.

Here’s the script we use to release new versions of the migration plugin: ClassicPress-Migration-Plugin/bin/build-plugin-zip.sh at f1b0f23b3f55401299e2ef11ed697bca98995b01 · ClassicPress/ClassicPress-Migration-Plugin · GitHub

It is very simple. Using hub to create the release automatically would be a good addition, and also making sure the zip program exists beforehand would be a good idea.

I did not know about this! I wonder if it also works when you let GitHub build your zip files for you…

3 Likes

Thanks @james for your suggestions… I’ll work them out!

I know it is not the better way to do those things, but for now I’m developing plugins on a “live” (or better call it a test) site and so the environment is always inside a running ClassicPress on the same VPS.

.gitattributes is a nice piece of git that can do (but I’ve not deeply studied this) many nice things, like fixing linefeeds and others. When you “download ZIP” from GitHub it works.

3 Likes

This is totally fine, it just shouldn’t be required in order to build the plugin!

3 Likes