Skip to content
Open
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 @@ -83,6 +83,9 @@
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 @@ -113,6 +116,8 @@
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 @@ -134,6 +139,8 @@
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 @@ -177,6 +184,8 @@
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 @@ -236,6 +245,8 @@
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 @@ -275,6 +286,8 @@
resolvedFlags.Priority.Value = options.Priority
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 @@ -312,6 +325,8 @@
resolvedFlags.ForcePackageDownload,
resolvedFlags.DeploymentTargets,
resolvedFlags.ExcludeTargets,
resolvedFlags.SpecificTargetTagNames,
resolvedFlags.ExcludedTargetTagNames,
resolvedFlags.Variables,
resolvedFlags.DeploymentFreezeNames,
resolvedFlags.DeploymentFreezeOverrideReason,
Expand Down Expand Up @@ -529,8 +544,9 @@
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 @@ -629,6 +645,20 @@
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 @@ -822,6 +852,56 @@
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 {

Check failure on line 865 in pkg/cmd/release/deploy/deploy.go

View workflow job for this annotation

GitHub Actions / govulncheck

step.AvailableTagSets undefined (type *deployments.DeploymentTemplateStep has no field or method 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 @@ -1077,6 +1157,34 @@
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)
Expand All @@ -1085,7 +1193,8 @@
Priority: cyan(%s)
Package Download: cyan(%s)
Deployment Targets: cyan(%s)
`)), deployAtStr, skipStepsStr, gfmStr, priorityStr, pkgDownloadStr, depTargetsStr)
Target Tags: cyan(%s)
`)), deployAtStr, skipStepsStr, gfmStr, priorityStr, 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