Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move tag rancher to release command #347

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/rancher_release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func main() {
setChartsBranchReferencesCommand(),
checkRancherRCDepsCommand(),
labelIssuesCommand(),
tagReleaseCommand(),
}
app.Flags = rootFlags

Expand Down
75 changes: 0 additions & 75 deletions cmd/rancher_release/tag_release.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/release/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var rootConfig *config.Config
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "release",
Short: "Central command to perform RKE2 and K3s Releases",
Short: "Central command to perform RKE2, K3s and Rancher Releases",
Long: ``,
}

Expand Down
78 changes: 54 additions & 24 deletions cmd/release/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@ import (
"os"
"time"

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

type tagRKE2CmdFlags struct {
AlpineVersion *string
ReleaseVersion *string
RCVersion *string
RPMVersion *int
}

type tagRancherCmdFlags struct {
Tag *string
Branch *string
RepoOwner *string
DryRun *bool
}

var (
alpineVersion *string
releaseVersion *string
rcVersion *string
rpmVersion *int
tagRKE2Flags tagRKE2CmdFlags
tagRancherFlags tagRancherCmdFlags
)

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

Expand All @@ -55,12 +68,12 @@ var rke2TagSubCmd = &cobra.Command{

switch args[0] {
case "image-build-base":
if err := rke2.ImageBuildBaseRelease(ctx, client, *alpineVersion, *dryRun); err != nil {
if err := rke2.ImageBuildBaseRelease(ctx, client, *tagRKE2Flags.AlpineVersion, *dryRun); err != nil {
return err
}
case "image-build-kubernetes":
now := time.Now().UTC().Format("20060102")
suffix := "-rke2" + *releaseVersion + "-build" + now
suffix := "-rke2" + *tagRKE2Flags.ReleaseVersion + "-build" + now

if *dryRun {
fmt.Println("dry-run:")
Expand Down Expand Up @@ -90,9 +103,9 @@ var rke2TagSubCmd = &cobra.Command{
return errors.New("invalid rpm tag. expected {testinglatest|stable}")
}

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)
rpmTag := fmt.Sprintf("+rke2%s.%s.%d", *tagRKE2Flags.ReleaseVersion, args[1], *tagRKE2Flags.RPMVersion)
if *tagRKE2Flags.RCVersion != "" {
rpmTag = fmt.Sprintf("+rke2%s-rc%s.%s.%d", *tagRKE2Flags.ReleaseVersion, *tagRKE2Flags.RCVersion, args[1], *tagRKE2Flags.RPMVersion)
}

if *dryRun {
Expand Down Expand Up @@ -122,26 +135,43 @@ var rke2TagSubCmd = &cobra.Command{
},
}

var rancherTagSubCmd = &cobra.Command{
Use: "rancher",
Short: "",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
ghClient := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)
return rancher.TagRelease(ctx, ghClient, *tagRancherFlags.Tag, *tagRancherFlags.Branch, *tagRancherFlags.RepoOwner, *tagRancherFlags.DryRun)
},
}

func init() {
rootCmd.AddCommand(tagCmd)

tagCmd.AddCommand(k3sTagSubCmd)
tagCmd.AddCommand(rke2TagSubCmd)
tagCmd.AddCommand(rancherTagSubCmd)

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
// and all subcommands, e.g.:
// tagCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:

// rke2
tagRKE2Flags.AlpineVersion = rke2TagSubCmd.Flags().StringP("alpine-version", "a", "", "Alpine version")
tagRKE2Flags.ReleaseVersion = rke2TagSubCmd.Flags().StringP("release-version", "r", "r1", "Release version")
tagRKE2Flags.RCVersion = rke2TagSubCmd.Flags().String("rc", "", "RC version")
tagRKE2Flags.RPMVersion = rke2TagSubCmd.Flags().Int("rpm-version", 0, "RPM version")

// rancher
tagRancherFlags.Tag = rancherTagSubCmd.Flags().StringP("tag", "t", "", "tag to be created. e.g: v2.8.1-rc4")
tagRancherFlags.Branch = rancherTagSubCmd.Flags().StringP("branch", "b", "", "branch to be used as the base to create the tag. e.g: release/v2.8")
tagRancherFlags.RepoOwner = rancherTagSubCmd.Flags().StringP("repo-owner", "o", "rancher", "repository owner to create the tag in, optional")
tagRancherFlags.DryRun = rancherTagSubCmd.Flags().BoolP("dry-run", "d", false, "don't push any changes, optional (default \"false\")")
if err := rancherTagSubCmd.MarkFlagRequired("tag"); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if err := rancherTagSubCmd.MarkFlagRequired("branch"); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
52 changes: 34 additions & 18 deletions release/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,35 +334,51 @@ func SetChartBranchReferences(ctx context.Context, forkPath, rancherBaseBranch,
return nil
}

func TagRancherRelease(ctx context.Context, ghClient *github.Client, tag, remoteBranch, repoOwner string, generalAvailability, ignoreDraft, dryRun bool) error {
logrus.Info("validating tag semver format")
func TagRelease(ctx context.Context, ghClient *github.Client, tag, remoteBranch, repoOwner string, dryRun bool) error {
fmt.Println("validating tag semver format")
if !semver.IsValid(tag) {
return errors.New("the tag `" + tag + "` isn't a valid semantic versioning string")
}
logrus.Info("getting remote branch information from " + repoOwner + "/" + rancherRepo)
fmt.Println("getting remote branch information from " + repoOwner + "/" + rancherRepo)
branch, _, err := ghClient.Repositories.GetBranch(ctx, repoOwner, rancherRepo, remoteBranch, true)
if err != nil {
return err
}
logrus.Info("the latest commit on branch " + remoteBranch + " is: " + *branch.Commit.SHA)

createAsDraft := !ignoreDraft
createAsPrerelease := !generalAvailability
logrus.Info("creating release ")
ghRelease := github.RepositoryRelease{
TagName: github.String(tag),
Name: github.String(rancherReleaseName(generalAvailability, tag)),
Draft: &createAsDraft,
Prerelease: &createAsPrerelease,
GenerateReleaseNotes: github.Bool(false),
if branch.Commit.SHA == nil {
return errors.New("branch commit sha is nil")
}
fmt.Println("the latest commit on branch " + remoteBranch + " is: " + *branch.Commit.SHA)
fmt.Println("checking if CI is passing")
if err := commitStateSuccess(ctx, ghClient, repoOwner, rancherRepo, *branch.Commit.SHA); err != nil {
return err
}
logrus.Infof("github release: %+v", ghRelease)
fmt.Println("creating tag: " + tag)
if dryRun {
logrus.Info("dry run, skipping release creation")
fmt.Println("dry run, skipping tag creation")
return nil
}
_, _, err = ghClient.Repositories.CreateRelease(ctx, repoOwner, rancherRepo, &ghRelease)
return err
ref, _, err := ghClient.Git.CreateRef(ctx, repoOwner, rancherRepo, &github.Reference{
Ref: github.String("refs/tags/" + tag),
Object: &github.GitObject{
SHA: branch.Commit.SHA,
},
})
if err != nil {
return err
}
fmt.Println("created tag: " + *ref.URL)
return nil
}

func commitStateSuccess(ctx context.Context, ghClient *github.Client, owner, repo, commit string) error {
status, _, err := ghClient.Repositories.GetCombinedStatus(ctx, owner, repo, commit, &github.ListOptions{})
if err != nil {
return err
}
if status.State != github.String("success") {
return errors.New("expected commit " + commit + " to have state 'success', instead, got " + *status.State)
}
return nil
}

func rancherReleaseName(generalAvailability bool, tag string) string {
Expand Down