Print only basename in the usage
[docker-gc-build/.git] / release.sh
1 #!/bin/bash
2
3 set -o nounset
4 set -o pipefail
5
6 die() {
7     echo "$1" >&2
8     exit 2
9 }
10
11 usage() {
12     cat <<EOF
13 ${0##*/} [options] [arguments]
14
15 Arguments:
16
17 -b Set the S3 bucket name
18
19 Options:
20
21 -h Help
22 -s Dry run (simulate)
23 -n Does not upload artifact (just build)
24 EOF
25     exit 1
26 }
27
28 do_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
39 parse_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
56
57 do_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
72 do_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
85 main() {
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
113 if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
114     main "$@"
115 fi