This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathd
executable file
·145 lines (122 loc) · 2.57 KB
/
d
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
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
# Docker utilities - d
set -o errexit
declare self dockerreg app package user repository
self=$(basename "$0")
dockerreg=https://docker.pkg.github.com
# Helper functions -----------------------------------------------------
check_cf_version() {
if ! cf --version | grep -q "version 6"; then
echo cf version 6 is required
exit 1
fi
}
usage() {
cat <<EOF
Usage: $self <options> <action>
with options:
-a | --app <app location>
-p | --package <package name>
-u | --user <GitHub user>
-r | --repository <GitHub repository>
with actions:
login: login to GitHub Packages
build: create the Docker image (needs -a, -p, -u and -r)
run: run a container locally based on the image (needs -p, -u and -r)
publish: publish the image to GitHub (needs -p, -u and -r)
EOF
}
tagname() {
echo "docker.pkg.github.com/$user/$repository/$package:latest"
}
checkval() {
while (( $# )); do
if [[ -z "$1" ]]; then
usage
exit 1
fi
shift
done
}
# Action functions -----------------------------------------------------
login() {
local user pass
echo Authenticating with GitHub Packages
read -r -p "Enter username: " user
read -r -s -p "Enter password / token: " pass
echo
echo -n "$pass" \
| docker login $dockerreg \
--username "$user" \
--password-stdin
}
build() {
checkval "$app" "$package" "$user" "$repository"
echo Building image for "$app"
docker build \
-t "$(tagname)" \
-f ./Dockerfile \
"$app"
}
run() {
checkval "$package" "$user" "$repository"
local port=${PORT:-5000}
echo Running detached instance of image locally
docker run \
-d \
-p "$port:$port" \
"$(tagname)"
}
publish() {
checkval "$package" "$user" "$repository"
echo Publishing image to GitHub Packages
docker push \
"$(tagname)"
}
# Main function --------------------------------------------------------
main() {
local action
local arg
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
while (( $# )); do
arg=$1
case $arg in
-a|--app)
shift
app=$1
;;
-p|--package)
shift
package=$1
;;
-u|--user)
shift
user=$1
;;
-r|--repository)
shift
repository=$1
;;
login|build|run|publish)
action=$arg
shift $(( $# - 1 ))
;;
*)
usage
exit 1
;;
esac
shift
done
if [[ -z "$action" ]]; then
usage
exit 1
fi
$action
}
# Execution start ------------------------------------------------------
check_cf_version
main "$@"