Skip to content

Commit

Permalink
Promote active deployment (#62)
Browse files Browse the repository at this point in the history
* Promote active deployment
* print branch name without newline
* add new version 1.9.0

---------

Co-authored-by: Richard Hagen <richard.hagen@bouvet.no>
  • Loading branch information
Richard87 and Richard87 authored Oct 11, 2023
1 parent da57ba0 commit 7991aae
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
44 changes: 41 additions & 3 deletions cmd/createPromotePipelineJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package cmd

import (
"errors"
apiclient "github.com/equinor/radix-cli/generated-client/client"
"github.com/equinor/radix-cli/generated-client/client/environment"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/equinor/radix-cli/generated-client/client/application"
Expand All @@ -35,14 +37,22 @@ var createPromotePipelineJobCmd = &cobra.Command{
return err
}

useActiveDeployment, _ := cmd.Flags().GetBool("use-active-deployment")
deploymentName, _ := cmd.Flags().GetString("deployment")
fromEnvironment, _ := cmd.Flags().GetString("from-environment")
toEnvironment, _ := cmd.Flags().GetString("to-environment")
triggeredByUser, _ := cmd.Flags().GetString("user")
follow, _ := cmd.Flags().GetBool("follow")

if appName == nil || *appName == "" || deploymentName == "" || fromEnvironment == "" || toEnvironment == "" {
return errors.New("application name, deployment name, from and to environments are required")
if !useActiveDeployment && deploymentName == "" {
return errors.New("Specifying deployment name or setting use-active-deployment is required")
}
if useActiveDeployment && deploymentName != "" {
return errors.New("You cannot set use-active-deployment and specify deployment name at the same time")
}

if appName == nil || *appName == "" || fromEnvironment == "" || toEnvironment == "" {
return errors.New("application name, from and to environments are required")
}

cmd.SilenceUsage = true
Expand All @@ -51,6 +61,16 @@ var createPromotePipelineJobCmd = &cobra.Command{
if err != nil {
return err
}

if useActiveDeployment {
d, err := getActiveDeploymentName(apiClient, *appName, fromEnvironment)
if err != nil {
return err
}

deploymentName = d
}

triggerPipelineParams := application.NewTriggerPipelinePromoteParams()
triggerPipelineParams.SetAppName(*appName)
triggerPipelineParams.SetPipelineParametersPromote(&models.PipelineParametersPromote{
Expand All @@ -75,6 +95,23 @@ var createPromotePipelineJobCmd = &cobra.Command{
},
}

func getActiveDeploymentName(apiClient *apiclient.Radixapi, appName, envName string) (string, error) {
params := environment.NewGetEnvironmentParams()
params.SetAppName(appName)
params.SetEnvName(envName)

resp, err := apiClient.Environment.GetEnvironment(params, nil)
if err != nil {
return "", errors.Wrap(err, "Failed to get environment details")
}

if resp.Payload.ActiveDeployment == nil || resp.Payload.ActiveDeployment.Name == "" {
return "", errors.Errorf("Environment '%s' does not have any active deployments", envName)
}

return resp.Payload.ActiveDeployment.Name, nil
}

func init() {
createJobCmd.AddCommand(createPromotePipelineJobCmd)
createPromotePipelineJobCmd.Flags().StringP("application", "a", "", "Name of the application to be promoted")
Expand All @@ -83,5 +120,6 @@ func init() {
createPromotePipelineJobCmd.Flags().StringP("to-environment", "", "", "The deployment target environment")
createPromotePipelineJobCmd.Flags().StringP("user", "u", "", "The user who triggered the promote pipeline job")
createPromotePipelineJobCmd.Flags().BoolP("follow", "f", false, "Follow the promote pipeline job log")
createPromotePipelineJobCmd.Flags().BoolP("use-active-deployment", "", false, "Promote the active deployment")
setContextSpecificPersistentFlags(createPromotePipelineJobCmd)
}
3 changes: 2 additions & 1 deletion cmd/getBranchEnvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"errors"
"fmt"

"github.com/equinor/radix-cli/pkg/settings"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,7 +49,7 @@ var getBranchEnvironmentCmd = &cobra.Command{
return err
}

println(*environment)
fmt.Print(*environment)
return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

const (
radixCLIError = "Error: Radix CLI executed with error"
version = "1.8.1"
version = "1.9.0"
)

var rootLongHelp = strings.TrimSpace(`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/go-openapi/strfmt v0.21.7
github.com/go-openapi/swag v0.22.4
github.com/go-openapi/validate v0.22.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
k8s.io/utils v0.0.0-20230505201702-9f6742963106
Expand Down

0 comments on commit 7991aae

Please sign in to comment.