Skip to content

Commit

Permalink
feat: implement better release commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveRuble committed Nov 19, 2020
1 parent 45a6991 commit 2b0a628
Show file tree
Hide file tree
Showing 9 changed files with 662 additions and 366 deletions.
18 changes: 0 additions & 18 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,24 +698,6 @@ var appPublishChartCmd = addCommand(



var appBuildImageCmd = addCommand(
appCmd,
&cobra.Command{
Use: "build-image [app]",
Aliases: []string{"build-images"},
Args: cobra.MaximumNArgs(1),
Short: "Builds the image(s) for an app.",
Long: `If app is not provided, the current directory is used. The image(s) will be built with the "latest" tag.`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
b := MustGetBosun(cli.Parameters{NoEnvironment:true})
app := mustGetApp(b, args)
ctx := b.NewContext().WithApp(app)
err := app.BuildImages(ctx)
return err
},
})

var appPullCmd = addCommand(
appCmd,
Expand Down
37 changes: 35 additions & 2 deletions cmd/app_publish_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/naveego/bosun/pkg/bosun"
"github.com/naveego/bosun/pkg/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var appPublishImageCmd = addCommand(
Expand All @@ -26,9 +27,41 @@ as "version-release".
app := mustGetApp(b, args)

helper := bosun.NewAppImageHelper(b)
req := bosun.PublishImagesRequest{App:app}
req := bosun.PublishImagesRequest{App:app, Pattern: viper.GetString(ArgImagePattern)}

err := helper.PublishImages(req)
return err
},
})
}, func(cmd *cobra.Command) {
cmd.Flags().String(ArgImagePattern, "", "filter pattern for images to actually process")
})


var appBuildImageCmd = addCommand(
appCmd,
&cobra.Command{
Use: "build-image [app]",
Aliases: []string{"build-images"},
Args: cobra.MaximumNArgs(1),
Short: "Builds the image(s) for an app.",
Long: `If app is not provided, the current directory is used. The image(s) will be built with the "latest" tag.`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
b := MustGetBosun(cli.Parameters{NoEnvironment:true})
app := mustGetApp(b, args)
ctx := b.NewContext().WithApp(app)
req := bosun.BuildImageRequest{
Ctx: ctx,
Pattern: viper.GetString(ArgImagePattern),
}
err := app.BuildImages(req)
return err
},
}, func(cmd *cobra.Command) {
cmd.Flags().String(ArgImagePattern, "", "filter pattern for images to actually process")
})

const (
ArgImagePattern = "pattern"
)
29 changes: 4 additions & 25 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,30 +650,7 @@ only those apps will be deployed. Otherwise, all apps in the release will be dep
withFilteringFlags,
withValueSetFlags)

var releaseCommitCmd = addCommand(releaseCmd, &cobra.Command{
Use: "commit",
Short: "Merges the release branch back to master for each app in the release, and the platform repository.",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
viper.BindPFlags(cmd.Flags())

b := MustGetBosun()
ctx := b.NewContext()

p, err := b.GetCurrentPlatform()
if err != nil {
return err
}

err = p.CommitCurrentRelease(ctx)
if err != nil {
return err
}

return nil
},
}, withFilteringFlags)

var releaseUpdateCmd = addCommand(releaseCmd, &cobra.Command{
Use: "update {stable|unstable} [apps...]",
Expand Down Expand Up @@ -735,7 +712,9 @@ var releaseChangelogCmd = addCommand(releaseCmd, &cobra.Command{
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
viper.BindPFlags(cmd.Flags())

return errors.New("not implemented")
/* viper.BindPFlags(cmd.Flags())
b := MustGetBosun()
ctx := b.NewContext()
Expand All @@ -750,7 +729,7 @@ var releaseChangelogCmd = addCommand(releaseCmd, &cobra.Command{
return err
}
return nil
return nil*/
},
}, withFilteringFlags)

Expand Down
95 changes: 95 additions & 0 deletions cmd/release_commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"github.com/naveego/bosun/pkg/bosun"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var releaseCommitCmd = addCommand(releaseCmd, &cobra.Command{
Use: "commit",
Short: "Commands for merging a release branch back to develop and master.",
SilenceErrors: true,
SilenceUsage: true,
})

var releaseCommitPlanCmd = addCommand(releaseCommitCmd, &cobra.Command{
Use: "plan",
Short: "Plans the commit of the release branch back to master for each app in the release, and the platform repository.",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {

b := MustGetBosun()

p, err := b.GetCurrentPlatform()
if err != nil {
return err
}

committer, err := bosun.NewReleaseCommitter(p, b)
if err != nil {
return err
}

err = committer.Plan()

return err
},
})

var releaseCommitShowCmd = addCommand(releaseCommitCmd, &cobra.Command{
Use: "show",
Short: "Shows the plan the commit of the release branch back to master for each app in the release, and the platform repository.",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {

b := MustGetBosun()

p, err := b.GetCurrentPlatform()
if err != nil {
return err
}

committer, err := bosun.NewReleaseCommitter(p, b)
if err != nil {
return err
}

plan, err := committer.GetPlan()

if err != nil {
return err
}

return printOutput(plan)
},
})

var releaseCommitExecuteCmd = addCommand(releaseCommitCmd, &cobra.Command{
Use: "execute",
Short: "Merges the release branch back to master for each app in the release, and the platform repository.",
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
viper.BindPFlags(cmd.Flags())

b := MustGetBosun()

p, err := b.GetCurrentPlatform()
if err != nil {
return err
}

committer, err := bosun.NewReleaseCommitter(p, b)
if err != nil {
return err
}


err = committer.Execute()

return err
},
})
33 changes: 30 additions & 3 deletions pkg/bosun/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,25 @@ func (a *App) GetTaggedImageNames(versionAndRelease ...string) []string {
return taggedNames
}

