Print only basename in the usage
[docker-gc-build/.git] / release.sh
CommitLineData
e9a368ec
SB
1#!/bin/bash
2
64e09ea0
SB
3set -o nounset
4set -o pipefail
e9a368ec 5
64e09ea0
SB
6die() {
7 echo "$1" >&2
e9a368ec
SB
8 exit 2
9}
10
1b0f1b1b
SB
11usage() {
12 cat <<EOF
80a56b9a 13${0##*/} [options] [arguments]
1b0f1b1b 14
9e18ebc0 15Arguments:
1b0f1b1b 16
9e18ebc0 17-b Set the S3 bucket name
1b0f1b1b 18
9e18ebc0 19Options:
1b0f1b1b 20
9e18ebc0
SB
21-h Help
22-s Dry run (simulate)
23-n Does not upload artifact (just build)
1b0f1b1b
SB
24EOF
25 exit 1
26}
27
9e18ebc0
SB
28do_prereq() {
29 declare -a deps=(gpg deb-s3)
30
31 for dep in ${deps[*]}
32 do
33 [[ ! -f $(command -v "$dep") ]] && {
34 die "You need to install: $dep"
35 }
36 done
37}
38
39parse_args() {
40 while getopts ":b:hsn" opt; do
41 case ${opt} in
42 b) BUCKET=${OPTARG}
43 ;;
44 h) usage
45 ;;
46 s) DRY_RUN="echo "
47 ;;
48 n) NO_UPLOAD=1
49 ;;
50 \?) echo "Invalid Option: -${OPTARG}" 1>&2; exit 1
51 ;;
52 esac
53 done
54}
55
e9a368ec 56
9e18ebc0
SB
57do_build() {
58 $DRY_RUN docker build \
59 -t "${TAG}" \
60 --build-arg VERSION="${VERSION}" \
61 --build-arg AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
62 --build-arg AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
63 -f ./Dockerfile .
64
65 $DRY_RUN docker run -v /tmp/:/tmp "${TAG}" /bin/bash -c 'cp /docker-gc*.deb /tmp'
66
67 package=$(ls /tmp/*.deb || :)
68 printf "[+] Debian Package generated into '%s'\n" "${package}"
69}
70
71
72do_upload() {
73 printf "[+] Using GPG %s for package signature\n" "${NITRO_GPG_KEY}"
74 $DRY_RUN deb-s3 upload \
75 --access-key-id="${AWS_ACCESS_KEY_ID}" \
76 --secret-access-key="${AWS_SECRET_ACCESS_KEY}" \
77 --s3-region="${AWS_REGION}" \
78 --bucket="${BUCKET}" \
79 --sign="${NITRO_GPG_KEY}" "${package}" || exit 1
80 printf "[+] Successfully uploaded package into '%s'\n" "${BUCKET}"
81}
82
83
84
85main() {
86 COMMIT=$( (cd docker-gc && git rev-parse --short HEAD) )
87 VERSION="2:$(cat "${PWD}"/docker-gc/version.txt)~${COMMIT}"
88 TAG="gonitro/docker-gc-build:${COMMIT}"
89 AWS_REGION=us-west-2
90 BUCKET=${BUCKET:-nitro-apt-repo}
91 DRY_RUN=${DRY_RUN:-}
92 NO_UPLOAD=${NO_UPLOAD:-}
93 KEYSERVER=${KEYSERVER:-pgpkeys.eu}
94 NITRO_GPG_KEY=$(gpg \
95 --keyserver "${KEYSERVER}" \
96 --batch \
97 --search-keys \
98 --with-colons infra-guild@gonitro.com 2>&1| sed -E -n 's/^pub:.*(........):.*:.*:.*::/\1/p')
99
100 do_prereq
101
102 parse_args "$@"
103
104 do_build
105
106 if [[ -z ${NO_UPLOAD} ]]; then
107 do_upload
108 fi
109
110 exit 0
111}
112
113if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
114 main "$@"
115fi