mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-18 09:17:49 +08:00
Merge pull request #2263 from crazy-max/resp-build-ref
build: set build ref in response
This commit is contained in:
commit
ae0a5e495a
@ -786,6 +786,7 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
|
|||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
buildRef := fmt.Sprintf("%s/%s/%s", node.Builder, node.Name, so.Ref)
|
||||||
var rr *client.SolveResponse
|
var rr *client.SolveResponse
|
||||||
if resultHandleFunc != nil {
|
if resultHandleFunc != nil {
|
||||||
var resultHandle *ResultHandle
|
var resultHandle *ResultHandle
|
||||||
@ -795,7 +796,6 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
|
|||||||
rr, err = c.Build(ctx, *so, "buildx", buildFunc, ch)
|
rr, err = c.Build(ctx, *so, "buildx", buildFunc, ch)
|
||||||
}
|
}
|
||||||
if desktop.BuildBackendEnabled() && node.Driver.HistoryAPISupported(ctx) {
|
if desktop.BuildBackendEnabled() && node.Driver.HistoryAPISupported(ctx) {
|
||||||
buildRef := fmt.Sprintf("%s/%s/%s", node.Builder, node.Name, so.Ref)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &desktop.ErrorWithBuildRef{
|
return &desktop.ErrorWithBuildRef{
|
||||||
Ref: buildRef,
|
Ref: buildRef,
|
||||||
@ -815,6 +815,7 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
|
|||||||
for k, v := range printRes {
|
for k, v := range printRes {
|
||||||
rr.ExporterResponse[k] = string(v)
|
rr.ExporterResponse[k] = string(v)
|
||||||
}
|
}
|
||||||
|
rr.ExporterResponse["buildx.build.ref"] = buildRef
|
||||||
|
|
||||||
node := dp.Node().Driver
|
node := dp.Node().Driver
|
||||||
if node.IsMobyDriver() {
|
if node.IsMobyDriver() {
|
||||||
|
@ -327,6 +327,7 @@ $ cat metadata.json
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
"buildx.build.ref": "mybuilder/mybuilder0/0fjb6ubs52xx3vygf6fgdl611",
|
||||||
"containerimage.config.digest": "sha256:2937f66a9722f7f4a2df583de2f8cb97fc9196059a410e7f00072fc918930e66",
|
"containerimage.config.digest": "sha256:2937f66a9722f7f4a2df583de2f8cb97fc9196059a410e7f00072fc918930e66",
|
||||||
"containerimage.descriptor": {
|
"containerimage.descriptor": {
|
||||||
"annotations": {
|
"annotations": {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@ -34,6 +35,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
|
|||||||
testBakeEmpty,
|
testBakeEmpty,
|
||||||
testBakeShmSize,
|
testBakeShmSize,
|
||||||
testBakeUlimits,
|
testBakeUlimits,
|
||||||
|
testBakeRefs,
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBakeLocal(t *testing.T, sb integration.Sandbox) {
|
func testBakeLocal(t *testing.T, sb integration.Sandbox) {
|
||||||
@ -586,3 +588,46 @@ target "default" {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Contains(t, string(dt), `1024`)
|
require.Contains(t, string(dt), `1024`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testBakeRefs(t *testing.T, sb integration.Sandbox) {
|
||||||
|
dockerfile := []byte(`
|
||||||
|
FROM scratch
|
||||||
|
COPY foo /foo
|
||||||
|
`)
|
||||||
|
bakefile := []byte(`
|
||||||
|
target "default" {
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
dir := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
|
||||||
|
fstest.CreateFile("Dockerfile", dockerfile, 0600),
|
||||||
|
fstest.CreateFile("foo", []byte("foo"), 0600),
|
||||||
|
)
|
||||||
|
|
||||||
|
dirDest := t.TempDir()
|
||||||
|
|
||||||
|
outFlag := "default.output=type=docker"
|
||||||
|
if sb.DockerAddress() == "" {
|
||||||
|
// there is no Docker atm to load the image
|
||||||
|
outFlag += ",dest=" + dirDest + "/image.tar"
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--metadata-file", filepath.Join(dirDest, "md.json"), "--set", outFlag))
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err, out)
|
||||||
|
|
||||||
|
dt, err := os.ReadFile(filepath.Join(dirDest, "md.json"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
type mdT struct {
|
||||||
|
Default struct {
|
||||||
|
BuildRef string `json:"buildx.build.ref"`
|
||||||
|
} `json:"default"`
|
||||||
|
}
|
||||||
|
var md mdT
|
||||||
|
err = json.Unmarshal(dt, &md)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NotEmpty(t, md.Default.BuildRef)
|
||||||
|
}
|
||||||
|
@ -53,6 +53,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
|
|||||||
testBuildNetworkModeBridge,
|
testBuildNetworkModeBridge,
|
||||||
testBuildShmSize,
|
testBuildShmSize,
|
||||||
testBuildUlimit,
|
testBuildUlimit,
|
||||||
|
testBuildRef,
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBuild(t *testing.T, sb integration.Sandbox) {
|
func testBuild(t *testing.T, sb integration.Sandbox) {
|
||||||
@ -284,23 +285,6 @@ RUN exit 1`)
|
|||||||
require.True(t, buildDetailsPattern.MatchString(string(out)), fmt.Sprintf("expected build details link in output, got %q", out))
|
require.True(t, buildDetailsPattern.MatchString(string(out)), fmt.Sprintf("expected build details link in output, got %q", out))
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestProject(t *testing.T) string {
|
|
||||||
dockerfile := []byte(`
|
|
||||||
FROM busybox:latest AS base
|
|
||||||
COPY foo /etc/foo
|
|
||||||
RUN cp /etc/foo /etc/bar
|
|
||||||
|
|
||||||
FROM scratch
|
|
||||||
COPY --from=base /etc/bar /bar
|
|
||||||
`)
|
|
||||||
dir := tmpdir(
|
|
||||||
t,
|
|
||||||
fstest.CreateFile("Dockerfile", dockerfile, 0600),
|
|
||||||
fstest.CreateFile("foo", []byte("foo"), 0600),
|
|
||||||
)
|
|
||||||
return dir
|
|
||||||
}
|
|
||||||
|
|
||||||
func testBuildProgress(t *testing.T, sb integration.Sandbox) {
|
func testBuildProgress(t *testing.T, sb integration.Sandbox) {
|
||||||
dir := createTestProject(t)
|
dir := createTestProject(t)
|
||||||
driver, _, _ := strings.Cut(sb.Name(), "+")
|
driver, _, _ := strings.Cut(sb.Name(), "+")
|
||||||
@ -530,3 +514,47 @@ COPY --from=build /ulimit /
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Contains(t, string(dt), `1024`)
|
require.Contains(t, string(dt), `1024`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testBuildRef(t *testing.T, sb integration.Sandbox) {
|
||||||
|
dir := createTestProject(t)
|
||||||
|
dirDest := t.TempDir()
|
||||||
|
|
||||||
|
outFlag := "--output=type=docker"
|
||||||
|
if sb.DockerAddress() == "" {
|
||||||
|
// there is no Docker atm to load the image
|
||||||
|
outFlag += ",dest=" + dirDest + "/image.tar"
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := buildxCmd(sb, withArgs("build", outFlag, "--metadata-file", filepath.Join(dirDest, "md.json"), dir))
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err, string(out))
|
||||||
|
|
||||||
|
dt, err := os.ReadFile(filepath.Join(dirDest, "md.json"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
type mdT struct {
|
||||||
|
BuildRef string `json:"buildx.build.ref"`
|
||||||
|
}
|
||||||
|
var md mdT
|
||||||
|
err = json.Unmarshal(dt, &md)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NotEmpty(t, md.BuildRef)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTestProject(t *testing.T) string {
|
||||||
|
dockerfile := []byte(`
|
||||||
|
FROM busybox:latest AS base
|
||||||
|
COPY foo /etc/foo
|
||||||
|
RUN cp /etc/foo /etc/bar
|
||||||
|
|
||||||
|
FROM scratch
|
||||||
|
COPY --from=base /etc/bar /bar
|
||||||
|
`)
|
||||||
|
dir := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("Dockerfile", dockerfile, 0600),
|
||||||
|
fstest.CreateFile("foo", []byte("foo"), 0600),
|
||||||
|
)
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user