docker / docker/buildx

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

Open
#1,203 28 comments 31 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/bake help wanted kind/enhancement
Dominant language
Go
Stars
4.5k
Forks
682
Avg merge
2d 14h
Merged PRs (30d)
29

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue does not name an implementation file, test, or entry point; start by locating bake HCL target inheritance and attribute evaluation in the buildx repository. Define and document a supported way for inherited attributes such as cache-from and tags to reference the current target name, then add coverage for the _base, image1, and image2 example.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, go
Domain
build-system, cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.