'How to set DOCKER_BUILDKIT=1 from .gitlab-ci.yml?

I have a working Dockerfile that requires execution with BuildKit support.

Question: how can I build the dockerfile from gitlab-ci and set DOCKER_BUILDKIT=1 globally?

.gitlab-ci.yml:

    image: docker:20
    
    variables:
      DOCKER_DRIVER: overlay2
    
    services:
      - docker:dind

    build:  
      script:
        - docker build . 

Dockerfile:

    FROM maven:3.8.4-eclipse-temurin-11 as dependencies
    COPY pom.xml .
    COPY src src
    RUN --mount=type=cache,target=/root/.m2 mvn package


Solution 1:[1]

Gitlab runs inside a linux (native or container), so I suggest you to change .gitlab-ci.yml as follow:

image: docker:20

variables:
  DOCKER_DRIVER: overlay2

services:
  - docker:dind

build:  
  script:
    - DOCKER_BUILDKIT=1 docker build . 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Antonio Petricca