Add plugin build roles

Add two roles for building plugins:

copy-plugin-deps simply copies the external_plugin_deps.bzl file
into place if it exists.

bazelisk-build is a general bazelisk build role.  In this case,
we use it to build a plugin.

Change-Id: I7f9aa59c4f977edda15b40b73ae8e5c682de34a3
diff --git a/playbooks/gerrit-plugin/build.yaml b/playbooks/gerrit-plugin/build.yaml
new file mode 100644
index 0000000..b29276a
--- /dev/null
+++ b/playbooks/gerrit-plugin/build.yaml
@@ -0,0 +1,5 @@
+- name: Build a gerrit plugin
+  hosts: all
+  roles:
+    - copy-plugin-deps
+    - bazelisk-build
diff --git a/roles/bazelisk-build/README.rst b/roles/bazelisk-build/README.rst
new file mode 100644
index 0000000..fd07d01
--- /dev/null
+++ b/roles/bazelisk-build/README.rst
@@ -0,0 +1,27 @@
+Run bazelisk build
+
+Runs bazelisk build with the specified targets.
+
+**Role Variables**
+
+.. zuul:rolevar:: bazelisk_targets
+   :default: ""
+
+   The bazelisk targets to build.
+
+.. zuul:rolevar:: bazelisk_test_targets
+   :default: ""
+
+   The bazelisk targets to test.  ``bazelisk test`` will only be run
+   if this value is not the empty string.
+
+.. zuul:rolevar:: bazelisk_executable
+   :default: bazelisk
+
+   The path to the bazelisk executable.  See
+   :zuul:role:`ensure-bazelisk`.
+
+.. zuul:rolevar:: zuul_work_dir
+   :default: {{ ansible_user_dir }}/{{ zuul.project.src_dir}}
+
+   The working directory in which to run bazelisk.
diff --git a/roles/bazelisk-build/defaults/main.yaml b/roles/bazelisk-build/defaults/main.yaml
new file mode 100644
index 0000000..5760549
--- /dev/null
+++ b/roles/bazelisk-build/defaults/main.yaml
@@ -0,0 +1,4 @@
+bazelisk_targets: ""
+bazelisk_test_targets: ""
+bazelisk_executable: bazelisk
+zuul_work_dir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir}}"
diff --git a/roles/bazelisk-build/tasks/main.yaml b/roles/bazelisk-build/tasks/main.yaml
new file mode 100644
index 0000000..d3a6615
--- /dev/null
+++ b/roles/bazelisk-build/tasks/main.yaml
@@ -0,0 +1,15 @@
+- name: Run bazelisk build
+  shell: |
+    java -fullversion
+    {{ bazelisk_executable }} version
+    {{ bazelisk_executable }} build --spawn_strategy=standalone --genrule_strategy=standalone {{ bazelisk_targets }}
+  args:
+    executable: /bin/bash
+    chdir: "{{ zuul_work_dir }}"
+- name: Run bazelisk test
+  when: "bazelisk_test_targets != ''"
+  shell: |
+    {{ bazelisk_executable }} test {{ bazelisk_test_targets }}
+  args:
+    executable: /bin/bash
+    chdir: "{{ zuul_work_dir }}"
diff --git a/roles/copy-plugin-deps/README.rst b/roles/copy-plugin-deps/README.rst
new file mode 100644
index 0000000..9a86596
--- /dev/null
+++ b/roles/copy-plugin-deps/README.rst
@@ -0,0 +1,11 @@
+Copy plugin bazel deps
+
+If the plugin being built has an ``external_plugin_deps.bzl`` file,
+copy it into the Gerrit plugins directory.
+
+**Role Variables**
+
+.. zuul:rolevar:: gerrit_plugin
+   :default: zuul.project.short_name
+
+   The name of the plugin to be built.
diff --git a/roles/copy-plugin-deps/defaults/main.yaml b/roles/copy-plugin-deps/defaults/main.yaml
new file mode 100644
index 0000000..798daf7
--- /dev/null
+++ b/roles/copy-plugin-deps/defaults/main.yaml
@@ -0,0 +1 @@
+gerrit_plugin: "{{ zuul.project.short_name }}"
diff --git a/roles/copy-plugin-deps/tasks/main.yaml b/roles/copy-plugin-deps/tasks/main.yaml
new file mode 100644
index 0000000..d2fdbdb
--- /dev/null
+++ b/roles/copy-plugin-deps/tasks/main.yaml
@@ -0,0 +1,8 @@
+- name: Check if external plugin deps exists
+  stat:
+    path: "{{ ansible_user_dir }}/src/gerrit.googlesource.com/gerrit/plugins/{{ gerrit_plugin }}/external_plugin_deps.bzl"
+  register: deps_stat
+
+- name: Copy external plugin deps
+  command: "cp -f {{ ansible_user_dir }}/src/gerrit.googlesource.com/gerrit/plugins/{{ gerrit_plugin }}/external_plugin_deps.bzl {{ gerrit_root }}/plugins"
+  when: "deps_stat.stat.exists is true"
diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml
index 8107e4f..3a04c84 100644
--- a/zuul.d/jobs.yaml
+++ b/zuul.d/jobs.yaml
@@ -1,9 +1,11 @@
 - job:
     name: gerrit-base
