Create APT repository for Bazel during release process

--
Change-Id: I1a817ebcd5a7ea644f6566f92d209548948679af
Reviewed-on: https://bazel-review.googlesource.com/#/c/3702
MOS_MIGRATED_REVID=123124232
diff --git a/scripts/ci/build.sh b/scripts/ci/build.sh
index 3d63948..1ca6017 100755
--- a/scripts/ci/build.sh
+++ b/scripts/ci/build.sh
@@ -29,6 +29,8 @@
 
 : ${GCS_BASE_URL:=https://storage.googleapis.com}
 : ${GCS_BUCKET:=bucket-o-bazel}
+: ${GCS_APT_BUCKET:=bazel-apt}
+: ${APT_GPG_KEY_NAME:=apt-key.sec.gpg}
 
 : ${EMAIL_TEMPLATE_RC:=${SCRIPT_DIR}/rc_email.txt}
 : ${EMAIL_TEMPLATE_RELEASE:=${SCRIPT_DIR}/release_email.txt}
@@ -252,6 +254,17 @@
       | "${hoedown}"
 }
 
+function get_gsutil() {
+  local gs="${GSUTIL:-$(which gsutil 2>/dev/null || true) -m}"
+  if [ ! -x "${gs}" ]; then
+    echo "Please set GSUTIL to the path the gsutil binary." >&2
+    echo "gsutil (https://cloud.google.com/storage/docs/gsutil/) is the" >&2
+    echo "command-line interface to google cloud." >&2
+    exit 1
+  fi
+  echo "${gs}"
+}
+
 # Deploy a release candidate to Google Cloud Storage.
 # It requires to have gsutil installed. You can force the path to gsutil
 # by setting the GSUTIL environment variable. The GCS_BUCKET should be the
@@ -259,15 +272,9 @@
 # This methods expects the following arguments:
 #   $1..$n files generated by package_build
 function release_to_gcs() {
-  local gs="${GSUTIL:-$(which gsutil 2>/dev/null || true) -m}"
-  local release_name=$(get_release_name)
-  local rc=$(get_release_candidate)
-  if [ ! -x "${gs}" ]; then
-    echo "Please set GSUTIL to the path the gsutil binary." >&2
-    echo "gsutil (https://cloud.google.com/storage/docs/gsutil/) is the" >&2
-    echo "command-line interface to google cloud." >&2
-    return 1
-  fi
+  local gs="$(get_gsutil)"
+  local release_name="$(get_release_name)"
+  local rc="$(get_release_candidate)"
   if [ -z "${GCS_BUCKET-}" ]; then
     echo "Please set GCS_BUCKET to the name of your Google Cloud Storage bucket." >&2
     return 1
@@ -284,8 +291,89 @@
         >"${dir}/${release_name}/rc${rc}"/index.html
     cd ${dir}
     "${gs}" cp -a public-read -r . "gs://${GCS_BUCKET}"
-    cd ${prev_dir}
-    rm -fr ${dir}
+    cd "${prev_dir}"
+    rm -fr "${dir}"
+    trap - EXIT
+  fi
+}
+
+function create_apt_repository() {
+  mkdir conf
+  cat > conf/distributions <<EOF
+Origin: Bazel Authors
+Label: Bazel
+Codename: stable
+Architectures: amd64
+Components: jdk1.7 jdk1.8
+Description: Bazel APT Repository
+DebOverride: override.stable
+DscOverride: override.stable
+SignWith: ${APT_GPG_KEY_ID}
+
+Origin: Bazel Authors
+Label: Bazel
+Codename: testing
+Architectures: amd64
+Components: jdk1.7 jdk1.8
+Description: Bazel APT Repository
+DebOverride: override.testing
+DscOverride: override.testing
+SignWith: ${APT_GPG_KEY_ID}
+EOF
+
+  cat > conf/options <<EOF
+verbose
+ask-passphrase
+basedir .
+EOF
+
+  touch conf/override.stable
+  touch conf/override.testing
+
+  (gpg --list-keys | grep "${APT_GPG_KEY_ID}" > /dev/null) || \
+  gpg --allow-secret-key-import --import /opt/secrets/"${APT_GPG_KEY_NAME}"
+
+  local distribution="$1"
+  local deb_pkg_name_jdk8="$2"
+  local deb_pkg_name_jdk7="$3"
+  reprepro -C jdk1.8 includedeb "${distribution}" "${deb_pkg_name_jdk8}"
+  reprepro -C jdk1.7 includedeb "${distribution}" "${deb_pkg_name_jdk7}"
+
+  "${gs}" -m cp -a public-read -r dists "gs://${GCS_APT_BUCKET}/"
+  "${gs}" cp -a public-read -r pool "gs://${GCS_APT_BUCKET}/"
+}
+
+function release_to_apt() {
+  local gs="$(get_gsutil)"
+  local release_name="$(get_release_name)"
+  local rc="$(get_release_candidate)"
+  if [ -z "${GCS_APT_BUCKET-}" ]; then
+    echo "Please set GCS_APT_BUCKET to the name of your GCS bucket for apt repository." >&2
+    return 1
+  fi
+  if [ -z "${APT_GPG_KEY_ID-}" ]; then
+    echo "Please set APT_GPG_KEY_ID for apt repository." >&2
+    return 1
+  fi
+  if [ -n "${release_name}" ]; then
+    # Make a temporary folder with the desired structure
+    local dir="$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXX)"
+    local prev_dir="$PWD"
+    trap "{ cd ${prev_dir}; rm -fr ${dir}; }" EXIT
+    mkdir -p "${dir}/${release_name}"
+    local release_label="$(get_full_release_name)"
+    local deb_pkg_name_jdk8="${release_name}/bazel_${release_label}-linux-x86_64.deb"
+    local deb_pkg_name_jdk7="${release_name}/bazel_${release_label}-jdk7-linux-x86_64.deb"
+    cp "${tmpdir}/bazel_${release_label}-linux-x86_64.deb" "${dir}/${deb_pkg_name_jdk8}"
+    cp "${tmpdir}/bazel_${release_label}-jdk7-linux-x86_64.deb" "${dir}/${deb_pkg_name_jdk7}"
+    cd "${dir}"
+    if [ -n "${rc}" ]; then
+      create_apt_repository testing "${deb_pkg_name_jdk8}" "${deb_pkg_name_jdk7}"
+    else
+      create_apt_repository stable "${deb_pkg_name_jdk8}" "${deb_pkg_name_jdk7}"
+    fi
+    cd "${prev_dir}"
+    rm -fr "${dir}"
     trap - EXIT
   fi
 }
@@ -301,6 +389,7 @@
   done
   release_to_github "${github_args[@]}"
   release_to_gcs "$@"
+  release_to_apt
 }
 
 # A wrapper for the whole release phase: