Skip to content

Commit

Permalink
add image-build-base-release command
Browse files Browse the repository at this point in the history
  • Loading branch information
tashima42 committed Oct 16, 2023
1 parent b705b0f commit 04f6c55
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 16 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: ECM Distro Tools Release
on:
push:
tags:
- "v*"
push:
tags:
- "v*"
jobs:
release:
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- name: Checkout
- name: Checkout
uses: actions/checkout@v4
- name: Build ECM Distro Tools
run: |
Expand All @@ -19,7 +19,7 @@ jobs:
uses: SierraSoftworks/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
overwrite: 'true'
overwrite: "true"
files: |
${{ github.workspace }}/bin/bootstrap_hash
${{ github.workspace }}/bin/check_for_k8s_release
Expand All @@ -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 Down
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
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 {
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

0 comments on commit 04f6c55

Please sign in to comment.