简体   繁体   中英

Cloud Build kubectl - How to Apply Output of Previous Step to Kubernetes Cluster

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

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

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

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

Update

I've tried the following:

- name: gcr.io/cloud-builders/bazel
  entrypoint: /bin/bash
  args:
    [
      "bazel",
      "run",
      "//:kubernetes",
      " > 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"

But then I get this error:

Running: kubectl apply -f kubernetes.yaml
error: the path "kubernetes.yaml" does not exist

Volume mount the same directory (not file) into both steps.

Pipe the Bazel command output to a file.

Reference that file in the kubectl apply --filename= step

Example

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"

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

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

Output:

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

Here's how to mount the volume:

https://cloud.google.com/cloud-build/docs/build-config#volumes

Basically add:

  volumes:
  - name: 'vol1'
    path: '/persistent_volume'

Then reference full path /persistent_volume/filename/ when writing / reading to your file.

As everyone already suggested here use volumes .

Tweak your cloudbuild.yaml file like this:

- name: gcr.io/cloud-builders/bazel
  entrypoint: /bin/bash
  args:
    [
      "bazel",
      "run",
      "//:kubernetes",
      " > /workspace/kubernetes.yaml",
    ]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM