-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_tag.go
219 lines (189 loc) · 5.37 KB
/
step_tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package cmd
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"k8s.io/helm/pkg/chartutil"
)
const (
VERSION = "version"
defaultVersionFile = "VERSION"
ValuesYamlRepositoryPrefix = " repository:"
ValuesYamlTagPrefix = " tag:"
)
// CreateClusterOptions the flags for running create cluster
type StepTagOptions struct {
StepOptions
Flags StepTagFlags
}
type StepTagFlags struct {
Version string
VersionFile string
ChartsDir string
ChartValueRepository string
}
var (
stepTagLong = templates.LongDesc(`
This pipeline step command creates a git tag using a version number prefixed with 'v' and pushes it to a
remote origin repo.
This commands effectively runs:
git commit -a -m "release $(VERSION)" --allow-empty
git tag -fa v$(VERSION) -m "Release version $(VERSION)"
git push origin v$(VERSION)
`)
stepTagExample = templates.Examples(`
jx step tag --version 1.0.0
`)
)
func NewCmdStepTag(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepTagOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "tag",
Short: "Creates a git tag and pushes to remote repo",
Long: stepTagLong,
Example: stepTagExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Flags.Version, VERSION, "v", "", "version number for the tag [required]")
cmd.Flags().StringVarP(&options.Flags.VersionFile, "version-file", "", defaultVersionFile, "The file name used to load the version number from if no '--version' option is specified")
cmd.Flags().StringVarP(&options.Flags.ChartsDir, "charts-dir", "d", "", "the directory of the chart to update the version")
cmd.Flags().StringVarP(&options.Flags.ChartValueRepository, "charts-value-repository", "r", "", "the fully qualified image name without the version tag. e.g. 'dockerregistry/myorg/myapp'")
return cmd
}
func (o *StepTagOptions) Run() error {
if o.Flags.Version == "" {
// lets see if its defined in the VERSION file
path := o.Flags.VersionFile
if path == "" {
path = "VERSION"
}
exists, err := util.FileExists(path)
if exists && err == nil {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
o.Flags.Version = string(data)
}
}
if o.Flags.Version == "" {
return errors.New("No version flag")
}
chartsDir := o.Flags.ChartsDir
if chartsDir != "" {
err := o.updateChart(o.Flags.Version, chartsDir)
if err != nil {
return err
}
err = o.updateChartValues(o.Flags.Version, chartsDir)
if err != nil {
return err
}
}
tag := "v" + o.Flags.Version
err := o.Git().AddCommmit("", fmt.Sprintf("release %s", o.Flags.Version))
if err != nil {
return err
}
err = o.Git().CreateTag("", tag, fmt.Sprintf("release %s", o.Flags.Version))
if err != nil {
return err
}
err = o.Git().PushTag("", tag)
if err != nil {
return err
}
log.Successf("Tag %s created and pushed to remote origin", tag)
return nil
}
func (o *StepTagOptions) updateChart(version string, chartsDir string) error {
chartFile := filepath.Join(chartsDir, "Chart.yaml")
exists, err := util.FileExists(chartFile)
if err != nil {
return err
}
if !exists {
return nil
}
chart, err := chartutil.LoadChartfile(chartFile)
if err != nil {
return err
}
if chart.Version == version {
return nil
}
chart.Version = version
err = chartutil.SaveChartfile(chartFile, chart)
if err != nil {
return fmt.Errorf("Failed to save chart %s: %s", chartFile, err)
}
return nil
}
func (o *StepTagOptions) updateChartValues(version string, chartsDir string) error {
valuesFile := filepath.Join(chartsDir, "values.yaml")
exists, err := util.FileExists(valuesFile)
if err != nil {
return err
}
if !exists {
return nil
}
data, err := ioutil.ReadFile(valuesFile)
lines := strings.Split(string(data), "\n")
chartValueRepository := o.Flags.ChartValueRepository
if chartValueRepository == "" {
chartValueRepository = o.defaultChartValueRepository()
}
updated := false
for idx, line := range lines {
if chartValueRepository != "" && strings.HasPrefix(line, ValuesYamlRepositoryPrefix) {
updated = true
lines[idx] = ValuesYamlRepositoryPrefix + " " + chartValueRepository
} else if strings.HasPrefix(line, ValuesYamlTagPrefix) {
updated = true
lines[idx] = ValuesYamlTagPrefix + " " + version
}
}
if updated {
err = ioutil.WriteFile(valuesFile, []byte(strings.Join(lines, "\n")), DefaultWritePermissions)
if err != nil {
return fmt.Errorf("Failed to save chart file %s: %s", valuesFile, err)
}
}
return nil
}
func (o *StepTagOptions) defaultChartValueRepository() string {
dockerRegistry := os.Getenv("DOCKER_REGISTRY")
dockerRegistryOrg := os.Getenv("DOCKER_REGISTRY_ORG")
if dockerRegistryOrg == "" {
dockerRegistryOrg = os.Getenv("ORG")
}
appName := os.Getenv("APP_NAME")
if dockerRegistry != "" && dockerRegistryOrg != "" && appName != "" {
return dockerRegistry + "/" + dockerRegistryOrg + "/" + appName
}
return ""
}