-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-secrets.sh
executable file
·59 lines (48 loc) · 1.22 KB
/
install-secrets.sh
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
#!/bin/bash
DIR=$(cd `dirname $0` && pwd)
if [ -z "$DRONE_SERVER" ] || [ -z "$DRONE_TOKEN" ]; then
echo "Missing drone environment [ DRONE_SERVER, DRONE_TOKEN ]"
echo
echo "export DRONE_SERVER={URL}"
echo "export DRONE_TOKEN={TOKEN}"
echo
exit 0
fi
if [ -z "$1" ]; then
echo "Missing repo parameter. run $0 {target}."
exit 0
fi
if ! type "drone" &>/dev/null; then
echo "'drone' command is not available."
exit 0
fi
TARGET=$1
if drone repo info $TARGET |& grep 'client error 404' &>/dev/null; then
echo "'$TARGET' repo is not available."
exit 0
fi
if [ ! -d $DIR/secrets ]; then
echo "'secrets' directory does not exist."
exit 0
fi
read -p "Are you sure to import secrets to $TARGET? [y/n]" -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
echo
for f in $DIR/secrets/*
do
if [ ! -f $f ] || [ -d $f ]; then
continue
fi
SECRET_NAME=`basename $f`
SECRET_FILE="@$f"
# remove existing secret
if drone secret rm --repository $TARGET --name $SECRET_NAME &>/dev/null; then
echo "update $SECRET_NAME..."
else
echo "import $SECRET_NAME..."
fi
# add secret
drone secret add --repository $TARGET --name $SECRET_NAME --value $SECRET_FILE
done