Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions internal/pkg/cli/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ const (
yesInitWorkloadFlag = "init-wkld"

// Build flags.
dockerFileFlag = "dockerfile"
dockerFileContextFlag = "build-context"
imageTagFlag = "tag"
stackOutputDirFlag = "output-dir"
uploadAssetsFlag = "upload-assets"
deployFlag = "deploy"
diffFlag = "diff"
diffAutoApproveFlag = "diff-yes"
sourcesFlag = "sources"
dockerFileFlag = "dockerfile"
dockerFileContextFlag = "build-context"
dockerFileBuildArgsFlag = "build-args"
imageTagFlag = "tag"
stackOutputDirFlag = "output-dir"
uploadAssetsFlag = "upload-assets"
deployFlag = "deploy"
diffFlag = "diff"
diffAutoApproveFlag = "diff-yes"
sourcesFlag = "sources"

// Flags for operational commands.
limitFlag = "limit"
Expand Down Expand Up @@ -192,6 +193,8 @@ var (
imageFlagDescription = fmt.Sprintf(`The location of an existing Docker image.
Cannot be specified with --%s or --%s.`, dockerFileFlag, dockerFileContextFlag)
dockerFileFlagDescription = fmt.Sprintf(`Path to the Dockerfile.
Cannot be specified with --%s.`, imageFlag)
dockerFileBuildArgsFlagDescription = fmt.Sprintf(`Key-value pairs converted to --build-args.
Cannot be specified with --%s.`, imageFlag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot be specified with --%s.`, imageFlag

aw yay thank you for adding this in the help menu message !

dockerFileContextFlagDescription = fmt.Sprintf(`Path to the Docker build context.
Cannot be specified with --%s.`, imageFlag)
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/cli/task_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type runTaskVars struct {
image string
dockerfilePath string
dockerfileContextPath string
dockerfileBuildArgs map[string]string
imageTag string

taskRole string
Expand Down Expand Up @@ -372,6 +373,10 @@ func (o *runTaskOpts) Validate() error {
return errors.New("cannot specify both `--image` and `--build-context`")
}

if o.image != "" && o.dockerfileBuildArgs != nil {
return errors.New("cannot specify both `--image` and `--build-args`")
}

if o.isDockerfileSet {
if _, err := o.fs.Stat(o.dockerfilePath); err != nil {
return fmt.Errorf("invalid `--dockerfile` path: %w", err)
Expand Down Expand Up @@ -963,6 +968,7 @@ func (o *runTaskOpts) buildAndPushImage(uri string) error {
Dockerfile: o.dockerfilePath,
Context: ctx,
Tags: append([]string{imageTagLatest}, additionalTags...),
Args: o.dockerfileBuildArgs,
}
buildArgsList, err := buildArgs.GenerateDockerBuildArgs(dockerengine.New(exec.NewCmd()))
if err != nil {
Expand Down Expand Up @@ -1193,6 +1199,7 @@ func BuildTaskRunCmd() *cobra.Command {
cmd.Flags().StringVarP(&vars.groupName, taskGroupNameFlag, nameFlagShort, "", taskGroupFlagDescription)

cmd.Flags().StringVar(&vars.dockerfilePath, dockerFileFlag, defaultDockerfilePath, dockerFileFlagDescription)
cmd.Flags().StringToStringVar(&vars.dockerfileBuildArgs, dockerFileBuildArgsFlag, nil, dockerFileBuildArgsFlagDescription)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an example to the [task run help menu] about how we can run task from dockerfile 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example to the help menu showing how to use the --build-args flag. Thanks for the suggestion!

cmd.Flags().StringVar(&vars.dockerfileContextPath, dockerFileContextFlag, "", dockerFileContextFlagDescription)
cmd.Flags().StringVarP(&vars.image, imageFlag, imageFlagShort, "", imageFlagDescription)
cmd.Flags().StringVar(&vars.imageTag, imageTagFlag, "", taskImageTagFlagDescription)
Expand Down Expand Up @@ -1228,6 +1235,7 @@ func BuildTaskRunCmd() *cobra.Command {

buildFlags := pflag.NewFlagSet("Build", pflag.ContinueOnError)
buildFlags.AddFlag(cmd.Flags().Lookup(dockerFileFlag))
buildFlags.AddFlag(cmd.Flags().Lookup(dockerFileBuildArgsFlag))
buildFlags.AddFlag(cmd.Flags().Lookup(dockerFileContextFlag))
buildFlags.AddFlag(cmd.Flags().Lookup(imageFlag))
buildFlags.AddFlag(cmd.Flags().Lookup(imageTagFlag))
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/cli/task_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestTaskRunOpts_Validate(t *testing.T) {

inImage string
inDockerfilePath string
inDockerfileBuildArgs map[string]string
inDockerfileContextPath string

inTaskRole string
Expand Down Expand Up @@ -235,6 +236,16 @@ func TestTaskRunOpts_Validate(t *testing.T) {

wantedError: errors.New("cannot specify both `--image` and `--build-context`"),
},
"both build args and image name specified": {
basicOpts: defaultOpts,

inImage: "113459295.dkr.ecr.ap-northeast-1.amazonaws.com/my-app",
inDockerfileBuildArgs: map[string]string{
"KEY": "VALUE",
},

wantedError: errors.New("cannot specify both `--image` and `--build-args`"),
},
"both dockerfile and image name specified": {
basicOpts: defaultOpts,

Expand Down Expand Up @@ -417,6 +428,7 @@ func TestTaskRunOpts_Validate(t *testing.T) {
subnets: tc.inSubnets,
securityGroups: tc.inSecurityGroups,
dockerfilePath: tc.inDockerfilePath,
dockerfileBuildArgs: tc.inDockerfileBuildArgs,
dockerfileContextPath: tc.inDockerfileContextPath,
envVars: tc.inEnvVars,
envFile: tc.inEnvFile,
Expand Down