func (a *App) BuildImages(ctx BosunContext) error {
type BuildImageRequest struct {
Pattern string
Ctx BosunContext
}

func (a *App) BuildImages(req BuildImageRequest) error {

ctx := req.Ctx

var err error
var re *regexp.Regexp

if req.Pattern != "" {
re, err = regexp.Compile(req.Pattern)
if err != nil {
return err
}
}


var report []string
for _, image := range a.GetImages() {
Expand All @@ -299,6 +317,13 @@ func (a *App) BuildImages(ctx BosunContext) error {
contextPath = ctx.ResolvePath(contextPath)
}

fullName := image.GetFullName()

if re != nil && !re.MatchString(fullName){
ctx.Log().Infof("Skipping image %s because it did not match pattern %q", fullName, req.Pattern)
continue
}

var buildCommand []string
if len(image.BuildCommand) > 0 {
buildCommand = image.BuildCommand
Expand All @@ -310,7 +335,7 @@ func (a *App) BuildImages(ctx BosunContext) error {
"--build-arg", fmt.Sprintf("VERSION_NUMBER=%s", a.Version),
"--build-arg", fmt.Sprintf("COMMIT=%s", a.GetCommit()),
"--build-arg", fmt.Sprintf("BUILD_NUMBER=%s", os.Getenv("BUILD_NUMBER")),
"--tag", image.GetFullName(),
"--tag", fullName,
contextPath,
}

Expand Down Expand Up @@ -505,6 +530,8 @@ func (a *App) BumpVersion(ctx BosunContext, bumpOrVersion string) error {
log := ctx.WithApp(a).Log()
wasDirty := a.Repo.LocalRepo.IsDirty()

originalVersion := a.Version

version, err := NewVersion(bumpOrVersion)
if err == nil {
a.Version = version
Expand Down Expand Up @@ -561,7 +588,7 @@ func (a *App) BumpVersion(ctx BosunContext, bumpOrVersion string) error {
if wasDirty {
log.Warn("Repo was dirty, will not commit bumped files.")
} else {
commitMsg := fmt.Sprintf("chore(version): %s bump to %s", bumpOrVersion, a.Version)
commitMsg := fmt.Sprintf("chore(version): bumped version from %s to %s", originalVersion, a.Version)
err = a.Repo.LocalRepo.Commit(commitMsg, ".")
if err != nil {
return errors.Wrap(err, "commit bumped files")
Expand Down
26 changes: 22 additions & 4 deletions pkg/bosun/app_image_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (
"github.com/naveego/bosun/pkg"
"github.com/naveego/bosun/pkg/slack"
"github.com/pkg/errors"
"regexp"
"strings"
)

func NewAppImageHelper(b *Bosun) AppImageHelper {
return AppImageHelper{Bosun:b}
return AppImageHelper{Bosun: b}
}

type AppImageHelper struct {
Bosun *Bosun
}

type PublishImagesRequest struct {
App *App
App *App
Pattern string
}

func (x AppImageHelper) PublishImages(req PublishImagesRequest) error {
Expand All @@ -27,6 +29,16 @@ func (x AppImageHelper) PublishImages(req PublishImagesRequest) error {

ctx := x.Bosun.NewContext()

var err error
var re *regexp.Regexp

if req.Pattern != "" {
re, err = regexp.Compile(req.Pattern)
if err != nil {
return err
}
}

var report []string

tags := []string{"latest", a.Version.String()}
Expand Down Expand Up @@ -57,9 +69,15 @@ func (x AppImageHelper) PublishImages(req PublishImagesRequest) error {

for _, tag := range tags {
for _, taggedName := range a.GetTaggedImageNames(tag) {

if re != nil && !re.MatchString(taggedName) {
ctx.Log().Infof("Skipping %s because it didn't match pattern %q", taggedName, req.Pattern)
continue
}

ctx.Log().Infof("Tagging and pushing %q", taggedName)
untaggedName := strings.Split(taggedName, ":")[0]
_, err := pkg.NewShellExe("docker", "tag", untaggedName, taggedName).Sudo(ctx.GetParameters().Sudo).RunOutLog()
_, err = pkg.NewShellExe("docker", "tag", untaggedName, taggedName).Sudo(ctx.GetParameters().Sudo).RunOutLog()
if err != nil {
return err
}
Expand All @@ -80,7 +98,7 @@ func (x AppImageHelper) PublishImages(req PublishImagesRequest) error {
changes, _ := g.ExecLines("log", "--pretty=oneline", "-n", "5", "--no-color")

slack.Notification{
IconEmoji:":frame_with_picture:",
IconEmoji: ":frame_with_picture:",
}.WithMessage(`Pushed images for %s from branch %s:
%s
Expand Down
Loading

0 comments on commit 2b0a628

Please sign in to comment.