forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_tag.go
105 lines (80 loc) · 2.08 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
package cmd
import (
"io"
"errors"
"fmt"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/log"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/spf13/cobra"
)
const (
VERSION = "version"
)
// CreateClusterOptions the flags for running crest cluster
type StepTagOptions struct {
StepOptions
Flags StepTagFlags
}
type StepTagFlags struct {
Version 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 cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := StepTagOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
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()
cmdutil.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Flags.Version, VERSION, "v", "", "version number for the tag [required]")
return cmd
}
func (o *StepTagOptions) Run() error {
if o.Flags.Version == "" {
return errors.New("No version flag")
}
tag := "v" + o.Flags.Version
err := gits.GitCmd("", "commit", "-a", "-m", fmt.Sprintf("release %s", o.Flags.Version), "--allow-empty")
if err != nil {
return err
}
err = gits.GitCmd("", "tag", "-fa", tag, "-m", fmt.Sprintf("release %s", o.Flags.Version))
if err != nil {
return err
}
err = gits.GitCmd("", "push", "origin", tag)
if err != nil {
return err
}
log.Successf("Tag %s created and pushed to remote origin", tag)
return nil
}