<p>I have a simple <code>cloudbuild.yaml</code> file which runs a Bazel command. This command returns a Kubernetes configuration in form as a log output.</p>

<p>My goal is to take the output of the first step and apply it to my Kubernetes cluster.</p>

<pre><code>steps:
  - name: gcr.io/cloud-builders/bazel
    args: ["run", "//:kubernetes"]

  - name: "gcr.io/cloud-builders/kubectl"
    args: ["apply", "&lt;log output of previous step&gt;"]
    env:
      - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
      - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"
</code></pre>

<h1>Update</h1>

<p>I've tried the following:</p>

<pre><code>- name: gcr.io/cloud-builders/bazel
  entrypoint: /bin/bash
  args:
    [
      "bazel",
      "run",
      "//:kubernetes",
      " &gt; kubernetes.yaml",
    ]

- name: "gcr.io/cloud-builders/kubectl"
  args: ["apply", "-f", "kubernetes.yaml"]
  env:
    - "CLOUDSDK_COMPUTE_ZONE=europe-west3-a"
    - "CLOUDSDK_CONTAINER_CLUSTER=cents-ideas"
</code></pre>

<p>But then I get this error:</p>

<pre><code>Running: kubectl apply -f kubernetes.yaml
error: the path "kubernetes.yaml" does not exist
</code></pre>

<p>Volume mount the same directory (not file) into both steps.</p>

<p>Pipe the Bazel command output to a file.</p>

<p>Reference that file in the <code>kubectl apply --filename=</code> step</p>

<h3>Example</h3>

<pre><code>options:
  volumes:
    - name: test
      path: /test
steps:
  - name: busybox
    args:
      - "ls"
      - "-l"
      - "/test"
  - name: busybox
    entrypoint: "/bin/sh"
    args:
      - "-c"
      - "touch /test/freddie"
  - name: busybox
    args:
      - "ls"
      - "-l"
      - "/test"
  - name: busybox
    args:
      - "cp"
      - "/test/freddie"
      - "/workspace"
  - name: busybox
    args:
      - "ls"
      - "-l"
      - "/workspace"
</code></pre>

<p>Using <code>options</code> to define <code>volumes</code> applies the volume to all steps; you may alternatively simply repeat the <code>volumes</code> in each step.</p>

<p>The example -- hopefully -- shows how to use both the default <code>/workspace</code> and a user-defined <code>/test</code> volume to create a file in a volume (and to copy this file to the default <code>/workspace</code> volume to prove that it is added.</p>

<p>Output:</p>

<pre><code>BUILD
Starting Step #0
Step #0: Already have image: busybox
Step #0: total 0
Finished Step #0
Starting Step #1
Step #1: Already have image: busybox
Finished Step #1
Starting Step #2
Step #2: Already have image: busybox
Step #2: total 0
Step #2: -rw-r--r--    1 root     root             0 Feb  4 17:53 freddie
Finished Step #2
Starting Step #3
Step #3: Already have image: busybox
Finished Step #3
Starting Step #4
Step #4: Already have image: busybox
Step #4: total 4
Step #4: -rw-r--r--    1 1000     1000           460 Feb  4 17:53 cloudbuild.yaml
Step #4: -rw-r--r--    1 root     root             0 Feb  4 17:53 freddie
Finished Step #4
PUSH
DONE
</code></pre>
