Unverified Commit f353e0cc authored by GridexX's avatar GridexX
Browse files

feat: improve docker_build job

parent 803ecc72
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
# Changelog
All notable changes to this job will be documented in this file.

## [2.0.0] - 2022-07-29
* 🚨 **BREAKING CHANGE**
  Variable `DOCKER_TAG` updated to `DOCKER_TAGS` to use multiple tags in image building. 
* Add new variable `DOCKER_SNAPSHOT_MODE` to modify the filesystem's snapshot
* Update kaniko image to `v1.8.1`

## [1.2.0] - 2022-06-13
* Add docker image tag in variable 

+31 −24
Original line number Diff line number Diff line
@@ -13,12 +13,18 @@ from a Dockerfile at the root of your project, and push it to a remote registry
1. Create a
   [Dockerfile](https://docs.docker.com/get-started/part2/#sample-dockerfile){:target="_blank"} (by default at the root of your project)
   to containerize your application
1. Copy/paste job URL in `include` list of your `.gitlab-ci.yml` (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
1. Copy the job URL located in the `Install` part of the right panel and add it inside the `include` list of your `.gitlab-ci.yml` file (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
4. If you need to customize the job (stage, variables, ...) 👉 check the [jobs
   customization](/use-the-hub/#jobs-customization)
5. Well done, your job is ready to work ! 😀

## Build behavior
## Job details

* Job name: `docker_build`
* Docker image: [`gcr.io/kaniko-project/executor:v1.8.1-debug`](https://github.com/GoogleContainerTools/kaniko){:target="_blank"}
* Default stage: `build`

### Build behavior

!!! info
    This build will use Gitlab CI predefined [environment variables](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html){:target="_blank"}
@@ -27,27 +33,28 @@ from a Dockerfile at the root of your project, and push it to a remote registry

The registry and tag of the resulting Docker image follow this behavior:

| `CUSTOM_TAG` used ? | Are you pushing git tag ? | Registry where image is pushed | Docker tag applied to the image |
| `CUSTOM_TAGS` used ? | Are you pushing git tag ? | Registry where image is pushed | Docker tag applied to the image |
|:--------------------|:--------------------------|:-------------------------------|:--------------------------------|
| No                  | No                        | Gitlab project registry        | Last commit SHA                 |
| No                  | Yes                       | Gitlab project registry        | Git tag name                    |
| Yes                 | No                        | Gitlab project registry        | `CUSTOM_TAG`                    |
| Yes                 | Yes                       | Gitlab project registry        | `CUSTOM_TAG`                    |
| Yes                 | No                        | Gitlab project registry        | `CUSTOM_TAGS`                    |
| Yes                 | Yes                       | Gitlab project registry        | `CUSTOM_TAGS`                    |

!!! info
    In order to use custom registries, you need to provide the file `config.json` that contains the auths, you can do that by passing it as a [CI/CD file](https://docs.gitlab.com/ee/ci/variables/#cicd-variable-types){:target="_blank"} named `CONFIG_FILE` (see example below)

## Variables
### Variables

| VARIABLE NAME                    | DESCRIPTION                                                                                                                                                                          | DEFAULT VALUE |
|:---------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------------|
| `CUSTOM_TAG`                     | If you want a specific tag for your image                                                                                                                                            | ` `           |
| Name | Description | Default |
| ---- | ----------- | ------- |
| `CUSTOM_TAGS` <img width=100/> | Add one or mor specific tag for your image. each tag are separated with space. <img width=175/>| ` ` <img width=100/>|
| `COMMIT_CREATE_LATEST` | In a commit context, also update `latest` tag | `false` |
| `TAG_CREATE_LATEST` | In a tag context, also update `latest` tag | `true` |
| `DOCKER_CONTEXT_PATH` | Path of build context from to repository root | ` ` |
| `DOCKERFILE_PATH` | Path to Dockerfile from the build context (see `DOCKER_CONTEXT_PATH`) | `Dockerfile` |
| `DOCKER_USE_CACHE` | Cache Dockerfile layers. Cached layers are stored in the [container registry](https://docs.gitlab.com/ee/user/packages/container_registry/){:target="_blank"} in `/cache` repository| `false` |
| `DOCKER_CACHE_TTL` | Cached layers TTL | `336h` |
| `DOCKER_SNAPSHOT_MODE` |  Flag to set how kaniko will snapshot the filesystem. With `redo`, may be 50% faster than default, as described in the [doc](https://github.com/GoogleContainerTools/kaniko#--snapshotmode).  | `redo` |
| `KANIKO_USE_NEWRUN` | Enable Kaniko option [`--use-new-run`](https://github.com/GoogleContainerTools/kaniko#--use-new-run) | `true` |
| `DOCKER_VERBOSITY`| Set the verbosity of the build in job's log (see [levels](https://github.com/GoogleContainerTools/kaniko#--verbosity){:target="_blank"}) | `info` |
| `DOCKER_OPTIONS` | If you want to use additional [options](https://github.com/GoogleContainerTools/kaniko#additional-flags){:target="_blank"} | ` ` |
@@ -74,5 +81,5 @@ The registry and tag of the resulting Docker image follow this behavior:



## Author
### Author
This resource is an **[official job](https://docs.r2devops.io/faq-labels/)** added in [**R2Devops repository**](https://gitlab.com/r2devops/hub) by [@thomasboni](https://gitlab.com/thomasboni)
 No newline at end of file
+16 −9
Original line number Diff line number Diff line
@@ -13,19 +13,19 @@ docker_build:
    CONFIG_FILE: ""
    DOCKER_CONTEXT_PATH: ""
    
    CUSTOM_TAG: ""
    CUSTOM_TAGS: ""
    COMMIT_CREATE_LATEST: "false"
    TAG_CREATE_LATEST: "true"
    DOCKERFILE_PATH: "Dockerfile"

    DOCKER_USE_CACHE: "false"
    DOCKER_CACHE_TTL: "336h"

    DOCKER_SNAPSHOT_MODE: "redo"
    KANIKO_USE_NEWRUN: "true"

    REGISTRY_PUSH: "true"
    DOCKER_VERBOSITY: "info"
    DOCKER_OPTIONS: ""
    IMAGE_TAG: "v1.5.1-debug"
    IMAGE_TAG: "v1.8.1-debug"
  script:
    - mkdir -p /kaniko/.docker/

@@ -51,16 +51,23 @@ docker_build:

    - DOCKER_CONTEXT_PATH=${CI_PROJECT_DIR}/${DOCKER_CONTEXT_PATH}

    - DOCKER_OPTIONS="--verbosity=${DOCKER_VERBOSITY} ${DOCKER_OPTIONS}"
    - DOCKER_OPTIONS="--verbosity=${DOCKER_VERBOSITY} --snapshotMode=${DOCKER_SNAPSHOT_MODE} ${DOCKER_OPTIONS}"
    
    - if [ ${REGISTRY_PUSH} == "false" ]; then
    -   DOCKER_OPTIONS="${DOCKER_OPTIONS} --no-push"
    - fi
    - if [ ! -z "$CUSTOM_REGISTRIES_DESTINATIONS" ]; then
    -   cp -rf $CONFIG_FILE /kaniko/.docker/config.json
    -   DOCKER_OPTIONS="${DOCKER_OPTIONS} $CUSTOM_REGISTRIES_DESTINATIONS"
    -   /kaniko/executor --context ${DOCKER_CONTEXT_PATH} --dockerfile ${DOCKER_CONTEXT_PATH}/${DOCKERFILE_PATH} ${DOCKER_OPTIONS}
    - else
    -   echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    -     if [ ! -z ${CUSTOM_TAG} ]; then
    -       /kaniko/executor --context ${DOCKER_CONTEXT_PATH} --dockerfile ${DOCKER_CONTEXT_PATH}/${DOCKERFILE_PATH} --destination ${CI_REGISTRY_IMAGE}:${CUSTOM_TAG} ${DOCKER_OPTIONS}
    -     if [ ! -z "${CUSTOM_TAGS}" ]; then
    -       DESTINATIONS=""
    -       for TAG in $(echo "${CUSTOM_TAGS}"); do
    -         DESTINATIONS="--destination ${CI_REGISTRY_IMAGE}:${TAG} ${DESTINATIONS}"
    -       done
    -       /kaniko/executor --context ${DOCKER_CONTEXT_PATH} --dockerfile ${DOCKER_CONTEXT_PATH}/${DOCKERFILE_PATH} ${DESTINATIONS} ${DOCKER_OPTIONS}
    -     else
    -       /kaniko/executor --context ${DOCKER_CONTEXT_PATH} --dockerfile ${DOCKER_CONTEXT_PATH}/${DOCKERFILE_PATH} --destination ${CI_REGISTRY_IMAGE}:${IMAGE_TAG} ${OPTIONAL_TAG} ${DOCKER_OPTIONS}
    -     fi