CI: Refactor versions.go (#55238)

* Refactor versions.go

* s/ReleaseModeConfig/BuildConfig
This commit is contained in:
Dimitris Sotirakis 2022-09-16 13:26:33 +03:00 committed by GitHub
parent e4741ce8d6
commit 95512c19e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 82 additions and 41 deletions

View File

@ -26,7 +26,7 @@ func BuildBackend(ctx *cli.Context) error {
} }
) )
mode, err := config.GetVersion(metadata.ReleaseMode.Mode) buildConfig, err := config.GetBuildConfig(metadata.ReleaseMode.Mode)
if err != nil { if err != nil {
return fmt.Errorf("could not get version / package info for mode '%s': %w", metadata.ReleaseMode.Mode, err) return fmt.Errorf("could not get version / package info for mode '%s': %w", metadata.ReleaseMode.Mode, err)
} }
@ -34,7 +34,7 @@ func BuildBackend(ctx *cli.Context) error {
const grafanaDir = "." const grafanaDir = "."
log.Printf("Building Grafana back-end, version %q, %s edition, variants [%v]", log.Printf("Building Grafana back-end, version %q, %s edition, variants [%v]",
version, edition, mode.Variants) version, edition, buildConfig.Variants)
p := syncutil.NewWorkerPool(cfg.NumWorkers) p := syncutil.NewWorkerPool(cfg.NumWorkers)
defer p.Close() defer p.Close()
@ -44,7 +44,7 @@ func BuildBackend(ctx *cli.Context) error {
} }
g, _ := errutil.GroupWithContext(ctx.Context) g, _ := errutil.GroupWithContext(ctx.Context)
for _, variant := range mode.Variants { for _, variant := range buildConfig.Variants {
variant := variant variant := variant
opts := grafana.BuildVariantOpts{ opts := grafana.BuildVariantOpts{

View File

@ -20,12 +20,12 @@ func BuildDocker(c *cli.Context) error {
} }
useUbuntu := c.Bool("ubuntu") useUbuntu := c.Bool("ubuntu")
verMode, err := config.GetVersion(metadata.ReleaseMode.Mode) buildConfig, err := config.GetBuildConfig(metadata.ReleaseMode.Mode)
if err != nil { if err != nil {
return err return err
} }
shouldSave := verMode.Docker.ShouldSave shouldSave := buildConfig.Docker.ShouldSave
if shouldSave { if shouldSave {
if err := gcloud.ActivateServiceAccount(); err != nil { if err := gcloud.ActivateServiceAccount(); err != nil {
return err return err
@ -39,7 +39,7 @@ func BuildDocker(c *cli.Context) error {
log.Printf("Building Docker images, version %s, %s edition, Ubuntu based: %v...", version, edition, log.Printf("Building Docker images, version %s, %s edition, Ubuntu based: %v...", version, edition,
useUbuntu) useUbuntu)
for _, arch := range verMode.Docker.Architectures { for _, arch := range buildConfig.Docker.Architectures {
if _, err := docker.BuildImage(version, arch, ".", useUbuntu, shouldSave, edition); err != nil { if _, err := docker.BuildImage(version, arch, ".", useUbuntu, shouldSave, edition); err != nil {
return cli.Exit(err.Error(), 1) return cli.Exit(err.Error(), 1)
} }

View File

@ -21,7 +21,7 @@ func BuildInternalPlugins(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
verMode, err := config.GetVersion(metadata.ReleaseMode.Mode) buildConfig, err := config.GetBuildConfig(metadata.ReleaseMode.Mode)
if err != nil { if err != nil {
return err return err
} }
@ -35,7 +35,7 @@ func BuildInternalPlugins(c *cli.Context) error {
var g *errutil.Group var g *errutil.Group
g, ctx = errutil.GroupWithContext(ctx) g, ctx = errutil.GroupWithContext(ctx)
if err := plugins.Build(ctx, grafanaDir, p, g, verMode); err != nil { if err := plugins.Build(ctx, grafanaDir, p, g, buildConfig); err != nil {
return cli.Exit(err.Error(), 1) return cli.Exit(err.Error(), 1)
} }
if err := g.Wait(); err != nil { if err := g.Wait(); err != nil {

View File

@ -18,7 +18,7 @@ func StoreStorybook(c *cli.Context) error {
return err return err
} }
verMode, err := config.GetVersion(metadata.ReleaseMode.Mode) buildConfig, err := config.GetBuildConfig(metadata.ReleaseMode.Mode)
if err != nil { if err != nil {
return err return err
} }
@ -26,8 +26,8 @@ func StoreStorybook(c *cli.Context) error {
return err return err
} }
storybookBucket := verMode.StorybookBucket storybookBucket := buildConfig.Buckets.Storybook
srcPath := verMode.StorybookSrcDir srcPath := buildConfig.Buckets.StorybookSrcDir
srcPath = filepath.Join(srcPath, deployment) srcPath = filepath.Join(srcPath, deployment)
gcs, err := storage.New() gcs, err := storage.New()

View File

@ -40,3 +40,11 @@ type LibC string
const ( const (
LibCMusl = "musl" LibCMusl = "musl"
) )
// Distribution is the base os image where the Grafana image is built on.
type Distribution string
const (
Ubuntu Distribution = "ubuntu"
Alpine Distribution = "alpine"
)

View File

@ -29,33 +29,38 @@ type PluginSignature struct {
type Docker struct { type Docker struct {
ShouldSave bool `json:"shouldSave,omitempty"` ShouldSave bool `json:"shouldSave,omitempty"`
Distribution []Distribution `json:"distribution,omitempty"`
Architectures []Architecture `json:"archs,omitempty"` Architectures []Architecture `json:"archs,omitempty"`
} }
// Version represents the "version.json" that defines all of the different variables used to build Grafana type Buckets struct {
type Version struct { Artifacts string `json:"artifacts,omitempty"`
Variants []Variant `json:"variants,omitempty"` ArtifactsEnterprise2 string `json:"artifactsEnterprise2,omitempty"`
PluginSignature PluginSignature `json:"pluginSignature,omitempty"` CDNAssets string `json:"CDNAssets,omitempty"`
Docker Docker `json:"docker,omitempty"` CDNAssetsDir string `json:"CDNAssetsDir,omitempty"`
PackagesBucket string `json:"packagesBucket,omitempty"` Storybook string `json:"storybook,omitempty"`
PackagesBucketEnterprise2 string `json:"packagesBucketEnterprise2,omitempty"` StorybookSrcDir string `json:"storybookSrcDir,omitempty"`
CDNAssetsBucket string `json:"CDNAssetsBucket,omitempty"` }
CDNAssetsDir string `json:"CDNAssetsDir,omitempty"`
StorybookBucket string `json:"storybookBucket,omitempty"` // BuildConfig represents the struct that defines all of the different variables used to build Grafana
StorybookSrcDir string `json:"storybookSrcDir,omitempty"` type BuildConfig struct {
Variants []Variant `json:"variants,omitempty"`
PluginSignature PluginSignature `json:"pluginSignature,omitempty"`
Docker Docker `json:"docker,omitempty"`
Buckets Buckets `json:"buckets,omitempty"`
} }
func (md *Metadata) GetReleaseMode() (ReleaseMode, error) { func (md *Metadata) GetReleaseMode() (ReleaseMode, error) {
return md.ReleaseMode, nil return md.ReleaseMode, nil
} }
// Versions is a map of versions. Each key of the Versions map is an event that uses the the config as the value for that key. // VersionMap is a map of versions. Each key of the Versions map is an event that uses the the config as the value for that key.
// For example, the 'pull_request' key will have data in it that might cause Grafana to be built differently in a pull request, // For example, the 'pull_request' key will have data in it that might cause Grafana to be built differently in a pull request,
// than the way it will be built in 'main' // than the way it will be built in 'main'
type VersionMap map[VersionMode]Version type VersionMap map[VersionMode]BuildConfig
// GetVersions reads the embedded config.json and decodes it. // GetBuildConfig reads the embedded config.json and decodes it.
func GetVersion(mode VersionMode) (*Version, error) { func GetBuildConfig(mode VersionMode) (*BuildConfig, error) {
if v, ok := Versions[mode]; ok { if v, ok := Versions[mode]; ok {
return &v, nil return &v, nil
} }

View File

@ -17,6 +17,9 @@ var Versions = VersionMap{
Architectures: []Architecture{ Architectures: []Architecture{
ArchAMD64, ArchAMD64,
}, },
Distribution: []Distribution{
Alpine,
},
}, },
}, },
MainMode: { MainMode: {
@ -42,10 +45,16 @@ var Versions = VersionMap{
ArchARM64, ArchARM64,
ArchARMv7, // GOARCH=ARM is used for both armv6 and armv7. They are differentiated by the GOARM variable. ArchARMv7, // GOARCH=ARM is used for both armv6 and armv7. They are differentiated by the GOARM variable.
}, },
Distribution: []Distribution{
Alpine,
Ubuntu,
},
},
Buckets: Buckets{
Artifacts: "grafana-downloads",
ArtifactsEnterprise2: "grafana-downloads-enterprise2",
CDNAssets: "grafana-static-assets",
}, },
PackagesBucket: "grafana-downloads",
PackagesBucketEnterprise2: "grafana-downloads-enterprise2",
CDNAssetsBucket: "grafana-static-assets",
}, },
CustomMode: { CustomMode: {
Variants: []Variant{ Variants: []Variant{
@ -70,10 +79,16 @@ var Versions = VersionMap{
ArchARM64, ArchARM64,
ArchARMv7, // GOARCH=ARM is used for both armv6 and armv7. They are differentiated by the GOARM variable. ArchARMv7, // GOARCH=ARM is used for both armv6 and armv7. They are differentiated by the GOARM variable.
}, },
Distribution: []Distribution{
Alpine,
Ubuntu,
},
},
Buckets: Buckets{
Artifacts: "grafana-downloads",
ArtifactsEnterprise2: "grafana-downloads-enterprise2",
CDNAssets: "grafana-static-assets",
}, },
PackagesBucket: "grafana-downloads",
PackagesBucketEnterprise2: "grafana-downloads-enterprise2",
CDNAssetsBucket: "grafana-static-assets",
}, },
ReleaseBranchMode: { ReleaseBranchMode: {
Variants: []Variant{ Variants: []Variant{
@ -98,10 +113,16 @@ var Versions = VersionMap{
ArchARM64, ArchARM64,
ArchARMv7, ArchARMv7,
}, },
Distribution: []Distribution{
Alpine,
Ubuntu,
},
},
Buckets: Buckets{
Artifacts: "grafana-downloads",
ArtifactsEnterprise2: "grafana-downloads-enterprise2",
CDNAssets: "grafana-static-assets",
}, },
PackagesBucket: "grafana-downloads",
PackagesBucketEnterprise2: "grafana-downloads-enterprise2",
CDNAssetsBucket: "grafana-static-assets",
}, },
TagMode: { TagMode: {
Variants: []Variant{ Variants: []Variant{
@ -126,11 +147,18 @@ var Versions = VersionMap{
ArchARM64, ArchARM64,
ArchARMv7, ArchARMv7,
}, },
Distribution: []Distribution{
Alpine,
Ubuntu,
},
},
Buckets: Buckets{
Artifacts: "grafana-prerelease/artifacts/downloads",
ArtifactsEnterprise2: "grafana-prerelease/artifacts/downloads-enterprise2",
CDNAssets: "grafana-prerelease",
CDNAssetsDir: "artifacts/static-assets",
Storybook: "grafana-prerelease",
StorybookSrcDir: "artifacts/storybook",
}, },
PackagesBucket: "grafana-prerelease/artifacts/downloads",
CDNAssetsBucket: "grafana-prerelease",
CDNAssetsDir: "artifacts/static-assets",
StorybookBucket: "grafana-prerelease",
StorybookSrcDir: "artifacts/storybook",
}, },
} }

View File

@ -18,7 +18,7 @@ type PluginSigningMode = int
// BuildPlugins builds internal plugins. // BuildPlugins builds internal plugins.
// The built plugins are placed in plugins-bundled/dist/. // The built plugins are placed in plugins-bundled/dist/.
func Build(ctx context.Context, grafanaDir string, p syncutil.WorkerPool, g *errutil.Group, verMode *config.Version) error { func Build(ctx context.Context, grafanaDir string, p syncutil.WorkerPool, g *errutil.Group, verMode *config.BuildConfig) error {
log.Printf("Building plugins in %q...", grafanaDir) log.Printf("Building plugins in %q...", grafanaDir)
root := filepath.Join(grafanaDir, "plugins-bundled", "internal") root := filepath.Join(grafanaDir, "plugins-bundled", "internal")