docker / buildx

Docker CLI plugin for extended build capabilities with BuildKit
Apache License 2.0
3.33k stars 448 forks source link

bake: add call methods support and printing #2556

Closed tonistiigi closed 1 day ago

tonistiigi commented 1 week ago

Define call method in the bake target

target "validate" {
    call = "check"
}
docker buildx bake validate

Set call/check on all active targets

docker buildx bake --call=outline
docker buildx bake --check
docker buildx bake --call check,format=json

The text output of call=outline should be updated in follow-up to include bake definition and variables. Getting variables seems tricky and I think needs HCL parser updates.

» docker buildx bake binaries test --call=outline 
[+] Building 1.0s (11/11) FINISHED                                                                                                                                           0.0s
binaries

TARGET: binaries

BUILD ARG                  VALUE   DESCRIPTION
GO_VERSION                 1.21
XX_VERSION                 1.4.0
GO_EXTRA_FLAGS
BUILDKIT_SBOM_SCAN_STAGE   true

test

TARGET: test-coverage

BUILD ARG    VALUE   DESCRIPTION
GO_VERSION   1.21
XX_VERSION   1.4.0

JSON output contains both Bake definition and the method result.

List all bake targets

docker buildx bake --list-targets
TARGET              DESCRIPTION
binaries
binaries-cross
default             binaries
image
image-cross
image-local
integration-test
integration-test-base
lint
lint-gopls          Run gopls analyzers
meta-helper

List all bake variables

docker buildx bake --list-variables
VARIABLE            VALUE   DESCRIPTION
DESTDIR             ./bin   Destination directory for build artifacts
DOCS_FORMATS            md
GOLANGCI_LINT_MULTIPLATFORM
GO_VERSION          <null>
HTTPS_PROXY
HTTP_PROXY
NO_PROXY
TEST_BUILDKIT_TAG       <null>
TEST_COVERAGE           <null>
dvdksn commented 4 days ago

Should call be a block type in the bake file when defining subfields? And should it be possible to set control directives here?

# Short form
target "default" {
  call = "check"
}

# Long form
target "default" {
  call = {
    method = "check"
    format = "json"
    error = true # ignorestatus / BUILDKIT_DOCKERFILE_CHECK=error=true
    skip = [ "NoEmptyContinuation", "StageNameCasing" ] # BUILDKIT_DOCKERFILE_CHECK=skip=StageNameCasing,NoEmptyContinuations
  }
}
crazy-max commented 3 days ago

Should call be a block type in the bake file when defining subfields?

Yes in the future we would like objects instead of csv values in HCL. This is tracked in https://github.com/docker/buildx/issues/438

tonistiigi commented 3 days ago

Should call be a block type in the bake file when defining subfields? And should it be possible to set control directives here?

format=json is for the whole output atm, so it can be set with --call but in bake definition it is just a method name. Should there be multiple targets with different format, there is no way to print it as if user requests JSON we shouldn't print random text in the middle of the output.

ignorestatus and #check=error=true are not quite the same field as well. I'm not sure if ignorestatus is even worth documenting atm.

crazy-max commented 1 day ago

:eyes: https://github.com/docker/buildx/actions/runs/9781238278/job/27004845569?pr=2556#step:7:468

=== FAIL: tests TestIntegration/TestBakeCallCheckFlag/worker=docker-container (4.20s)
    bake.go:1086: 
            Error Trace:    /src/tests/bake.go:1086
                                        /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:96
                                        /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:211
            Error:          Received unexpected error:
                            invalid character '/' after top-level value
            Test:           TestIntegration/TestBakeCallCheckFlag/worker=docker-container
    --- FAIL: TestIntegration/TestBakeCallCheckFlag/worker=docker-container (4.20s)

Looks flaky

crazy-max commented 1 day ago

Will open a follow-up when #2562 is merged to add cross linking from bake to build

tonistiigi commented 1 day ago

@crazy-max Something is printing to stdout on format=json I think