Skip to content

Commit

Permalink
dry run and tagging functionality (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
briandowns authored Jan 16, 2024
1 parent 2068a71 commit 95a04ef
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 15 deletions.
14 changes: 8 additions & 6 deletions cmd/release/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,37 @@ var k3sGenerateSubCmd = &cobra.Command{
Use: "k3s",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
client := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)

notes, err := release.GenReleaseNotes(ctx, "k3s-io", "k3s", *milestone, *prevMilestone, client)
if err != nil {
fmt.Println(err)
os.Exit(1)
return err
}

fmt.Print(notes.String())

return nil
},
}

var rke2GenerateSubCmd = &cobra.Command{
Use: "rke2",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
client := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)

notes, err := release.GenReleaseNotes(ctx, "rancher", "rke2", *milestone, *prevMilestone, client)
if err != nil {
fmt.Println(err)
os.Exit(1)
return err
}

fmt.Print(notes.String())

return nil
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/release/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
)

var dryRun *bool
var rootConfig *config.Config

// rootCmd represents the base command when called without any subcommands
Expand All @@ -21,8 +22,8 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
if err := rootCmd.Execute(); err != nil {
fmt.Println("error: " + err.Error())
os.Exit(1)
}
}
Expand Down
87 changes: 80 additions & 7 deletions cmd/release/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"
"time"

"github.com/rancher/ecm-distro-tools/release/rke2"
"github.com/rancher/ecm-distro-tools/repository"
"github.com/spf13/cobra"
)

var (
alpineVersion *string
alpineVersion *string
releaseVersion *string
rcVersion *string
rpmVersion *int
)

// tagCmd represents the tag command.
Expand Down Expand Up @@ -40,19 +45,80 @@ var rke2TagSubCmd = &cobra.Command{
Use: "rke2",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(*releaseVersion) != 2 {
return errors.New("invalid release version")
}

ctx := context.Background()
client := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)

switch args[0] {
case "image-build-base":
if err := rke2.ImageBuildBaseRelease(ctx, client, *alpineVersion, *dryRun); err != nil {
return err
}
case "image-build-kubernetes":
if err := rke2.ImageBuildBaseRelease(ctx, client, *alpineVersion, false); err != nil {
fmt.Println(err)
os.Exit(1)
now := time.Now().UTC().Format("20060201")
suffix := "+rke2" + *releaseVersion + "-build" + now

if *dryRun {
fmt.Println("dry-run:")
for _, version := range rootConfig.RKE2.Versions {
fmt.Println("\t" + version + suffix)
}
} else {
for _, version := range rootConfig.RKE2.Versions {
cro := repository.CreateReleaseOpts{
Owner: "rancher",
Repo: "image-build-kubernetes",
Branch: "master",
Name: version + suffix,
Prerelease: false,
}
if _, err := repository.CreateRelease(ctx, client, &cro); err != nil {
return err
}

fmt.Println("tag " + version + suffix + " created successfully")
}
}
case "rke2":
//
case "rpm":
if len(args) == 1 {
return errors.New("invalid rpm tag. expected {testinglatest|stable}")
}

fmt.Println("Successfully tagged")
rpmTag := fmt.Sprintf("+rke2%s.%s.%d", *releaseVersion, args[1], *rpmVersion)
if *rcVersion != "" {
rpmTag = fmt.Sprintf("+rke2%s-rc%s.%s.%d", *releaseVersion, *rcVersion, args[1], *rpmVersion)
}

if *dryRun {
fmt.Print("(dry-run)\n\nTagging github.com/rancher/rke2-packaging:\n\n")
for _, version := range rootConfig.RKE2.Versions {
fmt.Println("\t" + version + rpmTag)
}
} else {
for _, version := range rootConfig.RKE2.Versions {
cro := repository.CreateReleaseOpts{
Owner: "rancher",
Repo: "rke2-packaging",
Branch: "master",
Name: version + rpmTag,
Prerelease: false,
}
if _, err := repository.CreateRelease(ctx, client, &cro); err != nil {
return err
}
}
}
default:
return errors.New("unrecognized resource")
}

return nil
},
}

Expand All @@ -62,6 +128,13 @@ func init() {
tagCmd.AddCommand(k3sTagSubCmd)
tagCmd.AddCommand(rke2TagSubCmd)

dryRun = tagCmd.PersistentFlags().BoolP("dry-run", "d", false, "Dry run")

alpineVersion = rke2TagSubCmd.Flags().StringP("alpine-version", "a", "", "Alpine version")
releaseVersion = rke2TagSubCmd.Flags().StringP("release-version", "r", "r1", "Release version")
rcVersion = rke2TagSubCmd.Flags().String("rc", "", "RC version")
rpmVersion = rke2TagSubCmd.Flags().Int("rpm-version", 0, "RPM version")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand All @@ -70,5 +143,5 @@ func init() {

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
tagCmd.Flags().StringP("alpine-version", "a", "", "Alpine version")

}

0 comments on commit 95a04ef

Please sign in to comment.