Skip to content

Instantly share code, notes, and snippets.

@idokd
Last active October 10, 2019 12:41
Show Gist options
  • Select an option

  • Save idokd/1e87bd0ac0b025ca87abbb9bf96f61af to your computer and use it in GitHub Desktop.

Select an option

Save idokd/1e87bd0ac0b025ca87abbb9bf96f61af to your computer and use it in GitHub Desktop.
Deploy a Github Wordpress Plugin to Wordpress.org subversion using Google Cloud Build triggers
steps:
- name: 'debian:stable-slim'
args: ['bash', './deploy-plugin.sh']
env:
- 'BUILD=$BUILD_ID'
- 'PROJECT=$PROJECT_ID'
- 'REV=$REVISION_ID'
- 'VERSION=$TAG_NAME'
- 'SVN_USERNAME=$_SVN_USERNAME'
- 'SVN_PASSWORD=$_SVN_PASSWORD'
- 'SLUG=$_SLUG'
- 'REPOSITORY_URL=$_REPOSITORY_URL'
# Uses the docker build step to build an image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA', '.']
# image is pushed to Container Registry
images:
- 'gcr.io/$PROJECT_ID/$REPO_NAME'
#!/bin/bash
# Install necessary packages: subversion rsync git
apt-get update \
&& apt-get install -y subversion rsync git \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* \
&& git config --global user.email "10upbot+github@10up.com" \
&& git config --global user.name "10upbot on GitHub"
# Note that this does not use pipefail
# because if the grep later doesn't match any deleted files,
# which is likely the majority case,
# it does not exit with a 0, and I only care about the final exit.
set -eo
mkdir /github
GITHUB_WORKSPACE="/github/repos"
echo "Repository: $REPOSITORY_URL"
git clone $REPOSITORY_URL $GITHUB_WORKSPACE
GITHUB_REF="$GITHUB_WORKSPACE/.git"
echo "git ref: $GITHUB_REF"
# Ensure SVN username and password are set
# IMPORTANT: while secrets are encrypted and not viewable in the GitHub UI,
# they are by necessity provided as plaintext in the context of the Action,
# so do not echo or use debug mode unless you want your secrets exposed!
if [[ -z "$SVN_USERNAME" ]]; then
echo "Set the SVN_USERNAME secret"
exit 1
fi
if [[ -z "$SVN_PASSWORD" ]]; then
echo "Set the SVN_PASSWORD secret"
exit 1
fi
# Allow some ENV variables to be customized
if [[ -z "$SLUG" ]]; then
SLUG=${GITHUB_REPOSITORY#*/}
fi
echo "ℹ︎ SLUG is $SLUG"
# Does it even make sense for VERSION to be editable in a workflow definition?
if [[ -z "$VERSION" ]]; then
VERSION=${GITHUB_REF#refs/tags/}
fi
echo "ℹ︎ VERSION is $VERSION"
if [[ -z "$ASSETS_DIR" ]]; then
ASSETS_DIR=".wordpress-org"
fi
echo "ℹ︎ ASSETS_DIR is $ASSETS_DIR"
SVN_URL="http://plugins.svn.wordpress.org/${SLUG}/"
SVN_DIR="/github/svn-${SLUG}"
# Checkout just trunk and assets for efficiency
# Tagging will be handled on the SVN level
echo "➤ Checking out .org repository..."
svn checkout --depth immediates "$SVN_URL" "$SVN_DIR"
cd "$SVN_DIR"
svn update --set-depth infinity assets
svn update --set-depth infinity trunk
echo "➤ Copying files..."
if [[ -e "$GITHUB_WORKSPACE/.distignore" ]]; then
echo "ℹ︎ Using .distignore"
# Copy from current branch to /trunk, excluding dotorg assets
# The --delete flag will delete anything in destination that no longer exists in source
rsync -rc --exclude-from="$GITHUB_WORKSPACE/.distignore" "$GITHUB_WORKSPACE/" trunk/ --delete
else
echo "ℹ︎ Using .gitattributes"
echo "ℹ︎ workspace: $GITHUB_WORKSPACE"
# "Export" a cleaned copy to a temp directory
TMP_DIR="/github/archivetmp"
mkdir "$TMP_DIR"
git config --global user.email "10upbot+github@10up.com"
git config --global user.name "10upbot on GitHub"
# If there's no .gitattributes file, write a default one into place
if [[ ! -e "$GITHUB_WORKSPACE/.gitattributes" ]]; then
cat > "$GITHUB_WORKSPACE/.gitattributes" <<-EOL
/$ASSETS_DIR export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.github export-ignore
EOL
# Ensure we are in the $GITHUB_WORKSPACE directory, just in case
# The .gitattributes file has to be committed to be used
# Just don't push it to the origin repo :)
git add .gitattributes && git commit -m "Add .gitattributes file"
fi
# This will exclude everything in the .gitattributes file with the export-ignore flag
git archive HEAD | tar x --directory="$TMP_DIR"
cd "$SVN_DIR"
# Copy from clean copy to /trunk, excluding dotorg assets
# The --delete flag will delete anything in destination that no longer exists in source
rsync -rc "$TMP_DIR/" trunk/ --delete
fi
# Copy dotorg assets to /assets
rsync -rc "$GITHUB_WORKSPACE/$ASSETS_DIR/" assets/ --delete
# Add everything and commit to SVN
# The force flag ensures we recurse into subdirectories even if they are already added
# Suppress stdout in favor of svn status later for readability
echo "➤ Preparing files..."
svn add . --force > /dev/null
# SVN delete all deleted files
# Also suppress stdout here
svn status | grep '^\!' | sed 's/! *//' | xargs -I% svn rm % > /dev/null
# Copy tag locally to make this a single commit
echo "➤ Copying tag..."
svn cp "trunk" "tags/$VERSION"
svn status
echo "➤ Committing files..."
svn commit -m "Update to version $VERSION from GitHub" --no-auth-cache --non-interactive --username "$SVN_USERNAME" --password "$SVN_PASSWORD"
echo "✓ Plugin deployed!"
@idokd

idokd commented Oct 10, 2019

Copy link
Copy Markdown
Author

I have seen this wonderful Github Action: https://github.com/10up/action-wordpress-plugin-deploy

I am more git fan than svn (used both, just a bit rusty with subversion) - so the news that I have to use wordpress.org subversion to deploy the plugin to their plugin directory was a bit of a shock!, how now I will be back into having respositories in subversion, searching for a script that can do the sync correctly from github to subversion - i found this above action. But HEY! , Github actions are still in beta, and I am looking to be approved to this beta use.

meanwhile, since i couldnt wait, i had adapted the original code to cloudbuild.yaml and run it on google cloud builds.

  1. Mirrored my github respository to Google Source Repository
  2. Assigned new Push trigger on tags and cloudbuild.yaml
  3. Added these 2 files to my respository
  4. assigned Substitution variables for those: _SVN_USERNAME, _SVN_PASSWORD, _SLUG, _REPOSITORY_URL

where:
_SVN_USERNAME = Wordpress.org username
_SVN_PASSWORD = Wordpress.org password
_SLUG = your svn repository name
_REPOSITORY_URL = github respository clone url (https) not ssh!

now every time you create a tag in github, it will automatically be mirrored to google source repositories and run the cloudbuild.yaml build script to deploy.

or simply run it manually via cmd:

gcloud builds submit --config cloudbuild.yaml . --substitutions=
_SVN_USERNAME="{wordpress.org username}",_SVN_PASSWORD="wordpress.org password",_SLUG="plugin svn repository",TAG_NAME="release tag number",_REPOSITORY_URL=
"https://github.com/username/repository.git",REPO_NAME="that will be used for your build name"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment