From 35da394f531ad5aaaba082e67c83b08dc405db3b Mon Sep 17 00:00:00 2001
From: Monty Taylor <mordred@inaugust.com>
Date: Tue, 6 Mar 2018 13:30:35 -0600
Subject: [PATCH] Add role for fetching javascript built content

Built content and source tarballs are different. Have a role that
handles uploading built content.

Append -content to the tarball so that a project can publish both a
source tarball and a built content tarball without stepping on each
other.

Change-Id: I33dff8080018bda281a00e648468de80d3ecd679
---
 .../README.rst                                | 11 +++++++
 .../defaults/main.yaml                        |  1 +
 .../tasks/main.yaml                           | 29 +++++++++++++++++++
 3 files changed, 41 insertions(+)
 create mode 100644 roles/fetch-javascript-content-tarball/README.rst
 create mode 100644 roles/fetch-javascript-content-tarball/defaults/main.yaml
 create mode 100644 roles/fetch-javascript-content-tarball/tasks/main.yaml

diff --git a/roles/fetch-javascript-content-tarball/README.rst b/roles/fetch-javascript-content-tarball/README.rst
new file mode 100644
index 000000000..f8ff1d31e
--- /dev/null
+++ b/roles/fetch-javascript-content-tarball/README.rst
@@ -0,0 +1,11 @@
+Fetch a Javascript content tarball back to be published.
+
+A content tarball is one that contains built javascript/css artifacts,
+such as but not limited to those produced by the webpack ArchivePlugin.
+
+**Role Variables**
+
+.. zuul:rolevar:: zuul_work_dir
+   :default: {{ zuul.project.src_dir }}
+
+   Directory to work in.
diff --git a/roles/fetch-javascript-content-tarball/defaults/main.yaml b/roles/fetch-javascript-content-tarball/defaults/main.yaml
new file mode 100644
index 000000000..9739eb171
--- /dev/null
+++ b/roles/fetch-javascript-content-tarball/defaults/main.yaml
@@ -0,0 +1 @@
+zuul_work_dir: "{{ zuul.project.src_dir }}"
diff --git a/roles/fetch-javascript-content-tarball/tasks/main.yaml b/roles/fetch-javascript-content-tarball/tasks/main.yaml
new file mode 100644
index 000000000..b02ced804
--- /dev/null
+++ b/roles/fetch-javascript-content-tarball/tasks/main.yaml
@@ -0,0 +1,29 @@
+- name: Rename tarball for uploading
+  shell: |
+    mkdir -p dist
+    cp *.tgz dist/{{ zuul.project.short_name }}-content-latest.tar.gz
+  args:
+    chdir: "{{ zuul_work_dir }}"
+  tags:
+    # Ignore ANSIBLE0007: No need to use file module instead of mkdir
+    - skip_ansible_lint
+
+- name: Rename tagged tarball for uploading
+  when: zuul.tag is defined
+  shell: |
+    cp *.tgz dist/{{ zuul.project.short_name }}-content-{{ zuul.tag }}.tar.gz
+  args:
+    chdir: "{{ zuul_work_dir }}"
+
+- name: Ensure artifacts directory exists
+  file:
+    path: "{{ zuul.executor.work_root }}/artifacts"
+    state: directory
+  delegate_to: localhost
+
+- name: Collect artifacts
+  synchronize:
+    dest: "{{ zuul.executor.work_root }}/artifacts/"
+    mode: pull
+    src: "{{ zuul_work_dir }}/dist/"
+    verify_host: true