support compose build --pull

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-03-02 09:03:04 +01:00
parent dd843bded8
commit 6f73007265
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
8 changed files with 24 additions and 10 deletions

View File

@ -44,7 +44,7 @@ func newComposeService(ctx store.AciContext) aciComposeService {
} }
} }
func (cs *aciComposeService) Build(ctx context.Context, project *types.Project) error { func (cs *aciComposeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }

View File

@ -28,7 +28,7 @@ import (
type composeService struct { type composeService struct {
} }
func (c *composeService) Build(ctx context.Context, project *types.Project) error { func (c *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }

View File

@ -28,7 +28,7 @@ import (
// Service manages a compose project // Service manages a compose project
type Service interface { type Service interface {
// Build executes the equivalent to a `compose build` // Build executes the equivalent to a `compose build`
Build(ctx context.Context, project *types.Project) error Build(ctx context.Context, project *types.Project, options BuildOptions) error
// Push executes the equivalent ot a `compose push` // Push executes the equivalent ot a `compose push`
Push(ctx context.Context, project *types.Project) error Push(ctx context.Context, project *types.Project) error
// Pull executes the equivalent of a `compose pull` // Pull executes the equivalent of a `compose pull`
@ -65,6 +65,12 @@ type Service interface {
UnPause(ctx context.Context, project *types.Project) error UnPause(ctx context.Context, project *types.Project) error
} }
// BuildOptions group options of the Build API
type BuildOptions struct {
// Pull always attempt to pull a newer version of the image
Pull bool
}
// CreateOptions group options of the Create API // CreateOptions group options of the Create API
type CreateOptions struct { type CreateOptions struct {
// Remove legacy containers for services that are not defined in the project // Remove legacy containers for services that are not defined in the project

View File

@ -18,6 +18,7 @@ package compose
import ( import (
"context" "context"
"github.com/docker/compose-cli/api/compose"
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -30,6 +31,7 @@ type buildOptions struct {
*projectOptions *projectOptions
composeOptions composeOptions
quiet bool quiet bool
pull bool
} }
func buildCommand(p *projectOptions) *cobra.Command { func buildCommand(p *projectOptions) *cobra.Command {
@ -51,6 +53,7 @@ func buildCommand(p *projectOptions) *cobra.Command {
}, },
} }
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT") cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
return cmd return cmd
} }
@ -66,7 +69,9 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error {
} }
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Build(ctx, project) return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{
Pull: opts.pull,
})
}) })
return err return err
} }

View File

@ -32,8 +32,8 @@ import (
"github.com/docker/compose-cli/api/errdefs" "github.com/docker/compose-cli/api/errdefs"
) )
func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project) error { func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
return e.compose.Build(ctx, project) return e.compose.Build(ctx, project, options)
} }
func (e ecsLocalSimulation) Push(ctx context.Context, project *types.Project) error { func (e ecsLocalSimulation) Push(ctx context.Context, project *types.Project) error {

View File

@ -31,7 +31,7 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
) )
func (b *ecsAPIService) Build(ctx context.Context, project *types.Project) error { func (b *ecsAPIService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }

View File

@ -124,7 +124,7 @@ func (s *composeService) List(ctx context.Context, opts compose.ListOptions) ([]
} }
// Build executes the equivalent to a `compose build` // Build executes the equivalent to a `compose build`
func (s *composeService) Build(ctx context.Context, project *types.Project) error { func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }

View File

@ -19,6 +19,7 @@ package compose
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/docker/compose-cli/api/compose"
"os" "os"
"path" "path"
"strings" "strings"
@ -32,14 +33,16 @@ import (
bclient "github.com/moby/buildkit/client" bclient "github.com/moby/buildkit/client"
) )
func (s *composeService) Build(ctx context.Context, project *types.Project) error { func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
opts := map[string]build.Options{} opts := map[string]build.Options{}
imagesToBuild := []string{} imagesToBuild := []string{}
for _, service := range project.Services { for _, service := range project.Services {
if service.Build != nil { if service.Build != nil {
imageName := getImageName(service, project.Name) imageName := getImageName(service, project.Name)
imagesToBuild = append(imagesToBuild, imageName) imagesToBuild = append(imagesToBuild, imageName)
opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName) buildOptions := s.toBuildOptions(service, project.WorkingDir, imageName)
buildOptions.Pull = options.Pull
opts[imageName] = buildOptions
} }
} }