Skip to content

Commit 4c53c10

Browse files
committed
Refactor publish command
1 parent fdd83d0 commit 4c53c10

24 files changed

+1233
-281
lines changed

cli/azd/cmd/testdata/TestUsage-azd-deploy.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Usage
1111
Flags
1212
--all : Deploys all services that are listed in azure.yaml
1313
-e, --environment string : The name of the environment to use.
14+
--force-publish : Force publishes and overwrites existing images
1415
--from-package string : Deploys the packaged service located at the provided path. Supports zipped file packages (file path) or container images (image tag).
1516

1617
Global Flags

cli/azd/cmd/testdata/TestUsage-azd-publish.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
Publish a service to a container registry.
33

4-
Only works with Container App services.
4+
Supports Container App services only.
5+
Target registry set by AZURE_CONTAINER_REGISTRY_ENDPOINT environment variable or docker.registry in azure.yaml.
56

67
Usage
78
azd publish <service> [flags]

cli/azd/internal/cmd/deploy.go

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
2-
// Licensed under the MIT License.
32

43
package cmd
54

@@ -33,11 +32,11 @@ import (
3332
)
3433

3534
type DeployFlags struct {
36-
ServiceName string
37-
All bool
38-
PublishOnly bool
39-
fromPackage string
40-
global *internal.GlobalCommandOptions
35+
ServiceName string
36+
All bool
37+
fromPackage string
38+
forcePublish bool
39+
global *internal.GlobalCommandOptions
4140
*internal.EnvFlag
4241
}
4342

@@ -71,21 +70,19 @@ func (d *DeployFlags) bindCommon(local *pflag.FlagSet, global *internal.GlobalCo
7170
false,
7271
"Deploys all services that are listed in "+azdcontext.ProjectFileName,
7372
)
74-
local.BoolVar(
75-
&d.PublishOnly,
76-
"publish-only",
77-
false,
78-
"Publishes the container image to the registry without deploying the application.",
79-
)
80-
// `azd publish` is an alias for this
81-
_ = local.MarkHidden("publish-only")
8273
local.StringVar(
8374
&d.fromPackage,
8475
"from-package",
8576
"",
8677
//nolint:lll
8778
"Deploys the packaged service located at the provided path. Supports zipped file packages (file path) or container images (image tag).",
8879
)
80+
local.BoolVar(
81+
&d.forcePublish,
82+
"force-publish",
83+
false,
84+
"Force publishes and overwrites existing images",
85+
)
8986
}
9087

9188
func (d *DeployFlags) SetCommon(envFlag *internal.EnvFlag) {
@@ -230,10 +227,6 @@ func (da *DeployAction) Run(ctx context.Context) (*actions.ActionResult, error)
230227

231228
verb := "Deploying"
232229
commandTitle := "Deploying services (azd deploy)"
233-
if da.flags.PublishOnly {
234-
verb = "Publishing"
235-
commandTitle = "Publishing services (azd publish)"
236-
}
237230

238231
// Command title
239232
da.console.MessageUxItem(ctx, &ux.MessageTitle{
@@ -266,25 +259,19 @@ func (da *DeployAction) Run(ctx context.Context) (*actions.ActionResult, error)
266259
da.console.WarnForFeature(ctx, alphaFeatureId)
267260
}
268261

269-
if da.flags.PublishOnly {
270-
// Check if this service is a container app
271-
if svc.Host != project.ContainerAppTarget {
272-
da.console.StopSpinner(ctx, stepMessage, input.StepFailed)
273-
return nil, fmt.Errorf(
274-
"'publish' is only supported for container app services, but service '%s' has host type '%s'",
275-
svc.Name, svc.Host)
276-
}
277-
278-
// Set publish-only context and continue with normal deploy flow
279-
ctx = project.WithPublishOnly(ctx, true)
280-
}
281-
282262
var packageResult *project.ServicePackageResult
263+
var publishResult *project.ServicePublishResult
264+
var publishOptions *project.PublishOptions
265+
283266
if da.flags.fromPackage != "" {
284267
// --from-package set, skip packaging
285268
packageResult = &project.ServicePackageResult{
286269
PackagePath: da.flags.fromPackage,
287270
}
271+
272+
publishOptions = &project.PublishOptions{
273+
Overwrite: da.flags.forcePublish,
274+
}
288275
} else {
289276
// --from-package not set, automatically package the application
290277
packageResult, err = async.RunWithProgress(
@@ -297,11 +284,31 @@ func (da *DeployAction) Run(ctx context.Context) (*actions.ActionResult, error)
297284
},
298285
)
299286

300-
// do not stop progress here as next step is to deploy
287+
// do not stop progress here as next step is to publish
301288
if err != nil {
302289
da.console.StopSpinner(ctx, stepMessage, input.StepFailed)
303290
return nil, err
304291
}
292+
293+
publishOptions = &project.PublishOptions{
294+
Overwrite: true,
295+
}
296+
}
297+
298+
publishResult, err = async.RunWithProgress(
299+
func(publishProgress project.ServiceProgress) {
300+
progressMessage := fmt.Sprintf("Publishing service %s (%s)", svc.Name, publishProgress.Message)
301+
da.console.ShowSpinner(ctx, progressMessage, input.Step)
302+
},
303+
func(progress *async.Progress[project.ServiceProgress]) (*project.ServicePublishResult, error) {
304+
return da.serviceManager.Publish(ctx, svc, packageResult, progress, publishOptions)
305+
},
306+
)
307+
308+
// do not stop progress here as next step is to deploy
309+
if err != nil {
310+
da.console.StopSpinner(ctx, stepMessage, input.StepFailed)
311+
return nil, err
305312
}
306313

307314
deployResult, err := async.RunWithProgress(
@@ -310,7 +317,7 @@ func (da *DeployAction) Run(ctx context.Context) (*actions.ActionResult, error)
310317
da.console.ShowSpinner(ctx, progressMessage, input.Step)
311318
},
312319
func(progress *async.Progress[project.ServiceProgress]) (*project.ServiceDeployResult, error) {
313-
return da.serviceManager.Deploy(ctx, svc, packageResult, progress)
320+
return da.serviceManager.Deploy(ctx, svc, packageResult, publishResult, progress)
314321
},
315322
)
316323

@@ -349,10 +356,6 @@ func (da *DeployAction) Run(ctx context.Context) (*actions.ActionResult, error)
349356
}
350357

351358
messageHeader := fmt.Sprintf("Your application was deployed to Azure in %s.", ux.DurationAsText(since(startTime)))
352-
if da.flags.PublishOnly {
353-
messageHeader = fmt.Sprintf("Your application was published to the container registry in %s.",
354-
ux.DurationAsText(since(startTime)))
355-
}
356359

357360
return &actions.ActionResult{
358361
Message: &actions.ResultMessage{

0 commit comments

Comments
 (0)