-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.sh
More file actions
executable file
·115 lines (105 loc) · 3.58 KB
/
make.sh
File metadata and controls
executable file
·115 lines (105 loc) · 3.58 KB
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
#!/usr/bin/env bash
# shellcheck disable=SC3010,SC3040,SC3043,SC3060
set -euCo pipefail
owner='normal-computing'
project='loki'
repository='loki'
registry='ghcr.io'
version='3.5.1'
build_id=$(git describe --tags HEAD 2>/dev/null \
|| git describe --match=NeVeRmAtCh --always --abbrev=32 --dirty)
git_ref=$(git rev-parse --verify HEAD)
image="$registry/$owner/$project:$build_id"
image_source="https://github.com/$owner/$repository"
image_url="$image_source"
now=$(date -u +%FT%TZ)
main_build() {
ctr=$(buildah from "docker.io/grafana/loki:$version")
cleanup() {
buildah rm "$ctr"
}
trap cleanup EXIT
buildah config --user 65534 "$ctr"
buildah config --author "$owner" "$ctr"
buildah config --annotation org.opencontainers.image.authors="$owner" "$ctr"
buildah config --annotation org.opencontainers.image.created="$now" "$ctr"
buildah config --annotation org.opencontainers.image.revision="$git_ref" "$ctr"
buildah config --annotation org.opencontainers.image.source="$image_source" "$ctr"
buildah config --annotation org.opencontainers.image.url="$image_url" "$ctr"
buildah config --annotation org.opencontainers.image.version="$build_id" "$ctr"
buildah commit "$ctr" "$image"
cleanup
trap - EXIT
}
main_check() {
shellcheck -- *.sh
}
main_push() {
buildah push "$image"
if [[ -n "${tag_latest:-}" ]]; then
buildah push "$image" "$registry/$owner/$project:latest"
fi
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
echo "build-id=$build_id" >>"$GITHUB_OUTPUT"
fi
}
main_update() {
local new_version
if ! new_version=$(curl -sSfL "https://hub.docker.com/v2/repositories/grafana/loki/tags?page_size=100&ordering=last_updated" | \
jq -r '.results|.[]|.name' | \
grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | \
sort -V | \
tail -n1); then
echo "failed to get new version from docker hub tags" >&2; exit 1
fi
if [[ -z "$new_version" ]]; then
echo "failed to get a non empty version from docker hub tags" >&2; exit 2
fi
if [[ "$new_version" == "$version" ]]; then
exit 0
fi
echo "Updating the repository to $project version $new_version"
version="${version//./\\.}"
sed -e "s!^version='$version'\$!version='$new_version'!" -i make.sh
local uri
uri="$registry/$owner/$project"
sed -E "s!$uri:$version-[0-9]+!$uri:$new_version-0!" -i README.md
}
usage() {
if [[ -n "${1:-}" ]]; then
echo "$1" >&2
fi
echo "Usage:" >&2
echo " $0 build # build the container image." >&2
echo " $0 check # run checks." >&2
echo " $0 push # push the container image to the container registry." >&2
echo " $0 update # update the base image version" >&2
echo "Global options:" >&2
echo " -h, --help # show this help message'" >&2
echo " -v, --verbose # verbose mode via set -x" >&2
echo "Action specific options:" >&2
echo " -t, --tag-latest # also push the built image to latest" >&2
exit 1
}
PARSED=$(getopt -o htv -l help,tag-latest,verbose --name "$0" -- "$@") || exit 2
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help) usage ;;
-t|--tag-latest) tag_latest=1; shift ;;
-v|--verbose) verbose=1; shift ;;
--) shift; break ;;
*) break ;;
esac
done
action="${1:-}"
shift || true
if [[ -n "${verbose:-}" ]]; then set -x; fi
case "$action" in
build) main_build ;;
check) main_check ;;
push) main_push ;;
update) main_update ;;
"") usage ;;
*) usage "Unknown action: $action" ;;
esac