|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Note that this does not use pipefail |
| 4 | +# because if the grep later doesn't match any deleted files, |
| 5 | +# which is likely the majority case, |
| 6 | +# it does not exit with a 0, and I only care about the final exit. |
| 7 | +set -eo |
| 8 | + |
| 9 | +# Ensure SVN username and password are set |
| 10 | +# IMPORTANT: while secrets are encrypted and not viewable in the GitHub UI, |
| 11 | +# they are by necessity provided as plaintext in the context of the Action, |
| 12 | +# so do not echo or use debug mode unless you want your secrets exposed! |
| 13 | +if [[ -z "$SVN_USERNAME" ]]; then |
| 14 | + echo "Set the SVN_USERNAME secret" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +if [[ -z "$SVN_PASSWORD" ]]; then |
| 19 | + echo "Set the SVN_PASSWORD secret" |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +if [[ -z "$GITHUB_TOKEN" ]]; then |
| 24 | + echo "Set the GITHUB_TOKEN env variable" |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +# Allow some ENV variables to be customized |
| 29 | +if [[ -z "$SLUG" ]]; then |
| 30 | + SLUG=${GITHUB_REPOSITORY#*/} |
| 31 | +fi |
| 32 | +echo "ℹ︎ SLUG is $SLUG" |
| 33 | + |
| 34 | +# Does it even make sense for VERSION to be editable in a workflow definition? |
| 35 | +if [[ -z "$VERSION" ]]; then |
| 36 | + VERSION=${GITHUB_REF#refs/tags/} |
| 37 | +fi |
| 38 | +echo "ℹ︎ VERSION is $VERSION" |
| 39 | + |
| 40 | +if [[ -z "$ASSETS_DIR" ]]; then |
| 41 | + ASSETS_DIR=".wordpress-org" |
| 42 | +fi |
| 43 | +echo "ℹ︎ ASSETS_DIR is $ASSETS_DIR" |
| 44 | + |
| 45 | +SVN_URL="http://plugins.svn.wordpress.org/${SLUG}/" |
| 46 | +SVN_DIR="/github/svn-${SLUG}" |
| 47 | + |
| 48 | +# Checkout just trunk and assets for efficiency |
| 49 | +# Tagging will be handled on the SVN level |
| 50 | +echo "➤ Checking out .org repository..." |
| 51 | +svn checkout --depth immediates "$SVN_URL" "$SVN_DIR" |
| 52 | +cd "$SVN_DIR" |
| 53 | +svn update --set-depth infinity assets |
| 54 | +svn update --set-depth infinity trunk |
| 55 | + |
| 56 | +echo "➤ Copying files..." |
| 57 | +cd "$GITHUB_WORKSPACE" |
| 58 | + |
| 59 | +# "Export" a cleaned copy to a temp directory |
| 60 | +TMP_DIR="/github/archivetmp" |
| 61 | +mkdir "$TMP_DIR" |
| 62 | + |
| 63 | +git config --global user.email "10upbot+github@10up.com" |
| 64 | +git config --global user.name "10upbot on GitHub" |
| 65 | + |
| 66 | +# If there's no .gitattributes file, write a default one into place |
| 67 | +if [[ ! -e "$GITHUB_WORKSPACE/.gitattributes" ]]; then |
| 68 | + cat > "$GITHUB_WORKSPACE/.gitattributes" <<-EOL |
| 69 | + /$ASSETS_DIR export-ignore |
| 70 | + /.gitattributes export-ignore |
| 71 | + /.gitignore export-ignore |
| 72 | + /.github export-ignore |
| 73 | + EOL |
| 74 | + |
| 75 | + # Ensure we are in the $GITHUB_WORKSPACE directory, just in case |
| 76 | + # The .gitattributes file has to be committed to be used |
| 77 | + # Just don't push it to the origin repo :) |
| 78 | + git add .gitattributes && git commit -m "Add .gitattributes file" |
| 79 | +fi |
| 80 | + |
| 81 | +# This will exclude everything in the .gitattributes file with the export-ignore flag |
| 82 | +git archive HEAD | tar x --directory="$TMP_DIR" |
| 83 | + |
| 84 | +cd "$SVN_DIR" |
| 85 | + |
| 86 | +# Copy from clean copy to /trunk, excluding dotorg assets |
| 87 | +# The --delete flag will delete anything in destination that no longer exists in source |
| 88 | +rsync -rc "$TMP_DIR/" trunk/ --delete |
| 89 | + |
| 90 | +# Copy dotorg assets to /assets |
| 91 | +rsync -rc "$GITHUB_WORKSPACE/$ASSETS_DIR/" assets/ --delete |
| 92 | + |
| 93 | +# Add everything and commit to SVN |
| 94 | +# The force flag ensures we recurse into subdirectories even if they are already added |
| 95 | +# Suppress stdout in favor of svn status later for readability |
| 96 | +echo "➤ Preparing files..." |
| 97 | +svn add . --force > /dev/null |
| 98 | + |
| 99 | +# SVN delete all deleted files |
| 100 | +# Also suppress stdout here |
| 101 | +svn status | grep '^\!' | sed 's/! *//' | xargs -I% svn rm % > /dev/null |
| 102 | + |
| 103 | +# Copy tag locally to make this a single commit |
| 104 | +echo "➤ Copying tag..." |
| 105 | +svn cp "trunk" "tags/$VERSION" |
| 106 | + |
| 107 | +svn status |
| 108 | + |
| 109 | +echo "➤ Committing files..." |
| 110 | +svn commit -m "Update to version $VERSION from GitHub" --no-auth-cache --non-interactive --username "$SVN_USERNAME" --password "$SVN_PASSWORD" |
| 111 | + |
| 112 | +echo "✓ Plugin deployed!" |
0 commit comments