From 89f68c5ca072fe76f33f7946a901f26d6a2328c1 Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Sat, 16 Dec 2017 10:09:46 -0600
Subject: [PATCH] Fetch built web content in javascript jobs

If a javascript job produces web content that should be deployed (such
as zuul-web or storyboard-webclient) we should collect that in the post
step of the job and put it into the html/ directory.

While in there, add a bunch of get_*: false entries for the stat calls.
Calculating the md5 and whatnot is expensive, and we should really
always set them to false unless we're *using* the hash values.

Change-Id: I9b96656aa5f225e4b069a78442cf0159cadc9009
---
 roles/fetch-javascript-output/README.rst      |  8 ++-
 .../defaults/main.yaml                        |  1 +
 roles/fetch-javascript-output/tasks/main.yaml | 32 +++++++++++
 zuul.yaml                                     | 53 ++++++++++++++++++-
 4 files changed, 91 insertions(+), 3 deletions(-)

diff --git a/roles/fetch-javascript-output/README.rst b/roles/fetch-javascript-output/README.rst
index cf8518f2e..150b24991 100644
--- a/roles/fetch-javascript-output/README.rst
+++ b/roles/fetch-javascript-output/README.rst
@@ -1,4 +1,4 @@
-Collect logs from a javascript build
+Collect outputs from a javascript build
 
 **Role Variables**
 
@@ -6,3 +6,9 @@ Collect logs from a javascript build
    :default: {{ zuul.project.src_dir }}
 
    Directory to work in
+
+.. zuul:rolevar:: javascript_content_dir
+   :default: dist
+
+   Directory, relative to zuul_work_dir, in which javascript output content
+   is to be found.
diff --git a/roles/fetch-javascript-output/defaults/main.yaml b/roles/fetch-javascript-output/defaults/main.yaml
index 9739eb171..642843fe6 100644
--- a/roles/fetch-javascript-output/defaults/main.yaml
+++ b/roles/fetch-javascript-output/defaults/main.yaml
@@ -1 +1,2 @@
 zuul_work_dir: "{{ zuul.project.src_dir }}"
+javascript_content_dir: dist
diff --git a/roles/fetch-javascript-output/tasks/main.yaml b/roles/fetch-javascript-output/tasks/main.yaml
index e173556b9..1085e9840 100644
--- a/roles/fetch-javascript-output/tasks/main.yaml
+++ b/roles/fetch-javascript-output/tasks/main.yaml
@@ -17,11 +17,17 @@
 - name: Check for yarn.lock
   stat:
     path: "{{ zuul_work_dir }}/yarn.lock"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
   register: yarn_lock
 
 - name: Check for shrinkwrap
   stat:
     path: "{{ zuul_work_dir }}/npm-shrinkwrap.json"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
   when: not yarn_lock.stat.exists
   register: shrinkwrap
 
@@ -48,6 +54,9 @@
 - name: Check for reports
   stat:
     path: "{{ zuul_work_dir }}/reports"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
   register: reports_stat
 
 - name: Collect npm reports
@@ -61,6 +70,9 @@
 - name: Check for karma.subunit files
   stat:
     path: "{{ zuul_work_dir }}/karma.subunit"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
   register: karma_stat
 
 - name: Collect karma subunit files
@@ -75,6 +87,9 @@
   when: not yarn_lock.stat.exists
   stat:
     path: "{{ zuul_work_dir }}/npm-shrinkwrap.json"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
   register: shrinkwrap_final
 
 - name: Collect shrinkwrap file
@@ -86,3 +101,20 @@
   when:
     - not yarn_lock.stat.exists
     - shrinkwrap_final.stat.exists
+
+- name: Check for built output
+  stat:
+    path: "{{ zuul_work_dir }}/{{ javascript_content_dir }}"
+    get_checksum: false
+    get_mime: false
+    get_md5: false
+  register: javascript_output
+
+- name: Collect javascript output
+  synchronize:
+    src: "{{ zuul_work_dir }}/{{ javascript_content_dir }}/"
+    dest: "{{ log_path }}/html/"
+    mode: pull
+    copy_links: true
+    verify_host: true
+  when: javascript_output.stat.exists
diff --git a/zuul.yaml b/zuul.yaml
index de191c624..7777dc69a 100644
--- a/zuul.yaml
+++ b/zuul.yaml
@@ -243,7 +243,7 @@
       Responds to these variables:
 
       .. zuul:jobvar:: npm_command
-         :default: test
+         :default: build
 
          Command to pass to npm.
 
@@ -256,11 +256,16 @@
          :default: {{ zuul.project.src_dir }}
 
          Path to operate in.
+
+      .. zuul:jobvar: javascript_content_dir
+         :default: dist
+
+         Directory, relative to zuul_work_dir, holding build content.
     pre-run: playbooks/javascript/pre.yaml
     run: playbooks/javascript/run.yaml
     post-run: playbooks/javascript/post.yaml
     vars:
-      npm_command: test
+      npm_command: build
 
 - job:
     name: build-javascript-tarball
@@ -279,9 +284,43 @@
          :default: {{ zuul.project.src_dir }}
 
          Path to operate in.
+
+      .. zuul:jobvar: javascript_content_dir
+         :default: dist
+
+         Directory, relative to zuul_work_dir, holding build content.
     vars:
       npm_command: pack
 
+- job:
+    name: build-javascript-content
+    parent: nodejs-npm
+    description: |
+      Build javascript web content as it should be deployed.
+
+      Responds to these variables:
+
+      .. zuul:jobvar:: npm_command
+         :default: build
+
+         Command to pass to npm.
+
+      .. zuul:jobvar:: node_version
+         :default: 6
+
+         The version of Node to use.
+
+      .. zuul:jobvar: zuul_work_dir
+         :default: {{ zuul.project.src_dir }}
+
+         Path to operate in.
+
+      .. zuul:jobvar: javascript_content_dir
+         :default: dist
+
+         Directory, relative to zuul_work_dir, holding build content.
+    success-url: html/
+
 - job:
     name: nodejs-npm-run-test
     description: |
@@ -299,6 +338,11 @@
          :default: {{ zuul.project.src_dir }}
 
          Path to operate in.
+
+      .. zuul:jobvar: javascript_content_dir
+         :default: dist
+
+         Directory, relative to zuul_work_dir, holding build content.
     pre-run: playbooks/javascript/pre-test.yaml
     run: playbooks/javascript/run.yaml
     post-run: playbooks/javascript/post.yaml
@@ -322,6 +366,11 @@
          :default: {{ zuul.project.src_dir }}
 
          Path to operate in.
+
+      .. zuul:jobvar: javascript_content_dir
+         :default: dist
+
+         Directory, relative to zuul_work_dir, holding build content.
     vars:
       npm_command: lint