From e1985ce7352a46e1602a4e115df1b2037c7345d7 Mon Sep 17 00:00:00 2001
From: Mads Hartmann <mads379@gmail.com>
Date: Mon, 19 Sep 2022 14:00:08 +0000
Subject: [PATCH] Add withLocalPreview

---
 .github/pull_request_template.md       |  6 ++---
 .werft/jobs/build/build-and-publish.ts |  1 +
 .werft/jobs/build/job-config.ts        |  3 +++
 install/preview/BUILD.js               | 36 ++++++++++++++++++++++++++
 install/preview/BUILD.yaml             | 15 -----------
 5 files changed, 42 insertions(+), 19 deletions(-)
 create mode 100644 install/preview/BUILD.js

diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index 42cfca3b3ed9ec..4f809299f37d86 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -30,11 +30,9 @@ Does this PR require updates to the documentation at www.gitpod.io/docs?
 -->
 
 ## Werft options:
-<!--
-Optional annotations to add to the werft job.
 
-* with-preview - whether to create a preview environment for this PR
--->
+- [ ] /werft with-local-preview
+      If enabled this will build `install/preview`
 - [ ] /werft with-preview
 - [ ] /werft with-integration-tests=all
       Valid options are `all`, `workspace`, `webapp`, `ide`
diff --git a/.werft/jobs/build/build-and-publish.ts b/.werft/jobs/build/build-and-publish.ts
index 3ac770f745e3e6..88c253ec80ba01 100644
--- a/.werft/jobs/build/build-and-publish.ts
+++ b/.werft/jobs/build/build-and-publish.ts
@@ -56,6 +56,7 @@ export async function buildAndPublish(werft: Werft, jobConfig: JobConfig) {
         REPLICATED_APP: process.env.REPLICATED_APP,
         npmPublishTrigger: publishToNpm ? Date.now().toString() : "false",
         jbMarketplacePublishTrigger: publishToJBMarketplace ? Date.now().toString() : "false",
+        withLocalPreview: jobConfig.withLocalPreview,
     }).map(([key, value]) => `-D${key}=${value}`).join(" ");
 
     const buildFlags = [
diff --git a/.werft/jobs/build/job-config.ts b/.werft/jobs/build/job-config.ts
index b0fb97ed555aaf..b774f2bb1cc722 100644
--- a/.werft/jobs/build/job-config.ts
+++ b/.werft/jobs/build/job-config.ts
@@ -30,6 +30,7 @@ export interface JobConfig {
     withSelfHostedPreview: boolean;
     withObservability: boolean;
     withPayment: boolean;
+    withLocalPreview: boolean;
     workspaceFeatureFlags: string[];
     previewEnvironment: PreviewEnvironmentConfig;
     repository: Repository;
@@ -97,6 +98,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
     const withPayment = "with-payment" in buildConfig && !mainBuild;
     const withObservability = "with-observability" in buildConfig && !mainBuild;
     const withLargeVM = "with-large-vm" in buildConfig && !mainBuild;
+    const withLocalPreview = "with-local-preview" in buildConfig || mainBuild
     const repository: Repository = {
         owner: context.Repository.owner,
         repo: context.Repository.repo,
@@ -148,6 +150,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
         withPayment,
         withUpgradeTests,
         withSelfHostedPreview,
+        withLocalPreview,
         workspaceFeatureFlags,
         withLargeVM,
     };
diff --git a/install/preview/BUILD.js b/install/preview/BUILD.js
new file mode 100644
index 00000000000000..1cfe8b83283d82
--- /dev/null
+++ b/install/preview/BUILD.js
@@ -0,0 +1,36 @@
+const packages = []
+
+const withLocalPreview = args.withLocalPreview == "true"
+
+if (withLocalPreview) {
+    const docker = {
+        "name": "docker",
+        "type": "docker",
+        "deps": [
+          "install/installer:app",
+          "install/preview/prettylog:app"
+        ],
+        "argdeps": [
+            "imageRepoBase"
+        ],
+        "srcs": [
+            "entrypoint.sh",
+            "manifests/*.yaml"
+        ],
+        "config": {
+          "dockerfile": "leeway.Dockerfile",
+          "image": [`${args.imageRepoBase}/local-preview:${args.version}`]
+        }
+    }
+    packages.push(docker)
+} else {
+  packages.push({
+    "name": "docker",
+    "type": "generic",
+    "config": {
+      "commands": [
+        ["echo", "Skipping build of install/preview:docker as -DwithLocalPreview was not set to true"]
+      ]
+    }
+  })
+}
diff --git a/install/preview/BUILD.yaml b/install/preview/BUILD.yaml
index 8c9dad0069307a..e69de29bb2d1d6 100644
--- a/install/preview/BUILD.yaml
+++ b/install/preview/BUILD.yaml
@@ -1,15 +0,0 @@
-packages:
-  - name: docker
-    type: docker
-    deps:
-      - install/installer:app
-      - install/preview/prettylog:app
-    argdeps:
-      - imageRepoBase
-    srcs:
-      - "entrypoint.sh"
-      - "manifests/*.yaml"
-    config:
-      dockerfile: leeway.Dockerfile
-      image:
-        - ${imageRepoBase}/local-preview:${version}