Skip to content

Commit

Permalink
increment patch version and make UX consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Sanskar Jaiswal <[email protected]>
  • Loading branch information
aryan9600 committed Feb 6, 2024
1 parent bc9421d commit 6f1b328
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,18 @@ If you just need system applications, you could also try "setup-arkade":
## Bump Helm chart versions
To bump the minor of your Helm chart version, run `arkade chart bump --dir ./chart`. This updates the minor component of the version specified in Chart.yaml.
To bump the patch version of your Helm chart, run `arkade chart bump -f ./chart/values.yaml`. This updates the patch component of the version specified in Chart.yaml.

```bash
arkade chart bump --dir ./charts/flagger
arkade chart bump -f ./charts/flagger/values.yaml
charts/flagger/Chart.yaml 1.36.0 => 1.37.0
```

By default, the new version is written to stdout. To bump the version in the file, run the above command with the `--write` flag.
To bump the version in the chart's Chart.yaml only if the adjacent values file has any changes, specify the `--check-for-value-updates` flag:
To bump the version in the chart's Chart.yaml only if the chart has any changes, specify the `--check-for-updates` flag:

```bash
arkade chart bump --dir ./charts/flagger/ --check-for-value-updates values.yaml
arkade chart bump -f ./charts/flagger/values.yaml --check-for-updates
no changes detected in charts/flagger/values.yaml; skipping version bump
```

Expand Down
45 changes: 21 additions & 24 deletions cmd/chart/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,44 @@ const (
func MakeBump() *cobra.Command {
var command = &cobra.Command{
Use: "bump",
Short: "Bump the version of the Helm chart.",
Short: "Bump the patch version of the Helm chart.",
Long: `Bump the version present in the Chart.yaml of a Helm chart.
To bump the version only if the adjacent values file has changes then specify
the --check-for-value-updates flag. If the values file has no changes the command
To bump the version only if the chart has changes then specify the
--check-for-updates flag. If the chart has no changes the command
returns early with an exit code zero.
`,
Example: `arkade chart bump --dir ./chart
arkade chart bump --dir ./charts --check-for-value-updates values.yaml`,
Example: `arkade chart bump -f ./chart/values.yaml
arkade chart bump -f ./charts/values.yaml --check-for-updates`,
SilenceUsage: true,
}

command.Flags().StringP("dir", "d", "", "Path to the Helm chart directory or a directory containing Helm charts")
command.Flags().StringP("file", "f", "", "Path to values.yaml file")
command.Flags().BoolP("verbose", "v", false, "Verbose output")
command.Flags().BoolP("write", "w", false, "Write the updated values back to the file, or stdout when set to false")
command.Flags().String("check-for-value-updates", "", "Name of the values file to check if the chart's values have been modified before bumping version")
command.Flags().Bool("check-for-updates", false, "Check for updates to the chart before bumping its version")

command.RunE = func(cmd *cobra.Command, args []string) error {
chartDir, err := cmd.Flags().GetString("dir")
valuesFile, err := cmd.Flags().GetString("file")
if err != nil {
return fmt.Errorf("invalid value for --dir")
return fmt.Errorf("invalid value for --file")
}
if chartDir == "" {
return fmt.Errorf("flag --dir is required")
if valuesFile == "" {
return fmt.Errorf("flag --file is required")
}
verbose, _ := cmd.Flags().GetBool("verbose")
write, err := cmd.Flags().GetBool("write")
if err != nil {
return fmt.Errorf("invalid value for --write")
}
valuesFile, err := cmd.Flags().GetString("check-for-value-updates")
checkForUpdates, err := cmd.Flags().GetBool("check-for-updates")
if err != nil {
return fmt.Errorf("invalid value for --check-for-value-updates")
return fmt.Errorf("invalid value for --check-for-updates")
}

// Map with key as the path to Chart.yaml and the value as the parsed contents of Chart.yaml
chartDir := filepath.Dir(valuesFile)
chartYamlPath := filepath.Join(chartDir, ChartYamlFileName)

// Map with key as the path to Chart.yaml and the value as the parsed contents of Chart.yaml
var values helm.ValuesMap
// Try to read a Chart.yaml, but if thats unsuccessful then fall back to Chart.yml
if values, err = helm.Load(chartYamlPath); err != nil {
Expand All @@ -78,23 +80,18 @@ returns early with an exit code zero.
if !ok {
log.Printf("unable to find a valid version in %s", chartYamlPath)
}
if valuesFile != "" {
if checkForUpdates {
absPath, err := filepath.Abs(chartDir)
if err != nil {
return err
}
absValuesFile := filepath.Join(absPath, valuesFile)
_, err = os.Stat(absValuesFile)
if err != nil {
return fmt.Errorf("unable to find values file: %s", absValuesFile)
}

// Run `git diff --exit-code <file>` to check if the values file has any changes.
// Run `git diff --exit-code <file>` to check if any files in the chart dir changed.
// An exit code of 0 indicates that there are no changes, thus we skip bumping the
// version of the chart.
cmd := execute.ExecTask{
Command: "git",
Args: []string{"diff", "--exit-code", valuesFile},
Args: []string{"diff", "--exit-code", "."},
Cwd: absPath,
}
res, err := cmd.Execute(context.Background())
Expand All @@ -103,7 +100,7 @@ returns early with an exit code zero.
}

if res.ExitCode == 0 {
fmt.Printf("no changes detected in %s; skipping version bump\n", filepath.Join(chartDir, valuesFile))
fmt.Printf("no changes detected in %s; skipping version bump\n", chartDir)
os.Exit(0)
}
}
Expand All @@ -112,7 +109,7 @@ returns early with an exit code zero.
if err != nil {
return fmt.Errorf("%s", err)
}
newVer := ver.IncMinor()
newVer := ver.IncPatch()
fmt.Printf("%s %s => %s\n", chartYamlPath, ver.String(), newVer.String())
if write {
if verbose {
Expand Down

0 comments on commit 6f1b328

Please sign in to comment.