enterprise: Add a job to build an unstable image for enterprise

This makes use of the new dist target in the build system to allow for building
zip files containing all of the relevant utilities.

Change-Id: I39fe9dae7ab677097f0220db00ee427491604622
diff --git a/cicd/jobs/images.jenkins b/cicd/jobs/images.jenkins
new file mode 100644
index 0000000..db65fc1
--- /dev/null
+++ b/cicd/jobs/images.jenkins
@@ -0,0 +1,27 @@
+#!/usr/bin/env groovy
+
+def boards = [
+    "enterprise"
+]
+
+boards.each {
+    def boardName = it
+
+    pipelineJob("image.unstable.${boardName}") {
+        definition {
+            cpsScm {
+                scm {
+                    git {
+                        remote {
+                            url('https://coral.googlesource.com/gke-jenkins')
+                        }
+
+                        branches('*/master')
+                    }
+                }
+
+                scriptPath("cicd/pipelines/${boardName}/image_unstable.jenkins")
+            }
+        }
+    }
+}
diff --git a/cicd/pipelines/enterprise/image_unstable.jenkins b/cicd/pipelines/enterprise/image_unstable.jenkins
new file mode 100644
index 0000000..dcacf6e
--- /dev/null
+++ b/cicd/pipelines/enterprise/image_unstable.jenkins
@@ -0,0 +1,54 @@
+#!/usr/bin/env groovy
+
+library 'functions'
+
+def workspacePath = "/home/jenkins/workspace"
+def buildLabel = "${targetName}-${UUID.randomUUID().toString()}"
+def sourcePath = "${workspacePath}/src"
+
+// FIXME(jtgans): Get rid of privileged! This is a security risk!
+def jnlpContainer = containerTemplate(name: 'jnlp',
+                                      image: 'jenkins/jnlp-slave:alpine')
+def debianContainer = containerTemplate(name: 'debian',
+                                        image: 'gcr.io/mendel-linux-cloud-infra/mendel-builder:latest',
+                                        command: 'cat',
+                                        args: '',
+                                        ttyEnabled: true,
+                                        privileged: true)
+def aptlyVolume = persistentVolumeClaim(claimName: 'aptly-state', mountPath: '/var/lib/aptly')
+def envVars = [
+    'IS_JENKINS=true',
+    'RELEASE_NAME=unstable'
+]
+
+podTemplate(label: buildLabel, containers: [jnlpContainer, debianContainer], volumes: [aptlyVolume], envVars: envVars) {
+    node(buildLabel) {
+        dir(sourcePath) {
+            container('debian') {
+                stage('Init') {
+                    functions.initSourceTree(boardName, false /* needsNative */)
+                }
+
+                stage('Build') {
+                    def nprocs = sh(returnStdout: true, script: 'nproc').trim()
+                    sh "bash -c 'source build/setup.sh; m -j${nprocs} prereqs dist; exit \$?'"
+                }
+
+                stage('Deploy') {
+                    def imagePath = "/var/lib/aptly/images/enterprise/unstable"
+                    def files = findFiles(glob: "out/**/dist/*")
+
+                    if (files.size() == 0) {
+                        error 'No dist files generated. Assuming failed build.'
+                    }
+
+                    sh "mkdir -p ${imagePath}"
+
+                    for (file in files) {
+                        sh "cp ${file} ${imagePath}"
+                    }
+                }
+            }
+        }
+    }
+}