Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload .env as part of (re)deploys #6443

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions cli/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func DeployCmd(ch *cmdutil.Helper) *cobra.Command {
}
}

if len(args) > 0 {
opts.GitPath = args[0]
}

if !upload && !github {
confirmed, err := cmdutil.ConfirmPrompt("Enable automatic deploys to Rill Cloud from GitHub?", "", false)
if err != nil {
Expand Down
25 changes: 22 additions & 3 deletions cli/cmd/project/connect_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ func GitPushCmd(ch *cmdutil.Helper) *cobra.Command {
opts := &DeployOpts{}

deployCmd := &cobra.Command{
Use: "connect-github",
Use: "connect-github [<path>]",
Short: "Deploy project to Rill Cloud by pulling project files from a git repository",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
opts.GitPath = args[0]
}
return ConnectGithubFlow(cmd.Context(), ch, opts)
},
}
Expand Down Expand Up @@ -264,9 +267,25 @@ func ConnectGithubFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployOpts
ch.PrintfSuccess("Created project \"%s/%s\". Use `rill project rename` to change name if required.\n\n", ch.Org, res.Project.Name)
ch.PrintfSuccess("Rill projects deploy continuously when you push changes to Github.\n")

// If the Git path is local, we can parse the project and check if credentials are available for the connectors used by the project.
// Upload .env
if isLocalGitPath {
variablesFlow(ctx, ch, localProjectPath, opts.SubPath, opts.Name)
vars, err := local.ParseDotenv(ctx, localProjectPath)
if err != nil {
ch.PrintfWarn("Failed to parse .env: %v\n", err)
} else {
c, err := ch.Client()
if err != nil {
return err
}
_, err = c.UpdateProjectVariables(ctx, &adminv1.UpdateProjectVariablesRequest{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can skip this if there are no vars?

Organization: ch.Org,
Project: opts.Name,
Variables: vars,
})
if err != nil {
ch.PrintfWarn("Failed to upload .env: %v\n", err)
}
}
}

// Open browser
Expand Down
98 changes: 36 additions & 62 deletions cli/cmd/project/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"path/filepath"
"regexp"
"slices"
"strings"
"time"

Expand All @@ -19,7 +18,6 @@ import (
"github.com/rilldata/rill/cli/pkg/local"
"github.com/rilldata/rill/cli/pkg/printer"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/rilldata/rill/runtime/compilers/rillv1"
"github.com/rilldata/rill/runtime/pkg/activity"
"github.com/rilldata/rill/runtime/pkg/fileutil"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,9 +47,12 @@ func DeployCmd(ch *cmdutil.Helper) *cobra.Command {
opts := &DeployOpts{}

deployCmd := &cobra.Command{
Use: "deploy",
Use: "deploy [<path>]",
Short: "Deploy project to Rill Cloud by uploading the project files",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
opts.GitPath = args[0]
}
return DeployWithUploadFlow(cmd.Context(), ch, opts)
},
}
Expand Down Expand Up @@ -175,6 +176,7 @@ func DeployWithUploadFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployO
return err
}
printer.ColorGreenBold.Printf("All files uploaded successfully.\n\n")

// Update the project
// Silently ignores other flags like description etc which are handled with project update.
res, err := adminClient.UpdateProject(ctx, &adminv1.UpdateProjectRequest{
Expand All @@ -190,6 +192,23 @@ func DeployWithUploadFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployO
return fmt.Errorf("update project failed with error %w", err)
}
ch.Telemetry(ctx).RecordBehavioralLegacy(activity.BehavioralEventDeploySuccess)

// Fetch vars from .env
vars, err := local.ParseDotenv(ctx, localProjectPath)
if err != nil {
ch.PrintfWarn("Failed to parse .env: %v\n", err)
} else {
_, err = adminClient.UpdateProjectVariables(ctx, &adminv1.UpdateProjectVariablesRequest{
Organization: ch.Org,
Project: opts.Name,
Variables: vars,
})
if err != nil {
ch.PrintfWarn("Failed to upload .env: %v\n", err)
}
}

// Success
ch.PrintfSuccess("Updated project \"%s/%s\".\n\n", ch.Org, res.Project.Name)
return nil
}
Expand Down Expand Up @@ -233,8 +252,20 @@ func DeployWithUploadFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployO
// Success!
ch.PrintfSuccess("Created project \"%s/%s\". Use `rill project rename` to change name if required.\n\n", ch.Org, res.Project.Name)

// we parse the project and check if credentials are available for the connectors used by the project.
variablesFlow(ctx, ch, localProjectPath, opts.SubPath, opts.Name)
// Upload .env
vars, err := local.ParseDotenv(ctx, localProjectPath)
if err != nil {
ch.PrintfWarn("Failed to parse .env: %v\n", err)
} else {
_, err = adminClient.UpdateProjectVariables(ctx, &adminv1.UpdateProjectVariablesRequest{
Organization: ch.Org,
Project: opts.Name,
Variables: vars,
})
if err != nil {
ch.PrintfWarn("Failed to upload .env: %v\n", err)
}
}

