Skip to content

Commit 06e9c59

Browse files
authored
support disconnected bump of version as output (#7)
support disconnected bump of version as output
1 parent 6456666 commit 06e9c59

File tree

5 files changed

+123
-108
lines changed

5 files changed

+123
-108
lines changed

cmd/root.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
homedir "github.com/mitchellh/go-homedir"
99
"github.com/spf13/cobra"
1010
"github.com/spf13/viper"
11+
"github.com/sstarcher/helm-release/git"
1112
"github.com/sstarcher/helm-release/helm"
1213
)
1314

1415
var cfgFile string
1516
var tag string
1617
var tagPath string
1718
var printComputedVersion bool
19+
var bump string
1820

1921
// rootCmd represents the base command when called without any subcommands
2022
var rootCmd = &cobra.Command{
@@ -29,18 +31,28 @@ var rootCmd = &cobra.Command{
2931
dir = args[0]
3032
}
3133

32-
chart, err := helm.New(dir)
34+
git, err := git.New(dir)
3335
if err != nil {
3436
return err
3537
}
3638

37-
version, err := chart.Version()
39+
if printComputedVersion {
40+
version, err := git.BumpVersion(bump)
41+
if err != nil {
42+
return err
43+
}
44+
45+
_, err = os.Stdout.WriteString(*version)
46+
return err
47+
}
48+
49+
chart, err := helm.New(dir)
3850
if err != nil {
3951
return err
4052
}
4153

42-
if printComputedVersion {
43-
_, err = os.Stdout.WriteString(*version)
54+
version, err := git.NextVersion()
55+
if err != nil {
4456
return err
4557
}
4658

@@ -81,6 +93,7 @@ func init() {
8193
rootCmd.Flags().StringVarP(&tag, "tag", "t", "", "Sets the docker image tag in values.yaml")
8294
rootCmd.Flags().StringVar(&tagPath, "path", helm.DefaultTagPath, "Sets the path to the image tag to modify in values.yaml")
8395
rootCmd.Flags().BoolVar(&printComputedVersion, "print-computed-version", false, "Print the computed version string to stdout")
96+
rootCmd.Flags().StringVar(&bump, "bump", "", "Specifies to bump major, minor, or patch when using print-computed-version")
8497
}
8598

8699
// initConfig reads in config file and ENV variables if set.

git/git.go

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010
"strings"
1111

12+
"github.com/Masterminds/semver"
1213
log "github.com/sirupsen/logrus"
1314
)
1415

@@ -19,6 +20,8 @@ type Gitter interface {
1920
Sha() (sha string, err error)
2021
Branch() (string, error)
2122
IsTagged() bool
23+
NextVersion() (*string, error)
24+
BumpVersion(string) (*string, error)
2225
}
2326

2427
type git struct {
@@ -38,9 +41,8 @@ func New(directory string) (Gitter, error) {
3841

3942
// ~r4.8-40-g56a99c2~
4043
func (g *git) Tag() (tag string, err error) {
41-
tag = os.Getenv("LAST_TAG")
42-
43-
if tag != "" {
44+
tag, exists := os.LookupEnv("LAST_TAG")
45+
if exists {
4446
return
4547
}
4648

@@ -162,6 +164,101 @@ func (g *git) Branch() (string, error) {
162164
return strings.ToLower(branch), err
163165
}
164166

167+
func (g *git) gitSemVersion() (*semver.Version, error) {
168+
tag, err := g.Tag()
169+
if err != nil || tag == "" {
170+
tag = "0.0.1"
171+
log.Infof("unable to find any git tags using %s", tag)
172+
}
173+
174+
tag = strings.TrimPrefix(tag, "v")
175+
tag = strings.TrimPrefix(tag, "r")
176+
ver, err := semver.NewVersion(tag)
177+
if err != nil {
178+
return nil, fmt.Errorf("%s %s", tag, err)
179+
}
180+
return ver, nil
181+
}
182+
183+
func (g *git) BumpVersion(bump string) (*string, error) {
184+
ver, err := g.gitSemVersion()
185+
if err != nil {
186+
return nil, err
187+
}
188+
189+
version := *ver
190+
if bump == "major" {
191+
version = version.IncMajor()
192+
} else if bump == "minor" {
193+
version = version.IncMinor()
194+
} else {
195+
version = version.IncPatch()
196+
}
197+
198+
verStr := version.String()
199+
return &verStr, err
200+
}
201+
202+
// NextVersion determines the correct version
203+
func (g *git) NextVersion() (*string, error) {
204+
commits, err := g.Commits()
205+
if err != nil {
206+
return nil, err
207+
}
208+
209+
sha, err := g.Sha()
210+
if err != nil {
211+
return nil, fmt.Errorf("failed to fetch git sha %s", err)
212+
}
213+
214+
branch, err := g.Branch()
215+
if err != nil {
216+
return nil, fmt.Errorf("failed to fetch git branch %s", err)
217+
}
218+
219+
ver, err := g.gitSemVersion()
220+
if err != nil {
221+
return nil, err
222+
}
223+
224+
version := *ver
225+
prerel := ""
226+
tagged := g.IsTagged()
227+
if !tagged {
228+
if branch == "head" && commits == 0 {
229+
return nil, errors.New("this is likely an light-weight git tag. please use a annotated tag for helm release to function properly")
230+
}
231+
version = version.IncPatch()
232+
if branch != "master" {
233+
prerel = "0." + branch
234+
}
235+
}
236+
237+
if branch == "master" {
238+
if commits != 0 {
239+
if prerel != "" {
240+
prerel += "."
241+
}
242+
prerel += strconv.Itoa(commits)
243+
}
244+
}
245+
246+
if prerel != "" {
247+
version, err = version.SetPrerelease(prerel)
248+
if err != nil {
249+
return nil, err
250+
}
251+
}
252+
253+
version, err = version.SetMetadata(sha)
254+
if err != nil {
255+
return nil, err
256+
}
257+
258+
verStr := version.String()
259+
return &verStr, err
260+
}
261+
165262
func validate(dir string) error {
166263
cmd := exec.Command("git", "rev-parse", "--git-dir")
167264
cmd.Dir = dir

helm/helm.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10-
"strconv"
1110
"strings"
1211

1312
"gopkg.in/yaml.v2"
14-
15-
"github.com/Masterminds/semver"
16-
log "github.com/sirupsen/logrus"
17-
"github.com/sstarcher/helm-release/git"
1813
)
1914

2015
// DefaultTagPath is the default path to the image tag in a helm values.yaml
@@ -25,7 +20,6 @@ type Chart struct {
2520
Name string
2621
path string
2722
TagPath string
28-
git git.Gitter
2923
}
3024

3125
// New finds the helm chart in the directory and returns a Chart object
@@ -44,11 +38,6 @@ func New(dir string) (*Chart, error) {
4438
chart := charts[0]
4539
chart.TagPath = "image.tag"
4640

47-
var err error
48-
chart.git, err = git.New(chart.path)
49-
if err != nil {
50-
return nil, err
51-
}
5241
return &chart, nil
5342
}
5443

@@ -68,82 +57,6 @@ func findCharts(dir string) []Chart {
6857
return charts
6958
}
7059

71-
func (c *Chart) gitSemVersion() (*semver.Version, error) {
72-
tag, err := c.git.Tag()
73-
if err != nil {
74-
tag = "0.0.1"
75-
log.Infof("unable to find any git tags using %s", tag)
76-
}
77-
78-
tag = strings.TrimPrefix(tag, "v")
79-
tag = strings.TrimPrefix(tag, "r")
80-
ver, err := semver.NewVersion(tag)
81-
if err != nil {
82-
return nil, fmt.Errorf("%s %s", tag, err)
83-
}
84-
return ver, nil
85-
}
86-
87-
// Version determines the correct version for the chart
88-
func (c *Chart) Version() (*string, error) {
89-
commits, err := c.git.Commits()
90-
if err != nil {
91-
return nil, err
92-
}
93-
94-
sha, err := c.git.Sha()
95-
if err != nil {
96-
return nil, fmt.Errorf("failed to fetch git sha %s", err)
97-
}
98-
99-
branch, err := c.git.Branch()
100-
if err != nil {
101-
return nil, fmt.Errorf("failed to fetch git branch %s", err)
102-
}
103-
104-
ver, err := c.gitSemVersion()
105-
if err != nil {
106-
return nil, err
107-
}
108-
109-
version := *ver
110-
prerel := ""
111-
tagged := c.git.IsTagged()
112-
if !tagged {
113-
if branch == "head" && commits == 0 {
114-
return nil, errors.New("this is likely an light-weight git tag. please use a annotated tag for helm release to function properly")
115-
}
116-
version = version.IncPatch()
117-
if branch != "master" {
118-
prerel = "0." + branch
119-
}
120-
}
121-
122-
if branch == "master" {
123-
if commits != 0 {
124-
if prerel != "" {
125-
prerel += "."
126-
}
127-
prerel += strconv.Itoa(commits)
128-
}
129-
}
130-
131-
if prerel != "" {
132-
version, err = version.SetPrerelease(prerel)
133-
if err != nil {
134-
return nil, err
135-
}
136-
}
137-
138-
version, err = version.SetMetadata(sha)
139-
if err != nil {
140-
return nil, err
141-
}
142-
143-
verStr := version.String()
144-
return &verStr, err
145-
}
146-
14760
// UpdateChartVersion updates the version of the helm chart
14861
func (c *Chart) UpdateChartVersion(chartVersion string) error {
14962
var config map[interface{}]interface{}

helm/helm_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66
"testing"
77

8+
"github.com/sstarcher/helm-release/git"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -52,18 +53,6 @@ func TestUpdateImageInvalidPath(t *testing.T) {
5253
assert.NotNil(err)
5354
}
5455

55-
func TestVersion(t *testing.T) {
56-
assert := assert.New(t)
57-
58-
chart, err := New(noTags)
59-
assert.Nil(err)
60-
assert.NotNil(chart)
61-
62-
version, err := chart.Version()
63-
assert.Nil(err)
64-
assert.NotNil(version)
65-
}
66-
6756
var versionTests = []struct {
6857
branch string
6958
tag string
@@ -95,7 +84,10 @@ func TestVersions(t *testing.T) {
9584
os.Setenv("COMMITS", tt.commits)
9685
os.Setenv("IS_TAGGED", strconv.FormatBool(tt.tagged))
9786

98-
actual, err := chart.Version()
87+
git, err := git.New(".")
88+
assert.Nil(err)
89+
90+
actual, err := git.NextVersion()
9991
assert.Nil(err)
10092
if actual != nil {
10193
assert.Equal(tt.expected, *actual)

plugin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "release"
2-
version: "0.1.11"
2+
version: "0.2.1"
33
usage: "Determines the charts next release number"
44
description: |-
55
This plugin will use environment variables and git history to divine the next chart version.

0 commit comments

Comments
 (0)