From 951c2f4cde4776387f0ca33c6e19b4a00fdaac3c Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Thu, 17 Dec 2020 10:05:10 +1100
Subject: [PATCH] gerrit: get files from bazel build dir

bazel likes to build everything in ~/.cache and then symlink bazel-*
"convience symlinks" in the workspace/build directory.  This causes a
problem for building docker images where we run in the context of the
build directory; docker will not follow the symlinks out of build
directory.

Currently the bazelisk-build copies parts of the build to the
top-level; this means the bazelisk-build role is gerrit specific,
rather than generic as the name implies.

We modify the gerrit build step to break build output symlink and move
it into the top level of the build tree, which is the context the
docker build runs in later.  Since this is now just a normal
directory, we can copy from it at will there.

This is useful in follow-on builds where we want to start copying more
than just the release.war file from the build tree, e.g. polygerrit
plugin output.

While we're here, remove the javamelody things that were only for 2.X
series gerrit, which we don't build any more.

[1] https://docs.bazel.build/versions/master/output_directories.html

Change-Id: I00abe437925d805bd88824d653eec38fa95e4fcd
---
 docker/gerrit/bazel/Dockerfile       |  7 +------
 playbooks/zuul/gerrit/run.yaml       | 22 ++++++++++++++++++++++
 roles/bazelisk-build/tasks/main.yaml | 12 +++++-------
 3 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/docker/gerrit/bazel/Dockerfile b/docker/gerrit/bazel/Dockerfile
index 8966162f4e..67bb3e9361 100644
--- a/docker/gerrit/bazel/Dockerfile
+++ b/docker/gerrit/bazel/Dockerfile
@@ -17,15 +17,10 @@
 
 FROM docker.io/opendevorg/gerrit-base as gerrit
 
-COPY release.war /var/gerrit/bin/gerrit.war
+COPY bazel-bin/release.war /var/gerrit/bin/gerrit.war
 
 # Install plugins
 RUN mkdir /var/gerrit/plugins && \
   unzip -jo /var/gerrit/bin/gerrit.war WEB-INF/plugins/* -d /var/gerrit/plugins
 
 COPY opendevtheme.html /var/gerrit/plugins/opendevtheme.html
-
-FROM gerrit as gerrit-2
-
-# Only Gerrit 2.14, 2.15, and 2.16 need this COPY
-COPY javamelody-deps_deploy.jar /var/gerrit/lib/javamelody-deps_deploy.jar
diff --git a/playbooks/zuul/gerrit/run.yaml b/playbooks/zuul/gerrit/run.yaml
index 54e05745a2..56c7b3dce7 100644
--- a/playbooks/zuul/gerrit/run.yaml
+++ b/playbooks/zuul/gerrit/run.yaml
@@ -12,6 +12,28 @@
       vars:
         zuul_work_dir: /home/zuul/src/gerrit.googlesource.com/gerrit
 
+    # Bazel makes "convenience symlinks" [1] starting with bazel-* to
+    # the actual build output.  The problem is that we want to use the
+    # source tree as the context/build dir for our Docker container,
+    # and Docker refuses to follow symlinks for COPY commands; i.e. we
+    # can't copy stuff from the build output into the container.
+    # Therefore break the bazel-bin link and move to the top level so
+    # we can copy the built bits (jars, plugins, etc) into the
+    # container easily.
+    #
+    # [1] https://docs.bazel.build/versions/master/user-manual.html
+    - name: Make bazel-bin docker friendly
+      shell:
+        cmd: |
+          if [ ! -L bazel-bin ]; then
+            echo "bazel-bin not a symlink?"
+            exit 1
+          fi
+          target=$(readlink bazel-bin)
+          rm bazel-bin
+          mv $target bazel-bin
+        chdir:  /home/zuul/src/gerrit.googlesource.com/gerrit
+
     - name: Install OpenDev theme plugin
       copy:
         src: opendevtheme.html
diff --git a/roles/bazelisk-build/tasks/main.yaml b/roles/bazelisk-build/tasks/main.yaml
index 2a6c7f9dc1..e75e0f97a4 100644
--- a/roles/bazelisk-build/tasks/main.yaml
+++ b/roles/bazelisk-build/tasks/main.yaml
@@ -3,13 +3,11 @@
     set -x
     java -fullversion
     {{ bazelisk_executable }} version
-    {{ bazelisk_executable }} build --spawn_strategy=standalone --genrule_strategy=standalone {{ bazelisk_targets|join(' ') }}
-    if [[ -f bazel-bin/plugins/javamelody/javamelody-deps_deploy.jar ]] ; then
-        # versions 2.14, 2.15, and 2.16 generate this file
-        cp bazel-bin/plugins/javamelody/javamelody-deps_deploy.jar javamelody-deps_deploy.jar
-    fi
-    # release.war is a symlink. We want an actual file so that docker copy works right.
-    cp bazel-bin/release.war release.war
+    {{ bazelisk_executable }} \
+      build \
+      --spawn_strategy=standalone \
+      --genrule_strategy=standalone \
+      {{ bazelisk_targets|join(' ') }}
   args:
     executable: /bin/bash
     chdir: "{{ zuul_work_dir }}"