Debian Codegraph @

upload-release.sh

raw
1 #!/bin/bash
2 set -x
3 SELF="$0"
4 TAG="$1"
5 TOKEN="$2"
6
7 NAME="nss-pem"
8 NV="${TAG}"
9
10 usage() {
11 printf "Usage: %s TAG TOKEN\n" "$SELF" >&2
12 exit 1
13 }
14
15 die() {
16 printf "%s: error: %s\n" "$SELF" "$*" >&2
17 exit 1
18 }
19
20 # check arguments
21 test "$TAG" = "$(git describe --tags "$TAG")" || usage
22 test -n "$TOKEN" || usage
23
24 # dump a tarball
25 SRC_TAR="${NV}.tar"
26 git archive --prefix="$NV/" --format="tar" HEAD -- . > "$SRC_TAR" \
27 || die "failed to export sources"
28
29 # produce .tar.gz
30 TAR_GZ="${NV}.tar.gz"
31 gzip -c "$SRC_TAR" > "$TAR_GZ" || die "failed to create $TAR_GZ"
32
33 # produce .tar.xz
34 TAR_XZ="${NV}.tar.xz"
35 xz -c "$SRC_TAR" > "$TAR_XZ" || die "failed to create $TAR_XZ"
36
37 # sign the tarballs
38 for file in "$TAR_GZ" "$TAR_XZ"; do
39 gpg --armor --detach-sign "$file" || die "tarball signing failed"
40 test -f "${file}.asc" || die "tarball signature was not created"
41 done
42
43 # file to store response from GitHub API
44 JSON="./${NV}-github-relase.js"
45
46 # create a new release on GitHub
47 curl "https://api.github.com/repos/${USER}/${NAME}/releases" \
48 -o "$JSON" --fail --verbose \
49 --header "Authorization: token $TOKEN" \
50 --data '{
51 "tag_name": "'"$TAG"'",
52 "target_commitish": "master",
53 "name": "'"$NV"'",
54 "draft": false,
55 "prerelease": false
56 }' || exit $?
57
58 # parse upload URL from the response
59 UPLOAD_URL="$(grep '^ *"upload_url": "' "$JSON" \
60 | sed -e 's/^ *"upload_url": "//' -e 's/{.*}.*$//')"
61 grep '^https://uploads.github.com/.*/assets$' <<< "$UPLOAD_URL" || exit $?
62
63 # upload both .tar.gz and .tar.xz
64 for comp in gzip xz; do
65 file="${NV}.tar.${comp:0:2}"
66 curl "${UPLOAD_URL}?name=${file}" \
67 -T "$file" --fail --verbose \
68 --header "Authorization: token $TOKEN" \
69 --header "Content-Type: application/x-${comp}" \
70 || exit $?
71 done
72
73 # upload signatures
74 for file in "${TAR_GZ}.asc" "${TAR_XZ}.asc"; do
75 curl "${UPLOAD_URL}?name=${file}" \
76 -T "$file" --fail --verbose \
77 --header "Authorization: token $TOKEN" \
78 --header "Content-Type: text/plain" \
79 || exit $?
80 done