Skip to content

Commit

Permalink
Merge branch 'main' into bug/closed_pr
Browse files Browse the repository at this point in the history
  • Loading branch information
spchortis committed Aug 31, 2023
2 parents 48abcea + d0a8bce commit f76b26e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
45 changes: 24 additions & 21 deletions src/cli/onbranch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package cli

import (
"fmt"
"os"

git "github.com/go-git/go-git/v5"
"github.com/nearform/initium-cli/src/services/git"
"github.com/nearform/initium-cli/src/utils"
"github.com/urfave/cli/v2"
)

const (
cleanFlag string = "clean"
branchNameFlag string = "branch-name"
)

func (c icli) buildPushDeploy(cCtx *cli.Context) error {
err := c.Build(cCtx)
if err != nil {
Expand Down Expand Up @@ -40,53 +44,49 @@ func (c icli) OnBranchCMD() *cli.Command {
&cli.BoolFlag{
Name: stopOnBuildFlag,
Value: false,
Usage: "Stop the onbranch command after the build step",
},
&cli.BoolFlag{
Name: stopOnPushFlag,
Value: false,
Usage: "Stop the onbranch command after the push step",
},
&cli.BoolFlag{
Name: "clean",
Name: cleanFlag,
Value: false,
Usage: "Delete the knative service",
},
&cli.StringFlag{
Name: "branchName",
Name: branchNameFlag,
Usage: "Pass a branch name and disable autodetection",
},
}...)
return &cli.Command{
Name: "onbranch",
Usage: "deploy the application as a knative service",
Usage: "Build, push and deploy the application using the branch name as version and namespace",
Flags: flags,
Action: func(cCtx *cli.Context) error {
if cCtx.Bool("clean") {
if cCtx.Bool(cleanFlag) {
return c.Delete(cCtx)
}

return c.buildPushDeploy(cCtx)
},
Before: func(ctx *cli.Context) error {
var err error
if err := c.loadFlagsFromConfig(ctx); err != nil {
return err
}

wd, err := os.Getwd()

if err != nil {
return err
}

repo, err := git.PlainOpen(wd)
if err != nil {
return err
}
branchName := ctx.String(branchNameFlag)

head, err := repo.Head()
if err != nil {
return err
if branchName == "" {
branchName, err = git.GetBranchName()
if err != nil {
return err
}
}

branchName := head.Name().Short()

ctx.Set(appVersionFlag, utils.EncodeRFC1123(branchName))
ctx.Set(namespaceFlag, utils.EncodeRFC1123(branchName))

Expand All @@ -97,6 +97,9 @@ func (c icli) OnBranchCMD() *cli.Command {
if ctx.Bool(stopOnPushFlag) {
ignoredFlags = append(ignoredFlags, []string{endpointFlag, tokenFlag, caCRTFlag, namespaceFlag}...)
}
if ctx.Bool(cleanFlag) {
ignoredFlags = append(ignoredFlags, []string{registryPasswordFlag, registryUserFlag}...)
}

return c.checkRequiredFlags(ctx, ignoredFlags)
},
Expand Down
16 changes: 16 additions & 0 deletions src/services/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ func GetHash() (string, error) {
return headRef.Hash().String(), nil
}

func GetBranchName() (string, error) {
repo, err := initRepo()

if err != nil {
return "", err
}

head, err := repo.Head()
if err != nil {
return "", err
}

branchName := head.Name().Short()
return branchName, nil
}

func getGithubRemote() (string, error) {
repo, err := initRepo()

Expand Down
9 changes: 4 additions & 5 deletions src/services/k8s/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"text/template"
"time"

"github.com/charmbracelet/log"
"github.com/nearform/initium-cli/src/services/docker"
"github.com/nearform/initium-cli/src/services/project"

"github.com/nearform/initium-cli/src/utils/logger"

corev1 "k8s.io/api/core/v1"
apimachineryErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -73,7 +72,7 @@ func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*se
}

func Apply(namespace string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage) error {
logger.PrintInfo("Deploying Knative service to " + config.Host)
log.Info("Deploying Knative service", "host", config.Host, "name", project.Name, "namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

Expand Down Expand Up @@ -140,7 +139,7 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock
}

func Clean(namespace string, config *rest.Config, project *project.Project) error {
logger.PrintInfo("Deleting Knative service from " + config.Host)
log.Info("Deleting Knative service", "host", config.Host, "name", project.Name, "namespace", namespace)
ctx := context.Background()

// Create a new Knative Serving client
Expand All @@ -154,6 +153,6 @@ func Clean(namespace string, config *rest.Config, project *project.Project) erro
return fmt.Errorf("deleting the service: %v", err)
}

logger.PrintInfo("Deleted Knative service " + project.Name)
log.Info("The Knative service was successfully deleted", "host", config.Host, "name", project.Name, "namespace", namespace)
return nil
}

0 comments on commit f76b26e

Please sign in to comment.