Add support for building Scala-based plugins

Introduce support for the first Gerrit plugin written in Scala
and for potentially other future plugins.

Change-Id: I32bcc0640854dfea01617e8408708b985d863dc1
diff --git a/jenkins-docker/Makefile b/jenkins-docker/Makefile
index 5e214a3..5cb3527 100644
--- a/jenkins-docker/Makefile
+++ b/jenkins-docker/Makefile
@@ -1,4 +1,4 @@
-DIRS=master slave slave-buck slave-mvn slave-bazel
+DIRS=master slave slave-buck slave-mvn slave-bazel slave-sbt
 
 default: start
 
diff --git a/jenkins-docker/master/config.xml b/jenkins-docker/master/config.xml
index 909295b..283d65c 100644
--- a/jenkins-docker/master/config.xml
+++ b/jenkins-docker/master/config.xml
@@ -160,6 +160,48 @@
           <removeVolumes>false</removeVolumes>
           <pullStrategy>PULL_LATEST</pullStrategy>
         </com.nirima.jenkins.plugins.docker.DockerTemplate>
+        <com.nirima.jenkins.plugins.docker.DockerTemplate>
+          <configVersion>2</configVersion>
+          <labelString></labelString>
+          <launcher class="com.nirima.jenkins.plugins.docker.launcher.DockerComputerSSHLauncher">
+            <sshConnector plugin="ssh-slaves@1.11">
+              <port>22</port>
+              <jvmOptions></jvmOptions>
+              <javaPath></javaPath>
+              <maxNumRetries>0</maxNumRetries>
+              <retryWaitTime>0</retryWaitTime>
+            </sshConnector>
+          </launcher>
+          <remoteFsMapping></remoteFsMapping>
+          <remoteFs>/home/jenkins</remoteFs>
+          <instanceCap>5</instanceCap>
+          <mode>NORMAL</mode>
+          <retentionStrategy class="com.nirima.jenkins.plugins.docker.strategy.DockerOnceRetentionStrategy">
+            <idleMinutes>10</idleMinutes>
+            <idleMinutes defined-in="com.nirima.jenkins.plugins.docker.strategy.DockerOnceRetentionStrategy">10</idleMinutes>
+          </retentionStrategy>
+          <numExecutors>1</numExecutors>
+          <dockerTemplateBase>
+            <image>gerritforge/gerrit-ci-slave-bazel-sbt</image>
+            <dockerCommand></dockerCommand>
+            <lxcConfString>sbt</lxcConfString>
+            <hostname></hostname>
+            <dnsHosts/>
+            <volumes/>
+            <volumesFrom2/>
+            <environment/>
+            <bindPorts></bindPorts>
+            <bindAllPorts>false</bindAllPorts>
+            <privileged>true</privileged>
+            <tty>false</tty>
+            <extraHosts class="java.util.Collections$UnmodifiableRandomAccessList" resolves-to="java.util.Collections$UnmodifiableList">
+              <c class="list"/>
+              <list reference="../c"/>
+            </extraHosts>
+          </dockerTemplateBase>
+          <removeVolumes>false</removeVolumes>
+          <pullStrategy>PULL_LATEST</pullStrategy>
+        </com.nirima.jenkins.plugins.docker.DockerTemplate>
       </templates>
       <serverUrl>unix:///var/run/docker.sock</serverUrl>
       <connectTimeout>30</connectTimeout>
