deploy.sh 3.94 KB
Newer Older
Erick Hitter's avatar
Erick Hitter committed
1
2
3
4
5
6
7
8
#!/usr/bin/env bash

# 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

9
10
11
12
# Provide a basic version identifier, particularly since this script
# is usually accessed via CDN.
echo "ℹ︎ WP-ORG-PLUGIN-DEPLOY VERSION: 2019041401"

Erick Hitter's avatar
Erick Hitter committed
13
if [[ -z "$CI" ]]; then
14
	echo "𝘅︎ Script is only to be run by GitLab CI" 1>&2
Erick Hitter's avatar
Erick Hitter committed
15
16
17
	exit 1
fi

18
19
20
21
# Ensure certain environment variables are set
# IMPORTANT: while access to secrets is restricted in the GitLab UI,
# they are by necessity provided as plaintext in the context of this script,
# so do not echo or use debug mode unless you want your secrets exposed!
Erick Hitter's avatar
Erick Hitter committed
22
if [[ -z "$WP_ORG_USERNAME" ]]; then
23
	echo "𝘅︎ WordPress.org username not set" 1>&2
Erick Hitter's avatar
Erick Hitter committed
24
25
26
27
	exit 1
fi

if [[ -z "$WP_ORG_PASSWORD" ]]; then
28
	echo "𝘅︎ WordPress.org password not set" 1>&2
Erick Hitter's avatar
Erick Hitter committed
29
30
31
32
	exit 1
fi

if [[ -z "$PLUGIN_SLUG" ]]; then
33
	echo "𝘅︎ Plugin's SVN slug is not set" 1>&2
Erick Hitter's avatar
Erick Hitter committed
34
35
36
37
	exit 1
fi

if [[ -z "$PLUGIN_VERSION" ]]; then
38
	echo "𝘅︎ Plugin's version is not set" 1>&2
Erick Hitter's avatar
Erick Hitter committed
39
40
41
	exit 1
fi

42
43
44
45
if [[ -z "$WP_ORG_ASSETS_DIR" ]]; then
	WP_ORG_ASSETS_DIR=".wordpress-org"
fi

46
47
48
echo "ℹ︎ PLUGIN_SLUG: $PLUGIN_SLUG"
echo "ℹ︎ PLUGIN_VERSION: $PLUGIN_VERSION"
echo "ℹ︎ WP_ORG_RELEASE_REF: $WP_ORG_RELEASE_REF"
49
echo "ℹ︎ WP_ORG_ASSETS_DIR: $WP_ORG_ASSETS_DIR"
Erick Hitter's avatar
Erick Hitter committed
50
51
52
53
54

SVN_URL="https://plugins.svn.wordpress.org/${PLUGIN_SLUG}/"
SVN_DIR="$CI_BUILDS_DIR/svn-${PLUGIN_SLUG}"
TMP_DIR="$CI_BUILDS_DIR/git-archive"

55
56
# Limit checkouts for efficiency
echo "➤ Checking out dotorg repository..."
Erick Hitter's avatar
Erick Hitter committed
57
58
svn checkout --depth immediates "$SVN_URL" "$SVN_DIR"
cd "$SVN_DIR"
59
svn update --set-depth infinity assets
Erick Hitter's avatar
Erick Hitter committed
60
svn update --set-depth infinity trunk
61
svn update --set-depth infinity "tags/${PLUGIN_VERSION}"
Erick Hitter's avatar
Erick Hitter committed
62
63
64
65
66
67
68
69
70
71
72

# Ensure we are in the $CI_PROJECT_DIR directory, just in case
echo "➤ Copying files..."
cd "$CI_PROJECT_DIR"

git config --global user.email "git-contrib+ci@ethitter.com"
git config --global user.name "Erick Hitter (GitLab CI)"

# If there's no .gitattributes file, write a default one into place
if [[ ! -e "$CI_PROJECT_DIR/.gitattributes" ]]; then
	cat > "$CI_PROJECT_DIR/.gitattributes" <<-EOL
73
	/${WP_ORG_ASSETS_DIR} export-ignore
Erick Hitter's avatar
Erick Hitter committed
74
75
	/.gitattributes export-ignore
	/.gitignore export-ignore
76
	/.gitlab-ci.yml export-ignore
Erick Hitter's avatar
Erick Hitter committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	EOL

	# 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
mkdir "$TMP_DIR"
git archive HEAD | tar x --directory="$TMP_DIR"

cd "$SVN_DIR"

# Copy from clean copy to /trunk
# The --delete flag will delete anything in destination that no longer exists in source
rsync -r "$TMP_DIR/" trunk/ --delete

94
95
96
# Copy dotorg assets to /assets
rsync -r "${CI_PROJECT_DIR}/${WP_ORG_ASSETS_DIR}/" assets/ --delete

Erick Hitter's avatar
Erick Hitter committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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/$PLUGIN_VERSION"

svn status

# Stop here unless this is a merge into master.
if [[ -z "$CI_COMMIT_REF_NAME" || -z "$WP_ORG_RELEASE_REF" || "$CI_COMMIT_REF_NAME" != "$WP_ORG_RELEASE_REF" ]]; then
Erick Hitter's avatar
Erick Hitter committed
115
	echo "𝘅︎ EXITING before commit step as this is the '${CI_COMMIT_REF_NAME}' ref, not the '${WP_ORG_RELEASE_REF}' ref." 1>&2
Erick Hitter's avatar
Erick Hitter committed
116
117
118
119
120
121
122
	exit 0
fi

echo "➤ Committing files..."
svn commit -m "Update to version ${PLUGIN_VERSION} from GitLab (${CI_PROJECT_URL}; ${CI_JOB_URL})" --no-auth-cache --non-interactive  --username "$WP_ORG_USERNAME" --password "$WP_ORG_PASSWORD"

echo "✓ Plugin deployed!"