Skip to content

bake-hcl: add mechanism to reference the current target name #1203

@tnaroska

Description

@tnaroska

I have a bake hcl file defining multiple targets which share some common configuration (e.g. build-args, target platform, secrets, ...). To minimize duplication I have moved these into an abstract _base target and use the inherits attribute to import the shared values into each leaf traget.

This works great in general but fails for attributes like cache-from, tags which require their unique values for each lead target as shown in the example below.

target "_base" {
    args = {
        CORE_BRANCH  = CORE_BRANCH
        CORE_VERSION = CORE_VERSION
        BUILDKIT_SYNTAX = docker/dockerfile:1.4
    }
    cache-to  = ["type=inline"]
    secret    = ["id=artifactory_user,env=ARTIFACTORY_USER", "id=artifactory_pass,env=ARTIFACTORY_API_TOKEN"]
    platforms = ...
}

target "image1" {
    dockerfile = "image1/Dockerfile"
    cache-from = "myrepo/image1"
    tags = "myrepo/image1"
    inherits = ["_base"]
}

target "image2" {
    dockerfile = "image2/Dockerfile"
    cache-from = "myrepo/image2"
    tags = "myrepo/image2"
    inherits = ["_base"]
}
... more targets ...

The ask is to have a special hcl variable or function to reference the current target's name (think of $@ in a makefile). The same file could then be rewritten:

target "_base" {
    args = {
        CORE_BRANCH  = CORE_BRANCH
        CORE_VERSION = CORE_VERSION
        BUILDKIT_SYNTAX = docker/dockerfile:1.4
    }
    cache-to  = ["type=inline"]
    secret    = ["id=artifactory_user,env=ARTIFACTORY_USER", "id=artifactory_pass,env=ARTIFACTORY_API_TOKEN"]
    platforms = ...

    # using ${target} to reference target name. actual syntax TBD
    cache-from = "myrepo/${target}"
    tags = "myrepo/${target}"
}


target "image1" {
    dockerfile = "image1/Dockerfile"
    inherits = ["_base"]
}

target "image2" {
    dockerfile = "image2/Dockerfile"
    inherits = ["_base"]
}

Environment:

buildx   v0.8.2
buildkit v0.10.3

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions