-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-repo.sh
executable file
·192 lines (177 loc) · 5.35 KB
/
config-repo.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
set -eu -o pipefail
# Defaults
DOCKER_REPOSITORY=localhost
GITHUB_USERNAME=
IMAGE_NAME="$(basename "$(pwd)" | awk -F'-' '{print $NF}')"
MAINTAINER="$(getent passwd "${USER}" | awk -F':' '{print $5}')"
MAINTAINER="${MAINTAINER:-${USER}}"
#######################################
# Log a message to STDERR and exit with a speific code.
# Globals:
# None
# Arguments:
# An exit code
# A message
# Outputs:
# A message string is output to STDERR and then exit is called
# with the specified code.
#######################################
fatal() {
local exitCode="${1}"; shift
echo "${@}" 1>&2
exit "${exitCode}"
}
#######################################
# Print usage information.
# Globals:
# None
# Arguments:
# None
# Outputs:
# A string of usage information to STDERR
#######################################
usage() {
cat <<USAGE 1>&2
Update Dockerfile, Makefile, and README.md with new values. The Docker image
name is generated by stripping the leading "docker-" from this repository's
directory. Example: if this repository is named "docker-nginx" then the image
name will be "nginx."
Usage: $(basename "${0}") [-h] [-d string] [-i string] [-g string] [-m string]
-d <string> Docker repository (default: "${DOCKER_REPOSITORY}")
-g <string> GitHub username
-i <string> The Docker image name (default: "${IMAGE_NAME}")
-m <string> Maintainer string (e.g. "Mark <mark@cdll.me>",
default: "${MAINTAINER}")
-h This message
USAGE
}
#######################################
# Generate GitHub workflow Markdown badge.
# Globals:
# None
# Arguments:
# GitHub username
# GitHub repository name
# GitHub workflow name, minus the .yml extension (default: "ci")
# Git branch name (default: "main")
# Outputs:
# A string of Markdown
#######################################
github_workflow_badge() {
local github_username="${1}"; shift
local github_repo="${1}"; shift
local workflow_name="${1:-ci}"; shift
local branch="${1:-main}"; shift
local url
local badge_url
url="https://github.com"
url+="/${github_username}/${github_repo}"
url+="/actions/workflows/${workflow_name}.yml"
badge_url="${url}/badge.svg?branch=${branch}"
echo "[![CI to Docker Hub](${badge_url})](${url})"
}
#######################################
# Generate Docker image page URL.
# Globals:
# None
# Arguments:
# Docker repository name
# Image name
# Outputs:
# A string of the Docker image page URL
######################################
docker_image_page() {
local docker_repo="${1}"; shift
local image_name="${1}"; shift
local url
url="https://hub.docker.com/repository/docker"
url+="/${docker_repo}/${image_name}"
echo "${url}"
}
#######################################
# Generate Docker image tags page URL.
# Globals:
# None
# Arguments:
# Docker repository name
# Image name
# Outputs:
# A string of the Docker image tags page URL
#######################################
docker_image_tags_page() {
local docker_repo="${1}"; shift
local image_name="${1}"; shift
local url
url="$(docker_image_page "${docker_repo}" "${image_name}")"
url+="/tags?page=1&ordering=last_updated"
echo "${url}"
}
#######################################
# Generate Docker image pulls Markdown badge.
# Globals:
# None
# Arguments:
# Docker repository name
# Image name
# Outputs:
# A string of Markdown
#######################################
docker_pulls_badge() {
local docker_repo="${1}"; shift
local image_name="${1}"; shift
local badge_url
local url
badge_url="https://img.shields.io/docker/pulls"
badge_url+="/${docker_repo}/${image_name}"
url="$(docker_image_page "${docker_repo}" "${image_name}")"
echo "[![Docker Pulls](${badge_url})](${url})"
}
#######################################
# Generate Docker image size Markdown badge.
# Globals:
# None
# Arguments:
# Docker repository name
# Image name
# Outputs:
# A string of Markdown
#######################################
docker_size_badge() {
local docker_repo="${1}"; shift
local image_name="${1}"; shift
local badge_url
local url
badge_url="https://img.shields.io/docker/image-size"
badge_url+="/${docker_repo}/${image_name}"
badge_url+="/latest?label=latest"
url="$(docker_image_tags_page "${docker_repo}" "${image_name}")"
echo "[![Docker Image Size](${badge_url})](${url})"
}
##
# Main
##
while getopts "hd:g:hm:" opt; do
case ${opt} in
d) DOCKER_REPOSITORY="${OPTARG}";;
g) GITHUB_USERNAME="${OPTARG}";;
h) usage; exit;;
m) MAINTAINER="${OPTARG}";;
:) usage; fatal 1 "Option -${OPTARG} requires an argument.";;
?) usage; fatal 1 "Unknown option -${OPTARG}.";;
esac
done
# Dockerfile
# Replace LABEL maintainer=... with the specified value
sed -i "s/LABEL maintainer=.*/LABEL maintainer=\"${MAINTAINER}\"/" Dockerfile
# Makefile
# Replace REPO = ... with the specified value
sed -i "s/REPO = .*/REPO = ${DOCKER_REPOSITORY}/" Makefile
# README.md
# Replace everything between the "Badges" HTML comments
badge_contents="<!-- Badges -->\n"
badge_contents+="$(github_workflow_badge "${GITHUB_USERNAME}" "docker-${IMAGE_NAME}")\n"
badge_contents+="$(docker_pulls_badge "${DOCKER_REPOSITORY}" "${IMAGE_NAME}")\n"
badge_contents+="$(docker_size_badge "${DOCKER_REPOSITORY}" "${IMAGE_NAME}")\n"
badge_contents+="<!-- /Badges -->"
sed -i "/<!-- Badges -->/,/<!-- \/Badges -->/c${badge_contents}" README.md