|
| 1 | +--- |
| 2 | +title:"Google Cloud Builder" |
| 3 | +description:"Using the Google Cloud builder to create Docker images" |
| 4 | +group:integrations |
| 5 | + |
| 6 | +toc:true |
| 7 | +--- |
| 8 | + |
| 9 | +Google Cloud builder is an online service that allows you to build Docker images using the Google infrastructure and also push them to the Google Cloud registry. |
| 10 | + |
| 11 | +You can also use Cloud builder in a Codefresh pipeline in place of the[normal build step]({{site.baseurl}}/docs/codefresh-yaml/steps/build/). This way you can take advantage of the Cloud builder in your Codefresh pipelines, but still push to other registries that are connected to Codefresh (and not just GCR). |
| 12 | + |
| 13 | + |
| 14 | +##Prerequisites |
| 15 | + |
| 16 | +In order to use the Cloud builder service in your Codefresh pipeline you need |
| 17 | + |
| 18 | +1. A free Dockerhub account and[Dockerhub connected to Codefresh]({{site.baseurl}}/docs/docker-registries/external-docker-registries/docker-hub/). |
| 19 | +1. A Google Cloud subscription and a[service account for the Cloud builder service](https://cloud.google.com/cloud-build/docs/securing-builds/set-service-account-permissions). |
| 20 | + |
| 21 | +Save your service account as a JSON file and make sure you select at least the[following roles](https://cloud.google.com/container-registry/docs/access-control): |
| 22 | + |
| 23 | +* Cloud storage Admin |
| 24 | +* Storage Admin |
| 25 | +* Storage Object Viewer |
| 26 | +* Storage Object Creator |
| 27 | + |
| 28 | +You will use this JSON file either in the usual way of integration a[Google Docker registry]({{site.baseurl}}/docs/docker-registries/external-docker-registries/google-container-registry/) in Codefresh or directly in a pipeline as we will see later. |
| 29 | + |
| 30 | +##How it works |
| 31 | + |
| 32 | +The Google Cloud builder integration/authentication can be used in the following ways: |
| 33 | + |
| 34 | +1. Authentication will be retrieved from the GCR integration in your Codefresh account, and the resulting Docker image will also be pushed to GCR |
| 35 | +1. Authentication will be retrieved from the GCR integration in your Codefresh account but the resulting Docker image will be pushed to any other[external registry connected to Codefresh]({{site.baseurl}}/docs/docker-registries/external-docker-registries/) |
| 36 | +1. Authentication will be defined in the pipeline itself, and the resulting image can be pushed to any registry connected to Codefresh |
| 37 | + |
| 38 | +In the first two cases, you will enter your service account file centrally in the GCR integration screen and then any pipeline can authenticate to Google Cloud builder without any further configuration. |
| 39 | + |
| 40 | +{% |
| 41 | +include image.html |
| 42 | +lightbox="true" |
| 43 | +file="/images/artifacts/registry/add-gcr-registry.png" |
| 44 | +url="/images/220c472-add-gcr-new.png" |
| 45 | +alt="Using the JSON service account in Codefresh" |
| 46 | +caption="Using the JSON service account in Codefresh" |
| 47 | +max-width="50%" |
| 48 | +%} |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +##Using Google Cloud builder in a Codefresh pipeline |
| 53 | + |
| 54 | +In the most straightforward scenario you want to create a Docker image with Google Cloud builder and also push to GCR. |
| 55 | + |
| 56 | +{% include image.html |
| 57 | +lightbox="true" |
| 58 | +file="/images/integrations/gcloud-builder/build-push-gcr.png" |
| 59 | +url="/images/integrations/gcloud-builder/build-push-gcr.png" |
| 60 | +max-width="90%" |
| 61 | +caption="Using Google cloud builder in Codefresh" |
| 62 | +alt="Using Google cloud builder in Codefresh" |
| 63 | +%} |
| 64 | + |
| 65 | +Here is the full pipeline: |
| 66 | + |
| 67 | +`codefresh.yml` |
| 68 | +{% highlight yaml %} |
| 69 | +{% raw %} |
| 70 | +version: '1.0' |
| 71 | +steps: |
| 72 | + main_clone: |
| 73 | + title: Cloning main repository... |
| 74 | + type: git-clone |
| 75 | + repo: 'codefresh-contrib/golang-sample-app' |
| 76 | + revision: master |
| 77 | + git: github |
| 78 | + MyAppDockerImage: |
| 79 | + title: Building Docker Image |
| 80 | + type: build |
| 81 | + image_name: my-golang-image |
| 82 | + working_directory: ./ |
| 83 | + tag: slim |
| 84 | + registry: gcr |
| 85 | + dockerfile: Dockerfile.multistage |
| 86 | + provider: |
| 87 | + type: gcb |
| 88 | + arguments: |
| 89 | + cache: |
| 90 | + repo: "my-golang-image-cache/kaniko-cache" |
| 91 | + ttl: "10h" |
| 92 | +{% endraw %} |
| 93 | +{% endhighlight %} |
| 94 | + |
| 95 | + |
| 96 | +In the build step of the pipeline there is an extra property`provider` that specifies we want to use Cloud builder instead of the Codefresh native build step. |
| 97 | + |
| 98 | +The only required argument is the repository that will be used for[Kaniko caching](https://cloud.google.com/cloud-build/docs/kaniko-cache) to speed up subsequent builds. |
| 99 | + |
| 100 | +>Note that the Kaniko repo should NOT be the same as the repository used for the image itself. |
| 101 | +
|
| 102 | +{% include image.html |
| 103 | +lightbox="true" |
| 104 | +file="/images/integrations/gcloud-builder/image-dashboard.png" |
| 105 | +url="/images/integrations/gcloud-builder/image-dashboard.png" |
| 106 | +max-width="70%" |
| 107 | +caption="Inspecting an image from Google Cloud build" |
| 108 | +alt="Inspecting an image from Google Cloud build" |
| 109 | +%} |
| 110 | + |
| 111 | +After you run the pipeline you will see your Docker image in the[Image dashboard]({{site.baseurl}}/docs/docker-registries/working-with-docker-registries/) |
| 112 | + |
| 113 | +The docker image will also be visible in the Google Cloud Console view of your registry. |
| 114 | + |
| 115 | +###Pushing to a different registry |
| 116 | + |
| 117 | +Even though the Cloud builder pipeline step authentication is fetched from the GCR configuration, you don't have to push to GCR. |
| 118 | +Simply change the`registry` property in the build step to push the Docker image to another connected registry: |
| 119 | + |
| 120 | +`codefresh.yml` |
| 121 | +{% highlight yaml %} |
| 122 | +{% raw %} |
| 123 | +version: '1.0' |
| 124 | +steps: |
| 125 | + main_clone: |
| 126 | + title: Cloning main repository... |
| 127 | + type: git-clone |
| 128 | + repo: 'codefresh-contrib/golang-sample-app' |
| 129 | + revision: master |
| 130 | + git: github |
| 131 | + MyAppDockerImage: |
| 132 | + title: Building Docker Image |
| 133 | + type: build |
| 134 | + image_name: my-golang-image |
| 135 | + working_directory: ./ |
| 136 | + tag: slim |
| 137 | + registry: azure |
| 138 | + dockerfile: Dockerfile.multistage |
| 139 | + provider: |
| 140 | + type: gcb |
| 141 | + arguments: |
| 142 | + cache: |
| 143 | + repo: "my-golang-image-cache/kaniko-cache" |
| 144 | + ttl: "10h" |
| 145 | +{% endraw %} |
| 146 | +{% endhighlight %} |
| 147 | + |
| 148 | +This pipeline will push the Docker image created to another registry that is identified by[azure]({{site.baseurl}}/docs/docker-registries/external-docker-registries/azure-docker-registry/). |
| 149 | + |
| 150 | +###Authenticating to Cloud Builder in the pipeline |
| 151 | + |
| 152 | +If you don't want to reuse the Registry integration provided by Codefresh for easy authentication to Google Cloud builder, you can also use your service account JSON file directly in the pipeline. |
| 153 | + |
| 154 | +You can pass the contents of the JSON file as a variable in the pipeline and the build step will use it to authenticate. |
| 155 | + |
| 156 | +`codefresh.yml` |
| 157 | +{% highlight yaml %} |
| 158 | +{% raw %} |
| 159 | +version: '1.0' |
| 160 | +steps: |
| 161 | + main_clone: |
| 162 | + title: Cloning main repository... |
| 163 | + type: git-clone |
| 164 | + repo: 'codefresh-contrib/golang-sample-app' |
| 165 | + revision: master |
| 166 | + git: github |
| 167 | + MyAppDockerImage: |
| 168 | + title: Building Docker Image |
| 169 | + type: build |
| 170 | + image_name: my-golang-image |
| 171 | + working_directory: ./ |
| 172 | + tag: slim |
| 173 | + registry: azure |
| 174 | + dockerfile: Dockerfile.multistage |
| 175 | + provider: |
| 176 | + type: gcb |
| 177 | + arguments: |
| 178 | + google_app_creds: '${{G_CREDS_B64}}' |
| 179 | + cache: |
| 180 | + repo: "my-golang-image-cache/kaniko-cache" |
| 181 | + ttl: "10h" |
| 182 | +{% endraw %} |
| 183 | +{% endhighlight %} |
| 184 | + |
| 185 | +Here the pipeline will try to authenticate to Google Cloud builder using the contents of the`google_app_creds` property. |
| 186 | + |
| 187 | +The value of this property can be a pipeline variable, or project variable or any other standard Codefresh method such as[shared configuration]({{site.baseurl}}/docs/configure-ci-cd-pipeline/shared-configuration/). |
| 188 | + |
| 189 | +You need to escape the contents of the service account before you use in the pipeline with either of these commands on your local workstation: |
| 190 | + |
| 191 | +*`cat _json_key_file | base64 | tr -d ‘\n’` |
| 192 | +*`cat _json_key_file | base64 -w 0` |
| 193 | + |
| 194 | +###Using extra properties for Google Cloud builder |
| 195 | + |
| 196 | +The build step has several other properties can be used to fine-tune the Google Cloud builder behavior. |
| 197 | + |
| 198 | +Here is the full syntax: |
| 199 | + |
| 200 | + |
| 201 | +{% highlight yaml %} |
| 202 | +{% raw %} |
| 203 | + |
| 204 | +step_name: |
| 205 | + type: build |
| 206 | + title: Step Title |
| 207 | + description: Free text description |
| 208 | + working_directory: ${{clone_step_name}} |
| 209 | + dockerfile: path/to/Dockerfile |
| 210 | + image_name: owner/new-image-name |
| 211 | + tag: develop |
| 212 | + build_arguments: |
| 213 | + - key=value |
| 214 | + target: stage1 |
| 215 | + no_cache: false |
| 216 | + no_cf_cache: false |
| 217 | + fail_fast: false |
| 218 | + registry: my-registry |
| 219 | + provider: |
| 220 | + type: gcb |
| 221 | + arguments: |
| 222 | + google_app_creds: '${{G_CREDS_B64}}' |
| 223 | + cache: |
| 224 | + repo: "repositoryname/kaniko-cache" |
| 225 | + ttl: "10h" |
| 226 | + timeout: "600s" |
| 227 | + machineType: 'N1_HIGHCPU_8' |
| 228 | + logsBucket: "gs://your-project_cloudbuild/logs" |
| 229 | + diskSizeGb: 10 |
| 230 | + |
| 231 | +{% endraw %} |
| 232 | +{% endhighlight %} |
| 233 | + |
| 234 | +The extra fields are: |
| 235 | + |
| 236 | +{: .table .table-bordered .table-hover} |
| 237 | +| Field| Description| Required/Optional/Default| |
| 238 | +| ------------------------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -------------------------| |
| 239 | +|`type`| defines, which provider to use (currently`gcb` and`cf` types are available). It uses`cf` provider by default and the whole provider section can be omitted for a regular build step.| Required| |
| 240 | +|`arguments`| Parameters for Google Cloud builder| Required| |
| 241 | +|`google_app_creds`| base64 encoded string of the[Google app credentials JSON](https://cloud.google.com/docs/authentication/production). By default It will be taken from the existing GCR integration.| Optional| |
| 242 | +|`cache`| The list of Kaniko cache parameters| Required| |
| 243 | +|`repo`| Docker repository path for the Kaniko cache| Required| |
| 244 | +|`ttl`| Kaniko cache retention. Default value is`336h`| Optional| |
| 245 | +|`timeout`| This field is directly translated into the corresponding field of the[GCB manifest file](https://cloud.google.com/cloud-build/docs/build-config#structure_of_a_build_config_file). Default is`10m`| Optional| |
| 246 | +|`machineType`| This field is directly translated into the corresponding field of the[GCB manifest file](https://cloud.google.com/cloud-build/docs/build-config#structure_of_a_build_config_file)| Optional| |
| 247 | +|`diskSizeGb`| This field is directly translated into the corresponding field of the[GCB manifest file](https://cloud.google.com/cloud-build/docs/build-config#structure_of_a_build_config_file)| Optional| |
| 248 | +|`logsBucket`| This field is directly translated into the corresponding field of the[GCB manifest file](https://cloud.google.com/cloud-build/docs/build-config#structure_of_a_build_config_file)| Optional| |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | +The step also accepts all the field of the[standard build step]({{site.baseurl}}/docs/codefresh-yaml/steps/build/) but notice that the following fields are not supported in the current implementation and simply ignored by the GCB step logic: |
| 254 | + |
| 255 | +*`no_cache` |
| 256 | +* All the[buildkit]({{site.baseurl}}/docs/codefresh-yaml/steps/build/#buildkit-support) related fields |
| 257 | + |
| 258 | +Here is an example that uses all possible fields: |
| 259 | + |
| 260 | +`YAML` |
| 261 | +{% highlight yaml %} |
| 262 | +{% raw %} |
| 263 | +GCBuild: |
| 264 | + type: build |
| 265 | + image_name: '${{IMAGE_NAME}}' |
| 266 | + working_directory: ${{CloneStep}} |
| 267 | + tag: your-tag1 |
| 268 | + tags: |
| 269 | + - your-tag2 |
| 270 | + - your-tag3 |
| 271 | + target: 'test' |
| 272 | + no_cf_cache: false |
| 273 | + metadata: |
| 274 | + set: |
| 275 | + - qa: pending |
| 276 | + build_arguments: |
| 277 | + - WORD=Hello |
| 278 | + registry: 'reg-integration-name' |
| 279 | + dockerfile: |
| 280 | + content: |- |
| 281 | + FROM alpine as test |
| 282 | + RUN apk add skopeo |
| 283 | + ARG WORD |
| 284 | + RUN echo $WORD |
| 285 | + provider: |
| 286 | + type: gcb |
| 287 | + arguments: |
| 288 | + google_app_creds: '${{G_CREDS_B64}}' |
| 289 | + cache: |
| 290 | + repo: "repositoryname/kaniko-cache" |
| 291 | + ttl: "10h" |
| 292 | + timeout: "600s" |
| 293 | + machineType: 'N1_HIGHCPU_8' |
| 294 | + logsBucket: "gs://your-project_cloudbuild/logs" |
| 295 | + diskSizeGb: 10 |
| 296 | +{% endraw %} |
| 297 | +{% endhighlight %} |
| 298 | + |
| 299 | + |
| 300 | + |
| 301 | + |
| 302 | + |
| 303 | + |
| 304 | +##What to read next |
| 305 | + |
| 306 | +-[Creating pipelines]({{site.baseurl}}/docs/configure-ci-cd-pipeline/pipelines/) |
| 307 | +-[Pipeline steps]({{site.baseurl}}/docs/codefresh-yaml/steps/) |
| 308 | +-[Google Registry integration]({{site.baseurl}}/docs/docker-registries/external-docker-registries/google-container-registry/) |
| 309 | +-[Push step]({{site.baseurl}}/docs/codefresh-yaml/steps/push/) |
| 310 | +-[Build and push an image]({{site.baseurl}}/docs/yaml-examples/examples/build-and-push-an-image/) |
| 311 | + |
| 312 | + |
| 313 | + |