deploy

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DeployCmd = &cobra.Command{
	Use:   "deploy <deployment>",
	Short: "Run a deployment",
	Long: `
The deploy command runs a deployment for a given deployment name.

Usage:
	hyphen deploy <deployment> [flags]

Examples:
hyphen deploy deploy-dev

Use 'hyphen deploy --help' for more information about available flags.
`,
	Args: cobra.RangeArgs(0, 1),
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		return user.ErrorIfNotAuthenticated()
	},
	Run: func(cmd *cobra.Command, args []string) {
		orgId, err := flags.GetOrganizationID()
		if err != nil {
			printer.Error(cmd, err)
			return
		}

		deploymentName := ""
		if len(args) > 0 {
			deploymentName = args[0]
		}

		printer = cprint.NewCPrinter(flags.VerboseFlag)

		service := Deployment.NewService()

		deployments, err := service.SearchDeployments(orgId, deploymentName, 50, 1)

		if err != nil {
			printer.Error(cmd, fmt.Errorf("failed to list apps: %w", err))
			return
		}

		selectedDeployment := deployments[0]

		if len(deployments) > 1 {
			choices := make([]prompt.Choice, len(deployments))
			for i, deployment := range deployments {
				choices[i] = prompt.Choice{
					Id:      deployment.ID,
					Display: fmt.Sprintf("%s (%s)", deployment.Name, deployment.ID),
				}
			}

			choice, err := prompt.PromptSelection(choices, "Select a deployment to run:")

			if err != nil {
				printer.Error(cmd, err)
				return
			}

			if choice.Id == "" {
				printer.YellowPrint(("no choice made, canceling deploy"))
				return
			}

			for _, deployment := range deployments {
				if deployment.ID == choice.Id {
					selectedDeployment = deployment
					break
				}
			}
		}

		appSources := []Deployment.AppSources{}

		if noBuild {

		} else {

			firstApp := selectedDeployment.Apps[0]

			service := build.NewService()
			result, err := service.RunBuild(printer, firstApp.DeploymentSettings.ProjectEnvironment.ID, flags.VerboseFlag)
			if err != nil {
				printer.Error(cmd, err)
				return
			}
			appSources = append(appSources, Deployment.AppSources{
				AppId:    result.App.ID,
				Artifact: result.Artifact,
			},
			)
		}

		printer.Print(fmt.Sprintf("Running %s", selectedDeployment.Name))

		run, err := service.CreateRun(orgId, selectedDeployment.ID, appSources)
		if err != nil {
			printer.Error(cmd, fmt.Errorf("failed to create run: %w", err))
			return
		}

		statusModel := Deployment.StatusModel{
			OrganizationId: orgId,
			DeploymentId:   selectedDeployment.ID,
			RunId:          run.ID,
			Pipeline:       run.Pipeline,
			Service:        *service,
			AppUrl:         fmt.Sprintf("%s/%s/deploy/%s/runs/%s", apiconf.GetBaseAppUrl(), orgId, selectedDeployment.ID, run.ID),
		}
		statusDisplay := tea.NewProgram(statusModel)

		go func() {
			client := httputil.NewHyphenHTTPClient()
			url := fmt.Sprintf("%s/api/websockets/eventStream", apiconf.GetBaseWebsocketUrl())
			conn, err := client.GetWebsocketConnection(url)
			if err != nil {
				printer.Error(cmd, fmt.Errorf("failed to connect to WebSocket: %w", err))
				return
			}
			defer conn.Close()
			conn.WriteJSON(
				map[string]interface{}{
					"eventStreamTopic": "deploymentRun",
					"organizationId":   orgId,
				},
			)
		messageListener:
			for {
				_, message, err := conn.ReadMessage()
				if err != nil {
					printer.Error(cmd, fmt.Errorf("error reading WebSocket message: %w", err))
					break
				}

				var wsMessage Deployment.WebSocketMessage
				err = json.Unmarshal(message, &wsMessage)
				if err != nil {
					continue
				}

				var typeCheck map[string]interface{}
				err = json.Unmarshal(wsMessage.Data, &typeCheck)
				if err != nil {
					printer.Error(cmd, fmt.Errorf("error unmarshalling Data for type check: %w", err))
					continue
				}

				if _, ok := typeCheck["level"]; ok {
					var data Deployment.LogMessageData
					err = json.Unmarshal(wsMessage.Data, &data)
					if err != nil {
						continue
					}

				} else if _, ok := typeCheck["type"]; ok {
					var data Deployment.RunMessageData
					err = json.Unmarshal(wsMessage.Data, &data)
					if err != nil {
						continue
					}

					statusDisplay.Send(data)

					if data.Type == "run" && (data.Status == "succeeded" || data.Status == "failed" || data.Status == "canceled") {
						statusDisplay.Quit()
						break messageListener
					}
				}
			}
		}()
		statusDisplay.Run()

	},
}

Functions

func FindStepOrTaskByID

func FindStepOrTaskByID(pipeline models.DeploymentPipeline, id string) (interface{}, bool)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL