Skip to content

Commit

Permalink
Feature: Add build environment arguments to all deploy commands (#327)
Browse files Browse the repository at this point in the history
* Adds build variables to deployBranchCmd

* fixes gomod

* Adds build vars to other deploy commands

* Adds tests and changes split logic for build env arguments

* Supports promote build vars

* Updates docs

* Updates documentation for build vars

---------

Co-authored-by: Blaize Kaye <blaize.kaye@amazee.io>
  • Loading branch information
bomoko and Blaize Kaye committed Mar 20, 2024
1 parent a2838f8 commit b638de5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 21 deletions.
40 changes: 38 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ var deployPromoteCmd = &cobra.Command{
if cmdProjectName == "" || sourceEnvironment == "" || destinationEnvironment == "" {
return fmt.Errorf("Missing arguments: Project name, source environment, or destination environment is not defined")
}

buildVarStrings, err := cmd.Flags().GetStringSlice("buildvar")
if err != nil {
return err
}
buildVarMap, err := buildVarsToMap(buildVarStrings)
if err != nil {
return err
}

if yesNo(fmt.Sprintf("You are attempting to promote environment '%s' to '%s' for project '%s', are you sure?", sourceEnvironment, destinationEnvironment, cmdProjectName)) {
current := lagoonCLIConfig.Current
lc := client.New(
Expand All @@ -122,6 +132,7 @@ var deployPromoteCmd = &cobra.Command{
SourceEnvironment: sourceEnvironment,
DestinationEnvironment: destinationEnvironment,
Project: cmdProjectName,
BuildVariables: buildVarMap,
ReturnData: returnData,
}, lc)
if err != nil {
Expand Down Expand Up @@ -153,6 +164,16 @@ This environment should already exist in lagoon. It is analogous with the 'Deplo
if err != nil {
return err
}

buildVarStrings, err := cmd.Flags().GetStringSlice("buildvar")
if err != nil {
return err
}
buildVarMap, err := buildVarsToMap(buildVarStrings)
if err != nil {
return err
}

if cmdProjectName == "" || cmdProjectEnvironment == "" {
return fmt.Errorf("Missing arguments: Project name or environment name is not defined")
}
Expand All @@ -171,7 +192,8 @@ This environment should already exist in lagoon. It is analogous with the 'Deplo
Name: cmdProjectName,
},
},
ReturnData: returnData,
ReturnData: returnData,
BuildVariables: buildVarMap,
}, lc)
if err != nil {
return err
Expand Down Expand Up @@ -225,6 +247,15 @@ This pullrequest may not already exist as an environment in lagoon.`,
baseBranchRef == "" || headBranchName == "" || headBranchRef == "" {
return fmt.Errorf("Missing arguments: Project name, title, number, baseBranchName, baseBranchRef, headBranchName, or headBranchRef is not defined")
}
buildVarStrings, err := cmd.Flags().GetStringSlice("buildvar")
if err != nil {
return err
}
buildVarMap, err := buildVarsToMap(buildVarStrings)
if err != nil {
return err
}

returnData, err := cmd.Flags().GetBool("returnData")
if err != nil {
return err
Expand All @@ -249,6 +280,7 @@ This pullrequest may not already exist as an environment in lagoon.`,
HeadBranchName: headBranchName,
HeadBranchRef: headBranchRef,
ReturnData: returnData,
BuildVariables: buildVarMap,
}, lc)
if err != nil {
return err
Expand All @@ -272,14 +304,17 @@ func init() {

const returnDataUsageText = "Returns the build name instead of success text"
deployLatestCmd.Flags().Bool("returnData", false, returnDataUsageText)
deployLatestCmd.Flags().StringSlice("buildvar", []string{}, "Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])")

deployBranchCmd.Flags().StringP("branch", "b", "", "Branch name to deploy")
deployBranchCmd.Flags().StringP("branchRef", "r", "", "Branch ref to deploy")
deployBranchCmd.Flags().Bool("returnData", false, returnDataUsageText)
deployBranchCmd.Flags().StringSlice("buildvar", []string{}, "Adds one or more build variables to deployment, key and values separated by `=`: `--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2]`")
deployBranchCmd.Flags().StringSlice("buildvar", []string{}, "Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])")

deployPromoteCmd.Flags().StringP("destination", "d", "", "Destination environment name to create")
deployPromoteCmd.Flags().StringP("source", "s", "", "Source environment name to use as the base to deploy from")
deployPromoteCmd.Flags().Bool("returnData", false, returnDataUsageText)
deployPromoteCmd.Flags().StringSlice("buildvar", []string{}, "Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])")

deployPullrequestCmd.Flags().StringP("title", "t", "", "Pullrequest title")
deployPullrequestCmd.Flags().UintP("number", "n", 0, "Pullrequest number")
Expand All @@ -288,4 +323,5 @@ func init() {
deployPullrequestCmd.Flags().StringP("headBranchName", "H", "", "Pullrequest head branch name")
deployPullrequestCmd.Flags().StringP("headBranchRef", "M", "", "Pullrequest head branch reference hash")
deployPullrequestCmd.Flags().Bool("returnData", false, returnDataUsageText)
deployPullrequestCmd.Flags().StringSlice("buildvar", []string{}, "Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])")
}
1 change: 1 addition & 0 deletions docs/commands/lagoon_deploy_branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ lagoon deploy branch [flags]
```
-b, --branch string Branch name to deploy
-r, --branchRef string Branch ref to deploy
--buildvar strings Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])
-h, --help help for branch
--returnData Returns the build name instead of success text
```
Expand Down
5 changes: 3 additions & 2 deletions docs/commands/lagoon_deploy_latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ lagoon deploy latest [flags]
### Options

```
-h, --help help for latest
--returnData Returns the build name instead of success text
--buildvar strings Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])
-h, --help help for latest
--returnData Returns the build name instead of success text
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_deploy_promote.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lagoon deploy promote [flags]
### Options

```
--buildvar strings Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])
-d, --destination string Destination environment name to create
-h, --help help for promote
--returnData Returns the build name instead of success text
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_deploy_pullrequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ lagoon deploy pullrequest [flags]
```
-N, --baseBranchName string Pullrequest base branch name
-R, --baseBranchRef string Pullrequest base branch reference hash
--buildvar strings Add one or more build variables to deployment (--buildvar KEY1=VALUE1 [--buildvar KEY2=VALUE2])
-H, --headBranchName string Pullrequest head branch name
-M, --headBranchRef string Pullrequest head branch reference hash
-h, --help help for pullrequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mutation (
$environment: EnvironmentInput!, $returnData: Boolean!) {
$environment: EnvironmentInput!, $returnData: Boolean!, $buildVariables: [EnvKeyValueInput]) {
deployEnvironmentLatest(input: {
environment: $environment
returnData: $returnData
buildVariables: $buildVariables
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mutation (
$project: String!,
$sourceEnvironment: String!,
$destinationEnvironment: String!,
$returnData: Boolean!) {
$returnData: Boolean!,
$buildVariables: [EnvKeyValueInput]) {
deployEnvironmentPromote(input:{
sourceEnvironment:{
name: $sourceEnvironment
Expand All @@ -14,6 +15,7 @@ mutation (
name: $project
}
destinationEnvironment: $destinationEnvironment
buildVariables: $buildVariables
returnData: $returnData
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ mutation (
$baseBranchRef: String!,
$headBranchName: String!,
$headBranchRef: String!,
$returnData: Boolean!) {
$returnData: Boolean!,
$buildVariables: [EnvKeyValueInput]) {
deployEnvironmentPullrequest(input: {
project: $project
number: $number
Expand All @@ -16,6 +17,7 @@ mutation (
headBranchName: $headBranchName
headBranchRef: $headBranchRef
returnData: $returnData
buildVariables: $buildVariables
}
)
}
31 changes: 17 additions & 14 deletions internal/schema/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package schema

// DeployEnvironmentLatestInput is used as the input for deploying an environment.
type DeployEnvironmentLatestInput struct {
Environment EnvironmentInput `json:"environment"`
ReturnData bool `json:"returnData"`
Environment EnvironmentInput `json:"environment"`
ReturnData bool `json:"returnData"`
BuildVariables []EnvKeyValueInput `json:"buildVariables,omitempty"`
}

// DeployEnvironmentLatest is the response.
Expand All @@ -13,14 +14,15 @@ type DeployEnvironmentLatest struct {

// DeployEnvironmentPullrequestInput is used as the input for deploying a pull request.
type DeployEnvironmentPullrequestInput struct {
Project ProjectInput `json:"project"`
Number uint `json:"number"`
Title string `json:"title"`
BaseBranchName string `json:"baseBranchName"`
BaseBranchRef string `json:"baseBranchRef"`
HeadBranchName string `json:"headBranchName"`
HeadBranchRef string `json:"headBranchRef"`
ReturnData bool `json:"returnData"`
Project ProjectInput `json:"project"`
Number uint `json:"number"`
Title string `json:"title"`
BaseBranchName string `json:"baseBranchName"`
BaseBranchRef string `json:"baseBranchRef"`
HeadBranchName string `json:"headBranchName"`
HeadBranchRef string `json:"headBranchRef"`
ReturnData bool `json:"returnData"`
BuildVariables []EnvKeyValueInput `json:"buildVariables,omitempty"`
}

// DeployEnvironmentPullrequest is the response.
Expand All @@ -44,10 +46,11 @@ type DeployEnvironmentBranch struct {

// DeployEnvironmentPromoteInput is used as the input for promoting one environment to another.
type DeployEnvironmentPromoteInput struct {
Project string `json:"project"`
SourceEnvironment string `json:"sourceEnvironment"`
DestinationEnvironment string `json:"destinationEnvironment"`
ReturnData bool `json:"returnData"`
Project string `json:"project"`
SourceEnvironment string `json:"sourceEnvironment"`
DestinationEnvironment string `json:"destinationEnvironment"`
BuildVariables []EnvKeyValueInput `json:"buildVariables,omitempty"`
ReturnData bool `json:"returnData"`
}

// DeployEnvironmentPromote is the response.
Expand Down

0 comments on commit b638de5

Please sign in to comment.