diff --git a/jenkins-docker/slave-sbt/Dockerfile b/jenkins-docker/slave-sbt/Dockerfile
new file mode 100644
index 0000000..00efa9f
--- /dev/null
+++ b/jenkins-docker/slave-sbt/Dockerfile
@@ -0,0 +1,19 @@
+FROM gerritforge/gerrit-ci-slave-bazel
+
+# SBT Setup
+RUN echo "deb https://dl.bintray.com/sbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list && \
+    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 642AC823 && \
+    apt-get update && \
+    apt-get -y install --no-install-recommends sbt && \
+    rm -rf /etc/apt/sources.list.d/* && \
+    rm -rf /var/lib/apt/lists/*
+
+# SBT warm-up
+RUN cd /tmp && echo hello | sbt new sbt/scala-seed.g8
+RUN echo 'addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.4")' >> /tmp/hello/project/plugins.sbt
+RUN chown -R jenkins: /tmp/hello
+
+USER jenkins
+RUN cd /tmp/hello && sbt compile test assembly
+
+USER root
diff --git a/jenkins-docker/slave-sbt/Makefile b/jenkins-docker/slave-sbt/Makefile
new file mode 100644
index 0000000..2b41e1c
--- /dev/null
+++ b/jenkins-docker/slave-sbt/Makefile
@@ -0,0 +1,13 @@
+NO_CACHE=false
+IMAGE=gerritforge/gerrit-ci-slave-bazel-sbt
+
+# Targets
+
+build:
+	docker build --no-cache=$(NO_CACHE) -t ${IMAGE} .
+
+clean:
+	docker rmi -f ${IMAGE}
+
+.PHONY: clean image
+
diff --git a/jenkins/gerrit-sbt-build-plugin.sh b/jenkins/gerrit-sbt-build-plugin.sh
new file mode 100644
index 0000000..c073256
--- /dev/null
+++ b/jenkins/gerrit-sbt-build-plugin.sh
@@ -0,0 +1,25 @@
+#!/bin/bash -e
+
+if [ "{branch}" == "master" ]
+then
+  git read-tree -u --prefix=gerrit gerrit/{branch}
+  . set-java.sh 8
+
+  pushd gerrit
+  bazel build api
+  ./tools/maven/api.sh install
+  popd
+fi
+
+sbt -no-colors compile test assembly
+
+# Extract version information
+PLUGIN_JARS=$(find . -name '{name}*jar')
+for jar in $PLUGIN_JARS
+do
+  PLUGIN_VERSION=$(git describe  --always origin/{branch})
+  echo -e "Implementation-Version: $PLUGIN_VERSION" > MANIFEST.MF
+  jar ufm $jar MANIFEST.MF && rm MANIFEST.MF
+
+  echo "$PLUGIN_VERSION" > $jar-version
+done
diff --git a/jenkins/gerrit-sbt-plugin-gerrit-support.yaml b/jenkins/gerrit-sbt-plugin-gerrit-support.yaml
new file mode 100644
index 0000000..1acc12d
--- /dev/null
+++ b/jenkins/gerrit-sbt-plugin-gerrit-support.yaml
@@ -0,0 +1,6 @@
+- project:
+    name: gerrit-support
+    branch:
+      - master
+    jobs:
+      - 'plugin-{name}-sbt-{branch}':
diff --git a/jenkins/gerrit-sbt-plugin-template.yaml b/jenkins/gerrit-sbt-plugin-template.yaml
new file mode 100644
index 0000000..962e9df
--- /dev/null
+++ b/jenkins/gerrit-sbt-plugin-template.yaml
@@ -0,0 +1,47 @@
+- job-template:
+    name: 'plugin-{name}-sbt-{branch}'
+    description: "Plugin {name} {branch} branch build with Sbt\n"
+    node: sbt
+    wrappers:
+      - timeout:
+          timeout: 30
+          fail: true
+      - timestamps
+      - raw:
+          xml: <jenkins.plugins.logstash.LogstashBuildWrapper plugin="logstash@1.2.0"/>
+    properties:
+       - build-discarder:
+           days-to-keep: 20
+           artifact-days-to-keep: 20
+       - authorization:
+           anonymous:
+             - job-discover
+             - job-read
+             - job-extended-read
+    scm:
+      - git:
+         remotes:
+           - origin:
+               url: https://gerrit.googlesource.com/plugins/{name}
+           - gerrit:
+               url: https://gerrit.googlesource.com/gerrit
+         branches:
+           - origin/{branch}
+         submodule:
+           recursive: true
+         skip-tag: true
+
+    triggers:
+      - pollscm:
+          cron: 'H/50 * * * *'
+
+    builders:
+      - shell:
+         !include-raw: gerrit-sbt-build-plugin.sh
+    publishers:
+      - archive:
+         artifacts: '**/target/**/{name}*.jar*'
+
+    wrappers:
+      - raw:
+          xml: <jenkins.plugins.logstash.LogstashBuildWrapper plugin="logstash@1.2.0"/>