Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Introduce --iidfile option
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Oct 22, 2019
1 parent 0c6905a commit 47816e4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
17 changes: 16 additions & 1 deletion e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package e2e
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"strings"
"testing"

"github.com/docker/app/internal/store"

"github.com/deislabs/cnab-go/bundle"
"gotest.tools/assert"
"gotest.tools/icmd"
Expand All @@ -15,9 +18,12 @@ import (
func TestBuild(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
cmd := info.configuredCmd
tmp := os.TempDir()
defer os.RemoveAll(tmp)

testDir := path.Join("testdata", "build")
cmd.Command = dockerCli.Command("app", "build", "--tag", "single:1.0.0", "-f", path.Join(testDir, "single.dockerapp"), testDir)
iidfile := path.Join(tmp, "iid")
cmd.Command = dockerCli.Command("app", "build", "--tag", "single:1.0.0", "--iidfile", iidfile, "-f", path.Join(testDir, "single.dockerapp"), testDir)
icmd.RunCmd(cmd).Assert(t, icmd.Success)

cfg := getDockerConfigDir(t, cmd)
Expand All @@ -34,6 +40,15 @@ func TestBuild(t *testing.T) {
cmd.Command = dockerCli.Command("inspect", ref)
icmd.RunCmd(cmd).Assert(t, icmd.Success)
}

_, err = os.Stat(iidfile)
assert.NilError(t, err)
bytes, err := ioutil.ReadFile(iidfile)
assert.NilError(t, err)
iid := string(bytes)
actualID, err := store.FromBundle(&bndl)
assert.NilError(t, err)
assert.Equal(t, iid, actualID)
})
}

Expand Down
50 changes: 37 additions & 13 deletions internal/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strconv"
"strings"

"github.com/docker/app/internal/store"

"github.com/deislabs/cnab-go/bundle"
cnab "github.com/deislabs/cnab-go/driver"
"github.com/docker/app/internal/packager"
Expand All @@ -32,12 +34,13 @@ import (
)

type buildOptions struct {
noCache bool
progress string
pull bool
tag string
folder string
args []string
noCache bool
progress string
pull bool
tag string
folder string
imageIDFile string
args []string
}

func Cmd(dockerCli command.Cli) *cobra.Command {
Expand All @@ -63,6 +66,7 @@ func Cmd(dockerCli command.Cli) *cobra.Command {
flags.StringVarP(&opts.folder, "folder", "f", "", "Docker app folder containing application definition")
flags.BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image")
flags.StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables")
flags.StringVar(&opts.imageIDFile, "iidfile", "", "Write the app image ID to the file")

return cmd
}
Expand All @@ -73,6 +77,13 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) (refe
return nil, err
}

if opt.imageIDFile != "" {
// Avoid leaving a stale file if we eventually fail
if err := os.Remove(opt.imageIDFile); err != nil && !os.IsNotExist(err) {
return nil, err
}
}

if err = checkBuildArgsUniqueness(opt.args); err != nil {
return nil, err
}
Expand All @@ -94,18 +105,34 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) (refe
}
defer app.Cleanup()

buildopts, err := parseCompose(app, contextPath, opt)
bundle, err := buildImageUsingBuildx(app, contextPath, opt, dockerCli)
if err != nil {
return nil, err
}

if opt.imageIDFile != "" {
id, err := store.FromBundle(bundle)
if err != nil {
return nil, err
}
if err = ioutil.WriteFile(opt.imageIDFile, []byte(id.String()), 0644); err != nil {
return nil, err
}
}

return packager.PersistInBundleStore(ref, bundle)
}

func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions, dockerCli command.Cli) (*bundle.Bundle, error) {
buildopts, err := parseCompose(app, contextPath, opt)
if err != nil {
return nil, err
}
buildopts["com.docker.app.invocation-image"], err = createInvocationImageBuildOptions(dockerCli, app)
if err != nil {
return nil, err
}

debugBuildOpts(buildopts)

ctx, cancel := context.WithCancel(appcontext.Context())
defer cancel()
const drivername = "buildx_buildkit_default"
Expand All @@ -119,9 +146,7 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) (refe
Driver: d,
},
}

pw := progress.NewPrinter(ctx, os.Stderr, opt.progress)

// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
resp, err := build.Build(ctx, driverInfo, buildopts, nil, dockerCli.ConfigFile(), pw)
if err != nil {
Expand All @@ -137,8 +162,7 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) (refe
if err != nil {
return nil, err
}

return packager.PersistInBundleStore(ref, bundle)
return bundle, nil
}

func getAppFolder(opt buildOptions, contextPath string) (string, error) {
Expand Down

0 comments on commit 47816e4

Please sign in to comment.