+    nodeset: temp-debian
     description: |
-      Base job for building gerrit
+      Base job for building Gerrit
 
-      This job sets up all the repos which are required for a gerrit build.
+      This job sets up all the repos which are required for a gerrit
+      build.
     pre-run: playbooks/gerrit-base/pre.yaml
     required-projects:
       - gerrit
@@ -20,3 +22,31 @@
       - plugins/singleusergroup
       - plugins/webhooks
       - polymer-bridges
+
+- job:
+    name: gerrit-plugin-build
+    parent: gerrit-base
+    description: |
+      Builds a Gerrit plugin in-tree
+
+      Responds to these variables:
+
+      .. zuul:jobvar:: gerrit_plugin
+
+         The name of the plugin to build.  Defaults to the project
+         under test, but can be specified explicitly to build
+         cross-repo.
+
+      .. zuul:jobvar:: baselisk_targets
+
+         The bazelisk targets to build.  Defaults to the gerrit_plugin
+         specified above.
+
+      .. zuul:jobvar:: baselisk_test_targets
+
+         The bazelisk targets to test.
+    run: playbooks/gerrit-plugin/build.yaml
+    vars:
+      gerrit_plugin: "{{ zuul.project.short_name }}"
+      bazelisk_targets: "plugins/{{ gerrit_plugin }}:{{ gerrit_plugin }}"
+      zuul_work_dir: "{{ ansible_user_dir }}/src/gerrit.googlesource.com/gerrit"
diff --git a/zuul.d/project.yaml b/zuul.d/project.yaml
index 3645f44..aa8e103 100644
--- a/zuul.d/project.yaml
+++ b/zuul.d/project.yaml
@@ -5,3 +5,4 @@
         - test-ensure-java
         - test-gerrit-base
         - test-install-build-essential
+        - test-gerrit-plugin-build
diff --git a/zuul.d/test-jobs.yaml b/zuul.d/test-jobs.yaml
index 62211bc..4c946f2 100644
--- a/zuul.d/test-jobs.yaml
+++ b/zuul.d/test-jobs.yaml
@@ -35,10 +35,22 @@
     name: test-gerrit-base
     parent: gerrit-base
     run: playbooks/test-gerrit-base.yaml
-    nodeset: temp-debian
     files:
       - roles/ensure-bazelisk/.*
       - roles/ensure-java/.*
       - roles/prepare-gerrit-repos/.*
       - roles/install-build-essential/.*
       - playbooks/test-gerrit-base.yaml
+
+- job:
+    name: test-gerrit-plugin-build
+    parent: gerrit-plugin-build
+    required-projects:
+      - plugins/checks
+    vars:
+      gerrit_plugin: checks
+    files:
+      - roles/ensure-bazelisk/.*
+      - roles/prepare-gerrit-repos/.*
+      - roles/copy-plugin-deps/.*
+      - playbooks/gerrit-plugin/build.yaml