Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
113 changes: 111 additions & 2 deletions pkg/cmd/release/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const (
FlagAliasExcludeTarget = "exclude-target"
FlagAliasExcludeMachines = "excludeMachines" // octo wants a comma separated list. We prefer specifying --exclude-target multiple times, but CSV also works because pflag does it for free

FlagSpecificTargetTagName = "specific-target-tag"
FlagExcludedTargetTagName = "excluded-target-tag"

FlagVariable = "variable"

FlagUpdateVariables = "update-variables"
Expand Down Expand Up @@ -110,6 +113,8 @@ type DeployFlags struct {
ForcePackageDownload *flag.Flag[bool]
DeploymentTargets *flag.Flag[[]string]
ExcludeTargets *flag.Flag[[]string]
SpecificTargetTagNames *flag.Flag[[]string]
ExcludedTargetTagNames *flag.Flag[[]string]
DeploymentFreezeNames *flag.Flag[[]string]
DeploymentFreezeOverrideReason *flag.Flag[string]
}
Expand All @@ -130,6 +135,8 @@ func NewDeployFlags() *DeployFlags {
ForcePackageDownload: flag.New[bool](FlagForcePackageDownload, false),
DeploymentTargets: flag.New[[]string](FlagDeploymentTarget, false),
ExcludeTargets: flag.New[[]string](FlagExcludeDeploymentTarget, false),
SpecificTargetTagNames: flag.New[[]string](FlagSpecificTargetTagName, false),
ExcludedTargetTagNames: flag.New[[]string](FlagExcludedTargetTagName, false),
DeploymentFreezeNames: flag.New[[]string](FlagDeploymentFreezeName, false),
DeploymentFreezeOverrideReason: flag.New[string](FlagDeploymentFreezeOverrideReason, false),
}
Expand Down Expand Up @@ -172,6 +179,8 @@ func NewCmdDeploy(f factory.Factory) *cobra.Command {
flags.BoolVarP(&deployFlags.ForcePackageDownload.Value, deployFlags.ForcePackageDownload.Name, "", false, "Force re-download of packages")
flags.StringArrayVarP(&deployFlags.DeploymentTargets.Value, deployFlags.DeploymentTargets.Name, "", nil, "Deploy to this target (can be specified multiple times)")
flags.StringArrayVarP(&deployFlags.ExcludeTargets.Value, deployFlags.ExcludeTargets.Name, "", nil, "Deploy to targets except for this (can be specified multiple times)")
flags.StringArrayVarP(&deployFlags.SpecificTargetTagNames.Value, deployFlags.SpecificTargetTagNames.Name, "", nil, "Deploy to targets matching this tag (can be specified multiple times)")
flags.StringArrayVarP(&deployFlags.ExcludedTargetTagNames.Value, deployFlags.ExcludedTargetTagNames.Name, "", nil, "Deploy to targets except for those matching this tag (can be specified multiple times)")
flags.StringArrayVarP(&deployFlags.DeploymentFreezeNames.Value, deployFlags.DeploymentFreezeNames.Name, "", nil, "Override this deployment freeze (can be specified multiple times)")
flags.StringVarP(&deployFlags.DeploymentFreezeOverrideReason.Value, deployFlags.DeploymentFreezeOverrideReason.Name, "", "", "Reason for overriding a deployment freeze")

Expand Down Expand Up @@ -226,6 +235,8 @@ func deployRun(cmd *cobra.Command, f factory.Factory, flags *DeployFlags) error
ForcePackageDownload: flags.ForcePackageDownload.Value,
DeploymentTargets: flags.DeploymentTargets.Value,
ExcludeTargets: flags.ExcludeTargets.Value,
SpecificTargetTagNames: flags.SpecificTargetTagNames.Value,
ExcludedTargetTagNames: flags.ExcludedTargetTagNames.Value,
DeploymentFreezeNames: flags.DeploymentFreezeNames.Value,
DeploymentFreezeOverrideReason: flags.DeploymentFreezeOverrideReason.Value,
Variables: parsedVariables,
Expand Down Expand Up @@ -264,6 +275,8 @@ func deployRun(cmd *cobra.Command, f factory.Factory, flags *DeployFlags) error
resolvedFlags.GuidedFailureMode.Value = options.GuidedFailureMode
resolvedFlags.DeploymentTargets.Value = options.DeploymentTargets
resolvedFlags.ExcludeTargets.Value = options.ExcludeTargets
resolvedFlags.SpecificTargetTagNames.Value = options.SpecificTargetTagNames
resolvedFlags.ExcludedTargetTagNames.Value = options.ExcludedTargetTagNames
resolvedFlags.DeploymentFreezeNames.Value = options.DeploymentFreezeNames
resolvedFlags.DeploymentFreezeOverrideReason.Value = options.DeploymentFreezeOverrideReason

Expand Down Expand Up @@ -296,6 +309,8 @@ func deployRun(cmd *cobra.Command, f factory.Factory, flags *DeployFlags) error
resolvedFlags.ForcePackageDownload,
resolvedFlags.DeploymentTargets,
resolvedFlags.ExcludeTargets,
resolvedFlags.SpecificTargetTagNames,
resolvedFlags.ExcludedTargetTagNames,
resolvedFlags.Variables,
resolvedFlags.DeploymentFreezeNames,
resolvedFlags.DeploymentFreezeOverrideReason,
Expand Down Expand Up @@ -519,8 +534,9 @@ func AskQuestions(octopus *octopusApiClient.Client, stdout io.Writer, asker ques
isGuidedFailureModeSpecified := options.GuidedFailureMode != ""
isForcePackageDownloadSpecified := options.ForcePackageDownloadWasSpecified
isDeploymentTargetsSpecified := len(options.DeploymentTargets) > 0 || len(options.ExcludeTargets) > 0
isDeploymentTargetTagsSpecified := len(options.SpecificTargetTagNames) > 0 || len(options.ExcludedTargetTagNames) > 0

allAdvancedOptionsSpecified := isDeployAtSpecified && isExcludedStepsSpecified && isGuidedFailureModeSpecified && isForcePackageDownloadSpecified && isDeploymentTargetsSpecified
allAdvancedOptionsSpecified := isDeployAtSpecified && isExcludedStepsSpecified && isGuidedFailureModeSpecified && isForcePackageDownloadSpecified && isDeploymentTargetsSpecified && isDeploymentTargetTagsSpecified

shouldAskAdvancedQuestions := false
if !allAdvancedOptionsSpecified {
Expand Down Expand Up @@ -619,6 +635,20 @@ func AskQuestions(octopus *octopusApiClient.Client, stdout io.Writer, asker ques
return err
}
}

if !isDeploymentTargetTagsSpecified {
if len(deploymentEnvironmentIDs) == 0 { // if the Q&A process earlier hasn't loaded environments already, we need to load them now
selectedEnvironments, err := executionscommon.FindEnvironments(octopus, options.Environments)
if err != nil {
return err
}
deploymentEnvironmentIDs = util.SliceTransform(selectedEnvironments, func(env *environments.Environment) string { return env.ID })
}
options.SpecificTargetTagNames, options.ExcludedTargetTagNames, err = askTargetTags(octopus, asker, space.ID, selectedRelease.ID, deploymentEnvironmentIDs)
if err != nil {
return err
}
}
}
// DONE
return nil
Expand Down Expand Up @@ -812,6 +842,56 @@ func askDeploymentTargets(octopus *octopusApiClient.Client, asker question.Asker
return nil, nil
}

func askTargetTags(octopus *octopusApiClient.Client, asker question.Asker, spaceID string, releaseID string, deploymentEnvironmentIDs []string) ([]string, []string, error) {
var results []string

// collect all available target tags from deployment previews across all environments
for _, envID := range deploymentEnvironmentIDs {
preview, err := deployments.GetReleaseDeploymentPreview(octopus, spaceID, releaseID, envID, true)
if err != nil {
return nil, nil, err
}
for _, step := range preview.StepsToExecute {
for _, tagSet := range step.AvailableTagSets {
for _, tag := range tagSet.AvailableTags {
canonicalName := tagSet.TagSetName + "/" + tag.TagName
if !util.SliceContains(results, canonicalName) {
results = append(results, canonicalName)
}
}
}
}
}

if len(results) == 0 {
return nil, nil, nil
}

sort.Strings(results)

var selectedSpecificTags []string
err := asker(&survey.MultiSelect{
Message: "Specific target tags to include (If none selected, include all)",
Options: results,
}, &selectedSpecificTags)
if err != nil {
return nil, nil, err
}

var selectedExcludedTags []string
if len(selectedSpecificTags) == 0 {
err = asker(&survey.MultiSelect{
Message: "Target tags to exclude (If none selected, exclude none)",
Options: results,
}, &selectedExcludedTags)
if err != nil {
return nil, nil, err
}
}

return selectedSpecificTags, selectedExcludedTags, nil
}

func askDeploymentPreviewVariables(octopus *octopusApiClient.Client, variablesFromCmd map[string]string, asker question.Asker, spaceID string, releaseID string, deploymentPreviewsReqests []deployments.DeploymentPreviewRequest) (map[string]string, error) {
previews, err := deployments.GetReleaseDeploymentPreviews(octopus, spaceID, releaseID, deploymentPreviewsReqests, true)
if err != nil {
Expand Down Expand Up @@ -1065,14 +1145,43 @@ func PrintAdvancedSummary(stdout io.Writer, options *executor.TaskOptionsDeployR
depTargetsStr = sb.String()
}

targetTagsStr := "All included"
if len(options.SpecificTargetTagNames) != 0 || len(options.ExcludedTargetTagNames) != 0 {
sb := strings.Builder{}
if len(options.SpecificTargetTagNames) > 0 {
sb.WriteString("Include ")
for idx, name := range options.SpecificTargetTagNames {
if idx > 0 {
sb.WriteString(",")
}
sb.WriteString(name)
}
}
if len(options.ExcludedTargetTagNames) > 0 {
if sb.Len() > 0 {
sb.WriteString("; ")
}

sb.WriteString("Exclude ")
for idx, name := range options.ExcludedTargetTagNames {
if idx > 0 {
sb.WriteString(",")
}
sb.WriteString(name)
}
}
targetTagsStr = sb.String()
}

_, _ = fmt.Fprintf(stdout, output.FormatDoc(heredoc.Doc(`
bold(Additional Options):
Deploy Time: cyan(%s)
Skipped Steps: cyan(%s)
Guided Failure Mode: cyan(%s)
Package Download: cyan(%s)
Deployment Targets: cyan(%s)
`)), deployAtStr, skipStepsStr, gfmStr, pkgDownloadStr, depTargetsStr)
Target Tags: cyan(%s)
`)), deployAtStr, skipStepsStr, gfmStr, pkgDownloadStr, depTargetsStr, targetTagsStr)
}

func selectRelease(octopus *octopusApiClient.Client, ask question.Asker, questionText string, space *spaces.Space, project *projects.Project, channel *channels.Channel) (*releases.Release, error) {
Expand Down
Loading
Loading