|
| 1 | + |
| 2 | +# Building with Buildkit |
| 3 | + |
| 4 | +Before starting, make sure you have Tekton and Shipwright Build installed. |
| 5 | + |
| 6 | +See the [Try It!](../../README.md#try-it) section for more information. |
| 7 | + |
| 8 | +## Getting Started |
| 9 | + |
| 10 | +### Registry Authentication |
| 11 | + |
| 12 | +For this tutorial, we will require to create a `tutorial-secret` Kubernetes secret to access a [DockerHub](https://hub.docker.com/) registry, as follows: |
| 13 | + |
| 14 | +```sh |
| 15 | +$ REGISTRY_SERVER=https://index.docker.io/v1/ REGISTRY_USER=<your_registry_user> REGISTRY_PASSWORD=<your_registry_password> |
| 16 | +$ kubectl create secret docker-registry tutorial-secret --docker-server= $REGISTRY_SERVER --docker-username= $REGISTRY_USER --docker-password= $REGISTRY_PASSWORD [email protected] |
| 17 | +``` |
| 18 | + |
| 19 | +_Note_: For more information about authentication, please refer to the related [docs](/docs/development/authentication.md). |
| 20 | + |
| 21 | +## Create the strategy |
| 22 | + |
| 23 | +Ensure all strategies are in place, see the [Try It!](../../README.md#try-it) section for more information. |
| 24 | + |
| 25 | +```sh |
| 26 | +$ kubectl get cbs |
| 27 | +NAME AGE |
| 28 | +buildah 96s |
| 29 | +buildkit 78s |
| 30 | +buildpacks-v3 96s |
| 31 | +kaniko 96s |
| 32 | +ko 96s |
| 33 | +source-to-image 96s |
| 34 | +``` |
| 35 | + |
| 36 | +_Note_: For more information about strategies, please refer to the related [docs](/docs/buildstrategies.md). |
| 37 | + |
| 38 | +## Creating a Build |
| 39 | + |
| 40 | +For the Build definition, we will require the following: |
| 41 | + |
| 42 | +- A GitHub repository containing a Go [application](https://github.com/shipwright-io/sample-go/tree/main/docker-build) that requires a `Dockerfile`. |
| 43 | +- The `tutorial-secret` we just created. |
| 44 | +- The `buildkit` ClusterBuildStrategy. |
| 45 | + |
| 46 | +Let's apply our Build and wait for it to be ready: |
| 47 | + |
| 48 | +```bash |
| 49 | +$ export REGISTRY_ORG=<your_registry_org> |
| 50 | +$ cat <<EOF | kubectl apply -f - |
| 51 | +apiVersion: shipwright.io/v1alpha1 |
| 52 | +kind: Build |
| 53 | +metadata: |
| 54 | + name: go-tutorial-buildkit |
| 55 | +spec: |
| 56 | + source: |
| 57 | + url: https://github.com/shipwright-io/sample-go |
| 58 | + contextDir: docker-build/ |
| 59 | + dockerfile: docker-build/ |
| 60 | + strategy: |
| 61 | + name: buildkit |
| 62 | + kind: ClusterBuildStrategy |
| 63 | + output: |
| 64 | + image: docker.io/${REGISTRY_ORG}/go-tutorial:latest |
| 65 | + credentials: |
| 66 | + name: tutorial-secret |
| 67 | +EOF |
| 68 | +``` |
| 69 | + |
| 70 | +_Note_: Pay attention to the definition under `spec.source.dockerfile`. The `buildkit` expects a path to where your `Dockerfile` is located. |
| 71 | + |
| 72 | +Verify that the `go-tutorial` Build was created successfully: |
| 73 | + |
| 74 | +```sh |
| 75 | +$ kubectl get build |
| 76 | +NAME REGISTERED REASON BUILDSTRATEGYKIND BUILDSTRATEGYNAME CREATIONTIME |
| 77 | +go-tutorial-buildkit True Succeeded ClusterBuildStrategy buildkit 4s |
| 78 | +``` |
| 79 | + |
| 80 | +_Note_: For more information about Build resources, please refer to the related [docs](/docs/build.md). |
| 81 | + |
| 82 | +## Creating a BuildRun |
| 83 | + |
| 84 | +Second, we will create a `BuildRun` resource that references our previous `go-tutorial` Build: |
| 85 | + |
| 86 | +```sh |
| 87 | +$ cat <<EOF | kubectl create -f - |
| 88 | +apiVersion: shipwright.io/v1alpha1 |
| 89 | +kind: BuildRun |
| 90 | +metadata: |
| 91 | + name: a-buildkit-buildrun |
| 92 | +spec: |
| 93 | + buildRef: |
| 94 | + name: go-tutorial-buildkit |
| 95 | +EOF |
| 96 | +``` |
| 97 | + |
| 98 | +Wait until your `go-tutorial-buildrun` buildrun is completed: |
| 99 | + |
| 100 | +```sh |
| 101 | +$ kubectl get buildrun |
| 102 | +NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME |
| 103 | +a-buildkit-buildrun Unknown Pending 4s |
| 104 | +``` |
| 105 | + |
| 106 | +To know more about the state of the BuildRun, the `.Status.Conditions` fields provide more data: |
| 107 | + |
| 108 | +```sh |
| 109 | +$ kubectl get buildrun a-buildkit-buildrun -o json | jq '.status.conditions[]' |
| 110 | +{ |
| 111 | + "lastTransitionTime": "2021-04-01T09:15:13Z", |
| 112 | + "message": "All Steps have completed executing", |
| 113 | + "reason": "Succeeded", |
| 114 | + "status": "True", |
| 115 | + "type": "Succeeded" |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +_Note_: A BuildRun is a resource that runs to completion. The `REASON` column reflects the state of the resource. If the BuildRun ran to completion successfully, |
| 120 | +a `Succeeded` `REASON` is expected. |
| 121 | + |
| 122 | +_Note_: For more information about BuildRun resources, please refer to the related [docs](/docs/buildrun.md). |
| 123 | + |
| 124 | +## Closing |
| 125 | + |
| 126 | +Congratulations! You just created a container image from https://github.com/shipwright-io/sample-go using [Buildkit](https://github.com/moby/buildkit). |
| 127 | + |
| 128 | +The new container image should now be available in your container registry. |
0 commit comments