|
| 1 | +# Creating a simple Knative Build |
| 2 | + |
| 3 | +Use this page to learn how to create and then run a simple build in Knative. In |
| 4 | +this topic, you create a Knative Build configuration file for a simple app, |
| 5 | +deploy that build to Knative, and then test that the build completes. |
| 6 | + |
| 7 | +The following demonstrates the process of deploying and then testing that |
| 8 | +the build completed successfully. This sample build uses a hello-world-type app |
| 9 | +that uses [busybox](https://docs.docker.com/samples/library/busybox/) to simply |
| 10 | +print "*hello build*". |
| 11 | + |
| 12 | +Tip: See the |
| 13 | +[build code samples](builds.md#get-started-with-knative-build-samples) |
| 14 | +for examples of more complex builds, including code samples that use container |
| 15 | +images, authentication, and include multiple steps. |
| 16 | + |
| 17 | +## Before you begin |
| 18 | + |
| 19 | +Before you can run a Knative Build, you must have Knative installed in your |
| 20 | +Kubernetes cluster, and it must include the Knative Build component: |
| 21 | + |
| 22 | +* For details about installing a new instance of Knative in your Kubernetes |
| 23 | + cluster, see [Installing Knative](../install/README.md). |
| 24 | + |
| 25 | +* If you have a component of Knative installed and running, you must [ensure |
| 26 | + that the Knative Build component is also installed](installing-build-component.md). |
| 27 | + |
| 28 | +## Creating and running a build |
| 29 | + |
| 30 | +1. Create a configuration file named `build.yaml` that includes the following |
| 31 | + code. |
| 32 | + |
| 33 | + This `Build` resource definition includes a single "[step](builds.md#steps)" |
| 34 | + that performs the task of simply printing "*hello build*": |
| 35 | + |
| 36 | + ```yaml |
| 37 | + apiVersion: build.knative.dev/v1alpha1 |
| 38 | + kind: Build |
| 39 | + metadata: |
| 40 | + name: hello-build |
| 41 | + spec: |
| 42 | + steps: |
| 43 | + - name: hello |
| 44 | + image: busybox |
| 45 | + args: ['echo', 'hello', 'build'] |
| 46 | + ``` |
| 47 | +
|
| 48 | + Notice that this definition specifies `kind` as a `Build`, and that |
| 49 | + the name of this `Build` resource is `hello-build`. |
| 50 | + For more information about defining build configuration files, See the |
| 51 | + [`Build` reference topic](builds.md). |
| 52 | + |
| 53 | +1. Deploy the `build.yaml` configuration file and run the `hello-build` build on |
| 54 | + Knative by running the |
| 55 | + [`kubectl apply`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) |
| 56 | + command: |
| 57 | + |
| 58 | + ```shell |
| 59 | + kubectl apply -f build.yaml |
| 60 | + ``` |
| 61 | + |
| 62 | + Response: |
| 63 | + ```shell |
| 64 | + build "hello-build" created |
| 65 | + ``` |
| 66 | + |
| 67 | +1. Verify that the `hello-build` build resource has been created by running the |
| 68 | + [`kubectl get`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get) |
| 69 | + command: |
| 70 | + |
| 71 | + ```shell |
| 72 | + kubectl get builds |
| 73 | + ``` |
| 74 | + |
| 75 | + Response: |
| 76 | + ```shell |
| 77 | + NAME AGE |
| 78 | + hello-build 4s |
| 79 | + ``` |
| 80 | + |
| 81 | +1. After the build is created, you can run the following |
| 82 | + [`kubectl get`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get) |
| 83 | + command to retrieve details about the `hello-build` build, specifically, in |
| 84 | + which cluster and pod the build is running: |
| 85 | + |
| 86 | + ```shell |
| 87 | + kubectl get build hello-build -oyaml |
| 88 | + ``` |
| 89 | + |
| 90 | + Response: |
| 91 | + ```shell |
| 92 | + apiVersion: build.knative.dev/v1alpha1 |
| 93 | + kind: Build |
| 94 | +
|
| 95 | + ... |
| 96 | +
|
| 97 | + status: |
| 98 | + builder: Cluster |
| 99 | + cluster: |
| 100 | + namespace: default |
| 101 | + podName: hello-build-jx4ql |
| 102 | + conditions: |
| 103 | + - state: Complete |
| 104 | + status: "True" |
| 105 | + stepStates: |
| 106 | + - terminated: |
| 107 | + reason: Completed |
| 108 | + - terminated: |
| 109 | + reason: Completed |
| 110 | + ``` |
| 111 | + |
| 112 | + Notice that the values of `completed` indicate that the build was |
| 113 | + successful, and that `hello-build-jx4ql` is the pod where the build ran. |
| 114 | + |
| 115 | + Tip: You can also retrieve the `podName` by running the following command: |
| 116 | + |
| 117 | + ```shell |
| 118 | + kubectl get build hello-build -ojsonpath={.status.cluster.podName} |
| 119 | + ``` |
| 120 | + |
| 121 | +1. Optional: Run the following |
| 122 | + [`kubectl get`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get) |
| 123 | + command to retrieve details about the `hello-build-[ID]` pod, including |
| 124 | + the name of the |
| 125 | + [Init container](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/): |
| 126 | + |
| 127 | + ```shell |
| 128 | + kubectl get pod hello-build-[ID] -oyaml |
| 129 | + ``` |
| 130 | + where `[ID]` is the suffix of your pod name, for example |
| 131 | + `hello-build-jx4ql`. |
| 132 | + |
| 133 | + The response of this command includes a lot of detail, as well as |
| 134 | + the `build-step-hello` name of the Init container. |
| 135 | + |
| 136 | + Tip: The name of the Init container is determined by the `name` that is |
| 137 | + specified in the `steps` field of the build configuration file, for |
| 138 | + example `build-step-[ID]`. |
| 139 | + |
| 140 | +1. To verify that your build performed the single task of printing |
| 141 | + "*hello build*", you can run the |
| 142 | + [`kubectl logs`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs) |
| 143 | + command to retrieve the log files from the `build-step-hello` Init container |
| 144 | + in the `hello-build-[ID]` pod: |
| 145 | + |
| 146 | + ```shell |
| 147 | + kubectl logs $(kubectl get build hello-build -ojsonpath={.status.cluster.podName}) -c build-step-hello |
| 148 | + ``` |
| 149 | + |
| 150 | + Response: |
| 151 | + ```shell |
| 152 | + hello build |
| 153 | + ``` |
| 154 | + |
| 155 | +### Learn more |
| 156 | + |
| 157 | +To learn more about the objects and commands used in this topic, see: |
| 158 | + |
| 159 | +* [Knative `Build` resources](builds.md) |
| 160 | +* [Kubernetes Init containers](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) |
| 161 | +* [Kubernetes kubectl CLI](https://kubernetes.io/docs/reference/kubectl/kubectl/) |
| 162 | + |
| 163 | +For information about contributing to the Knative Build project, see the |
| 164 | +[Knative Build code repo](https://github.com/knative/build/). |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +Except as otherwise noted, the content of this page is licensed under the |
| 169 | +[Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), |
| 170 | +and code samples are licensed under the |
| 171 | +[Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). |
0 commit comments