// Open browser
if res.Project.FrontendUrl != "" {
Expand All @@ -249,63 +280,6 @@ func DeployWithUploadFlow(ctx context.Context, ch *cmdutil.Helper, opts *DeployO
return nil
}

func variablesFlow(ctx context.Context, ch *cmdutil.Helper, gitPath, subPath, projectName string) {
// Parse the project's connectors
repo, instanceID, err := cmdutil.RepoForProjectPath(gitPath)
if err != nil {
return
}
parser, err := rillv1.Parse(ctx, repo, instanceID, "prod", "duckdb")
if err != nil {
return
}
connectors := parser.AnalyzeConnectors(ctx)
for _, c := range connectors {
if c.Err != nil {
return
}
}

// Remove the default DuckDB connector we always add
for i, c := range connectors {
if c.Name == "duckdb" {
connectors = slices.Delete(connectors, i, i+1)
break
}
}

// Exit early if all connectors can be used anonymously
foundNotAnonymous := false
for _, c := range connectors {
if !c.AnonymousAccess {
foundNotAnonymous = true
}
}
if !foundNotAnonymous {
return
}

ch.PrintfWarn("\nCould not access all connectors. Rill requires credentials for the following connectors:\n\n")
for _, c := range connectors {
if c.AnonymousAccess {
continue
}
fmt.Printf(" - %s", c.Name)
if len(c.Resources) == 1 {
fmt.Printf(" (used by %s)", c.Resources[0].Name.Name)
} else if len(c.Resources) > 1 {
fmt.Printf(" (used by %s and others)", c.Resources[0].Name.Name)
}
fmt.Print("\n")
}
if subPath == "" {
ch.PrintfWarn("\nRun `rill env configure --project %s` to provide credentials.\n\n", projectName)
} else {
ch.PrintfWarn("\nRun `rill env configure --project %s` from directory `%s` to provide credentials.\n\n", projectName, gitPath)
}
time.Sleep(2 * time.Second)
}

func createOrgFlow(ctx context.Context, ch *cmdutil.Helper, defaultName string) error {
c, err := ch.Client()
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions cli/pkg/local/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package local

import (
"context"
"fmt"

"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/runtime/compilers/rillv1"
)

func ParseDotenv(ctx context.Context, projectPath string) (map[string]string, error) {
repo, instanceID, err := cmdutil.RepoForProjectPath(projectPath)
if err != nil {
return nil, err
}
parser, err := rillv1.Parse(ctx, repo, instanceID, "prod", "duckdb")
if err != nil {
return nil, err
}
if parser.RillYAML == nil {
return nil, fmt.Errorf("not a valid Rill project (missing a rill.yaml file)")
}
return parser.DotEnv, nil
}
28 changes: 17 additions & 11 deletions cli/pkg/local/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
localv1 "github.com/rilldata/rill/proto/gen/rill/local/v1"
"github.com/rilldata/rill/proto/gen/rill/local/v1/localv1connect"
"github.com/rilldata/rill/runtime/compilers/rillv1"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -393,21 +392,14 @@ func (s *Server) DeployProject(ctx context.Context, r *connect.Request[localv1.D
}

// Parse .env and push it as variables
repo, instanceID, err := cmdutil.RepoForProjectPath(s.app.ProjectPath)
dotenv, err := ParseDotenv(ctx, s.app.ProjectPath)
if err != nil {
return nil, err
}
parser, err := rillv1.Parse(ctx, repo, instanceID, "prod", "duckdb")
if err != nil {
return nil, fmt.Errorf("failed to parse project: %w", err)
}
if parser.RillYAML == nil {
return nil, fmt.Errorf("not a valid Rill project (missing a rill.yaml file)")
return nil, fmt.Errorf("failed to parse .env: %w", err)
}
_, err = c.UpdateProjectVariables(ctx, &adminv1.UpdateProjectVariablesRequest{
Organization: r.Msg.Org,
Project: r.Msg.ProjectName,
Variables: parser.DotEnv,
Variables: dotenv,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -460,6 +452,20 @@ func (s *Server) RedeployProject(ctx context.Context, r *connect.Request[localv1
}
}

// Parse .env and push it as variables
dotenv, err := ParseDotenv(ctx, s.app.ProjectPath)
if err != nil {
return nil, fmt.Errorf("failed to parse .env: %w", err)
}
_, err = c.UpdateProjectVariables(ctx, &adminv1.UpdateProjectVariablesRequest{
Organization: projResp.Project.OrgName,
Project: projResp.Project.Name,
Variables: dotenv,
})
if err != nil {
return nil, err
}

// TODO : Add other update project fields
return connect.NewResponse(&localv1.RedeployProjectResponse{
FrontendUrl: projResp.Project.FrontendUrl,
Expand Down
Loading