Update release process of apt repo

With the new release process, Bazel APT repo will now support
installing Bazel at a specific version.
Users can run "apt-get install bazel=1.0.0", can call "bazel" from command line.

We also publish side-by-side packages named "bazel-<version>", which
made it possible to install multiple Bazel versions at the same time.
Users can run "apt-get install bazel-1.0.0", then call "bazel-1.0.0" from command line.
This will support https://github.com/bazelbuild/bazel/pull/10272

Closes #10278.

PiperOrigin-RevId: 281723964
diff --git a/scripts/ci/build.sh b/scripts/ci/build.sh
index abc940a..8ed03d6 100755
--- a/scripts/ci/build.sh
+++ b/scripts/ci/build.sh
@@ -208,6 +208,93 @@
   fi
 }
 
+# Generate new content of Release file
+function print_new_release_content() {
+  local distribution="$1"
+  # Print the headers of the original Release file
+  cat <<EOF
+Origin: Bazel Authors
+Label: Bazel
+Codename: $1
+Date: $(date -u "+%a, %d %b %Y %H:%M:%S UTC")
+Architectures: amd64
+Components: jdk1.8
+Description: Bazel APT Repository
+EOF
+  metadata_files=("jdk1.8/binary-amd64/Packages" "jdk1.8/binary-amd64/Packages.gz" "jdk1.8/binary-amd64/Release" "jdk1.8/source/Sources.gz" "jdk1.8/source/Release")
+  # Re-generate hashes for all metadata fiels
+  echo MD5Sum:
+   for file in ${metadata_files[*]}; do
+    path="dists/${distribution}/$file"
+    echo "" "$(md5sum ${path} | cut -d " " -f1)" "$(ls -l ${path} | cut -d " " -f5)" "$file"
+   done
+  echo SHA1:
+   for file in ${metadata_files[*]}; do
+    path="dists/${distribution}/$file"
+    echo "" "$(sha1sum ${path} | cut -d " " -f1)" "$(ls -l ${path} | cut -d " " -f5)" "$file"
+   done
+  echo SHA256:
+   for file in ${metadata_files[*]}; do
+    path="dists/${distribution}/$file"
+    echo "" "$(sha256sum ${path} | cut -d " " -f1)" "$(ls -l ${path} | cut -d " " -f5)" "$file"
+   done
+}
+
+# Merge metadata with previous distribution
+function merge_previous_dists() {
+  local distribution="$1"
+  # Download the metadata info from previous distrubution
+  mkdir -p previous
+  gsutil -m cp -r "gs://bazel-apt/dists" "./previous"
+
+  # Merge Packages and Packages.gz file
+  cat "previous/dists/${distribution}/jdk1.8/binary-amd64/Packages" >> "dists/${distribution}/jdk1.8/binary-amd64/Packages"
+  gzip -9c "dists/${distribution}/jdk1.8/binary-amd64/Packages" > "dists/${distribution}/jdk1.8/binary-amd64/Packages.gz"
+
+  # Merge Sources.gz file
+  gunzip "previous/dists/${distribution}/jdk1.8/source/Sources.gz"
+  gunzip "dists/${distribution}/jdk1.8/source/Sources.gz"
+  cat "previous/dists/${distribution}/jdk1.8/source/Sources" >> "dists/${distribution}/jdk1.8/source/Sources"
+  gzip -9c "dists/${distribution}/jdk1.8/source/Sources" > "dists/${distribution}/jdk1.8/source/Sources.gz"
+  rm -f "dists/${distribution}/jdk1.8/source/Sources"
+
+  # Update Release file
+  print_new_release_content "${distribution}" > "dists/${distribution}/Release.new"
+  mv "dists/${distribution}/Release.new" "dists/${distribution}/Release"
+
+  # Generate new signatures for Release file
+  rm -f "dists/stable/InRelease" "dists/stable/Release.gpg"
+  gpg --output "dists/stable/InRelease" --clear-sign "dists/stable/Release"
+  gpg --output "dists/stable/Release.gpg" --detach-sign "dists/stable/Release"
+}
+
+# Create a debian package with version in package name and add it to the repo
+function add_versioned_deb_pkg() {
+  local distribution="$1"
+  local deb_pkg_name="$2"
+  # Extract the original package
+  mkdir -p deb-old
+  dpkg-deb -R "${deb_pkg_name}" deb-old
+
+  # Get bazel version
+  bazel_version=$(grep "Version:" deb-old/DEBIAN/control | cut -d " " -f2)
+  bazel_version=${bazel_version/\~/}
+
+  # Generate new control file
+  mkdir -p deb-new/DEBIAN
+  sed "s/Package:\ bazel/Package:\ bazel-${bazel_version}/g" "deb-old/DEBIAN/control" > "deb-new/DEBIAN/control"
+
+  # Rename the actual Bazel binary to bazel-${bazel_version}
+  mkdir -p deb-new/usr/bin
+  cp "deb-old/usr/bin/bazel-real" "deb-new/usr/bin/bazel-${bazel_version}"
+
+  # Re-pack the debian package and add it to the repo
+  versioned_deb_pkg_name="bazel-${bazel_version}-versioned-package-amd64.deb"
+  chmod -R 0755 deb-new
+  dpkg-deb -b deb-new "${versioned_deb_pkg_name}"
+  reprepro -C jdk1.8 includedeb "${distribution}" "${versioned_deb_pkg_name}"
+}
+
 function create_apt_repository() {
   mkdir conf
   cat > conf/distributions <<EOF
@@ -259,6 +346,10 @@
   reprepro -C jdk1.8 includedeb "${distribution}" "${deb_pkg_name}"
   reprepro -C jdk1.8 includedsc "${distribution}" "${deb_dsc_name}"
 
+  add_versioned_deb_pkg "${distribution}" "${deb_pkg_name}"
+
+  merge_previous_dists "${distribution}"
+
   gsutil -m cp -r dists pool "gs://bazel-apt"
 }
 
diff --git a/scripts/packages/debian/BUILD b/scripts/packages/debian/BUILD
index 44e7375..d525b24 100644
--- a/scripts/packages/debian/BUILD
+++ b/scripts/packages/debian/BUILD
@@ -1,11 +1,11 @@
+load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar")
+
 filegroup(
     name = "srcs",
     srcs = glob(["**"]),
     visibility = ["//scripts:__subpackages__"],
 )
 
-load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar")
-
 pkg_tar(
     name = "bazel-bin",
     srcs = [
@@ -69,7 +69,7 @@
 pkg_deb(
     name = "bazel-debian",
     architecture = "amd64",
-    built_using = "bazel (HEAD)",
+    built_using = "bazel",
     conffiles = [
         "etc/bash_completion.d/bazel",
         "etc/bazel.bazelrc",
@@ -80,7 +80,6 @@
         "g++",
         "zlib1g-dev",
         "unzip",
-        "python",
     ],
     description_file = "description",
     homepage = "http://bazel.build",