-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmykomap-site-deploy
executable file
·133 lines (123 loc) · 3.74 KB
/
mykomap-site-deploy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
# This script deploys a mini-website that is built/uses mykomap. It
# uses `rsync` to synchronise a remote directory to match a local
# directory (`rsync` is therefore obviously a requirement).
#
# General usage:
#
# mykomap-site-deploy [-f <from dir> ] [-t <to url>] [-u <user>] [-g <group>]
#
# It is intended to be run as an NPM run-script, both within `mykomap`
# itself, and consuming packages. Therefore it accepts by default
# package.json parameters values exposed by NPM via the environment
# variables prefixed with `npm_`, as described here:
#
# https://docs.npmjs.com/cli/v6/using-npm/scripts#environment
#
# The main parameters you need to define override the defaults in the
# `config` block, which are:
#
# "config": {
# "deploy_to": "",
# "deploy_user": "www-data",
# "deploy_group": "www-data"
# },
#
# These define:
#
# - `deploy_to`: the destination URL to pass to `rsync`
# - `deploy_user`: the user who should own the deployed files
# - `deploy_group`: the group who should own the deployed files
#
# As described in the package.json documentation, these default values
# can be overridden on a per-package basis. For example, to set
# `deploy_to` to something else for an NPM package called `mypackage`:
#
# npm config set mypackage:deploy_to=example.com:/some/path
#
# To support scripting, however, the parameters `-t`, `-u` and `-g`
# can be supplied, and will override all of the above
# configuration. The `-f` parameter will override the hardwired
# default source directory `build/out`.
#
# (As of NPM v2.0.0, any trailing parameters passed to npm when
# invoking a run-script are passed to the script verbatim, see link
# below.)
#
# https://docs.npmjs.com/cli/v6/commands/npm-run-script
#
# (Also, as of NPM v7, npm_package_config_* variables no longer get
# their values from the configured values in .npmrc; therefore it is
# neccessary to get them using `npm config get ...` and set the
# variables as a workaround within this script.)
#
# This means you can deploy to a host `[email protected]:/some/other/path`
# whatever the configuration, using:
#
# npm run deploy -- -t [email protected]:/some/other/path
#
# ...assuming the run script is named `deploy` like below:
#
# "scripts": {
# "deploy": "bin/mykomap-site-deploy"
# },
#
set -e
# Get the defaults
for suffix in user group to; do
# Work around the loss of the ability for .npmrc to override
# npm_package_config_* environment variables:
# https://github.com/npm/rfcs/blob/main/implemented/0021-reduce-lifecycle-script-environment.md
var=${npm_package_name:?npm_package_name unset}:deploy_$suffix
val=$(npm config get $var)
if [ "$val" = "undefined" ]; then
eval deploy_$suffix="\$npm_package_config_deploy_$suffix"
else
eval deploy_$suffix="$val"
fi
done
deploy_from=build/out/
# Parse options
while getopts "f:t:u:g:" OPTION
do
case $OPTION in
f)
deploy_from="$OPTARG"
;;
t)
deploy_to="$OPTARG"
;;
u)
deploy_user="$OPTARG"
;;
g)
deploy_group="$OPTARG"
;;
?)
printf "invalid option '$OPTION', stopping\n" >&2
exit 1
;;
esac
done
# remove the options from the argument list
shift $((OPTIND-1))
if [ -n "$*" ]; then
printf "Unknown parameters: $*\n"
exit -1
fi
echo "# deploy_from=$deploy_from"
echo "# deploy_to=$deploy_to"
echo "# deploy_user=$deploy_user"
echo "# deploy_group=$deploy_group"
# Echo command with arguments expanded
set -x
rsync \
-avzc \
--delete \
--rsync-path='sudo rsync' \
--usermap="*:${deploy_user:?}" \
--groupmap="*:${deploy_group:?}" \
--exclude=build.txt \
--exclude=.* \
"${deploy_from:?}" \
"${deploy_to:?}"