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

#275: add image-build-base-release command #281

Merged
merged 7 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
${{ github.workspace }}/cmd/gen_release_notes/bin/*
${{ github.workspace }}/cmd/k3s_release/bin/*
${{ github.workspace }}/cmd/rancher_release/bin/*
${{ github.workspace }}/cmd/rke2_release/bin/*
${{ github.workspace }}/cmd/standup/bin/*
${{ github.workspace }}/cmd/test_coverage/bin/*
${{ github.workspace }}/cmd/upstream_go_version/bin/*
Expand All @@ -53,4 +54,4 @@ jobs:
push: true
tags: rancher/ecm-distro-tools:${{ github.ref_name }}
file: ./Dockerfile
context: .
context: .
tashima42 marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@ _testmain.go
.idea
*.iml

cmd/gen_release_report/bin
cmd/gen_release_notes/bin
cmd/backport/bin
cmd/standup/bin
cmd/k3s_release/bin
cmd/test_coverage/bin
cmd/upstream_go_version/bin
cmd/rancher_release/bin
cmd/*/bin

data/*

Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ k3s_release:
rancher_release:
cd cmd/$@ && $(MAKE)

.PHONY: rke2_release
rke2_release:
cd cmd/$@ && $(MAKE)

.PHONY: backport
backport:
cd cmd/$@ && $(MAKE)
Expand Down
2 changes: 1 addition & 1 deletion cmd/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BINARIES = gen_release_notes gen_release_report backport standup k3s_release rancher_release test_coverage upstream_go_version
BINARIES = gen_release_notes gen_release_report backport standup k3s_release rancher_release test_coverage upstream_go_version rke2_release
ARCHS = amd64 arm64
OSs = linux darwin freebsd

Expand Down
23 changes: 23 additions & 0 deletions cmd/rke2_release/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
include ../Makefile

GO = go

BINDIR := bin
BINARY := rke2_release

VERSION = v0.1.0
GIT_SHA = $(shell git rev-parse HEAD)
override LDFLAGS += -X main.gitSHA=$(GIT_SHA) -X main.version=$(VERSION) -X main.name=$(BINARY) -extldflags '-static -Wl,--fatal-warnings'
TAGS = "netgo osusergo no_stage static_build"

$(BINDIR)/$(BINARY): clean
for arch in $(ARCHS); do \
for os in $(OSs); do \
$(GO_COMPILE) ; \
done; \
done

.PHONY: clean
clean:
$(GO) clean
rm -f $(BINDIR)/$(BINARY)
28 changes: 28 additions & 0 deletions cmd/rke2_release/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# rke2_release

rke2_release is a utility that performs the bulk of the actions to complete a rke2 release.

Please reference the help menu from the binary.

## Commands

### image-build-base-release

Checks if new Golang versions are available and creates new releases in `rancher/image-build-base`

| **Flag** | **Description** | **Required** |
| ----------------------------------- | --------------- | ------------ |
| `github-token`, `g`, `GITHUB_TOKEN` | Github Token | TRUE |

**Examples**

```
export GITHUB_TOKEN={YOUR_GITHUB_TOKEN}

rke2_release image-build-base-release
```

## Contributions

- File Issue with details of the problem, feature request, etc.
- Submit a pull request and include details of what problem or feature the code is solving or implementing.
37 changes: 37 additions & 0 deletions cmd/rke2_release/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"os"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

var rootFlags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Specify release config file",
EnvVars: []string{"RELEASE_CONFIG"},
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "Debug mode",
},
}

func main() {
app := cli.NewApp()
app.Name = "rke2-release"
app.Usage = "Perform a rke2 release"
app.UseShortOptionHandling = true
app.Commands = []*cli.Command{
imageBuildBaseReleaseCommand(),
}
app.Flags = rootFlags

if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
33 changes: 33 additions & 0 deletions cmd/rke2_release/update_image_build_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"context"
"errors"

"github.com/rancher/ecm-distro-tools/release/rke2"
"github.com/urfave/cli/v2"
)

func imageBuildBaseReleaseCommand() *cli.Command {
return &cli.Command{
Name: "image-build-base-release",
Usage: "checks if new golang versions are available and creates new releases for rancher/image-build-base",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "github-token",
Aliases: []string{"g"},
EnvVars: []string{"GITHUB_TOKEN"},
Required: true,
},
},
Action: imageBuildBaseRelease,
}
}

func imageBuildBaseRelease(c *cli.Context) error {
token := c.String("github-token")
if token == "" {
return errors.New("env var GITHUB_TOKEN is required")
}
return rke2.ImageBuildBaseRelease(context.Background(), token)
}
86 changes: 86 additions & 0 deletions release/rke2/rke2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package rke2

import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"time"

"github.com/google/go-github/v39/github"
ecmHTTP "github.com/rancher/ecm-distro-tools/http"
"github.com/rancher/ecm-distro-tools/repository"
"github.com/sirupsen/logrus"
)

const (
goDevURL = "https://go.dev/dl/?mode=json"
githubApiURL = "https://api.github.com/"
imageBuildBaseRepo = "image-build-base"
)

type goVersionRecord struct {
Version string `json:"version"`
Stable bool `json:"stable"`
}

type GithubRelease struct {
Name string `json:"name"`
}

func ImageBuildBaseRelease(ctx context.Context, githubToken string) error {
tashima42 marked this conversation as resolved.
Show resolved Hide resolved
versions, err := goVersions(goDevURL)
if err != nil {
return err
}
ghClient := repository.NewGithub(ctx, githubToken)
imageBuildBaseOrg, err := repository.OrgFromRepo(imageBuildBaseRepo)
if err != nil {
return err
}
for _, version := range versions {
logrus.Info("version: " + version.Version)
if !version.Stable {
logrus.Info("version " + version.Version + " is not stable")
continue
}
v := "v" + strings.Split(version.Version, "go")[1] + "b1"
logrus.Info("stripped version: " + v)
_, _, err := ghClient.Repositories.GetReleaseByTag(ctx, imageBuildBaseOrg, imageBuildBaseRepo, v)
if err == nil {
logrus.Info("release " + v + " already exists")
continue
}
logrus.Info("release " + v + " doesn't exists, creating release")
_, _, err = ghClient.Repositories.CreateRelease(ctx, imageBuildBaseOrg, imageBuildBaseRepo, &github.RepositoryRelease{
TagName: github.String(v),
Name: github.String(v),
Prerelease: github.Bool(false),
})
if err != nil {
return err
}
logrus.Info("created release for version: " + v)
}
return nil
}

func goVersions(goDevURL string) ([]goVersionRecord, error) {
httpClient := ecmHTTP.NewClient(time.Second * 15)
res, err := httpClient.Get(goDevURL)
if err != nil {
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, errors.New("failed to get stable go versions")
}

var versions []goVersionRecord
if err := json.NewDecoder(res.Body).Decode(&versions); err != nil {
return nil, err
}
return versions, nil
}
7 changes: 4 additions & 3 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ const (

// repoToOrg associates repo to org.
var repoToOrg = map[string]string{
"rke2": "rancher",
"k3s": "k3s-io",
"rancher": "rancher",
"rke2": "rancher",
"k3s": "k3s-io",
"rancher": "rancher",
"image-build-base": "rancher",
}

// stripBackportTag returns a string with a prefix backport tag removed
Expand Down