func deploy()

in cmd/cloudshell_open/deploy.go [27:73]


func deploy(project, name, image, region string, envs []string, options options) (string, error) {
	envVars := parseEnv(envs)

	client, err := runClient(region)
	if err != nil {
		return "", fmt.Errorf("failed to initialize Run API client: %w", err)
	}

	svc, err := getService(project, name, region)
	if err == nil {
		// existing service
		svc = patchService(svc, envVars, image, options)
		_, err = client.Namespaces.Services.ReplaceService("namespaces/"+project+"/services/"+name, svc).Do()
		if err != nil {
			if e, ok := err.(*googleapi.Error); ok {
				return "", fmt.Errorf("failed to deploy existing Service: code=%d message=%s -- %s", e.Code, e.Message, e.Body)
			}
			return "", fmt.Errorf("failed to deploy to existing Service: %w", err)
		}
	} else {
		// new service
		svc := newService(name, project, image, envVars, options)
		_, err = client.Namespaces.Services.Create("namespaces/"+project, svc).Do()
		if err != nil {
			if e, ok := err.(*googleapi.Error); ok {
				return "", fmt.Errorf("failed to deploy a new Service: code=%d message=%s -- %s", e.Code, e.Message, e.Body)
			}
			return "", fmt.Errorf("failed to deploy a new Service: %w", err)
		}
	}

	if options.AllowUnauthenticated == nil || *options.AllowUnauthenticated {
		if err := allowUnauthenticated(project, name, region); err != nil {
			return "", fmt.Errorf("failed to allow unauthenticated requests on the service: %w", err)
		}
	}

	if err := waitReady(project, name, region); err != nil {
		return "", err
	}

	out, err := getService(project, name, region)
	if err != nil {
		return "", fmt.Errorf("failed to get service after deploying: %w", err)
	}
	return out.Status.Url, nil
}