diff --git a/.drone.yml b/.drone.yml index 3a24127df3b..485055b7e5b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -308,7 +308,7 @@ steps: image: grafana/build-container:1.5.9 name: build-frontend - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} + - ./bin/build build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} depends_on: - yarn-install environment: @@ -643,7 +643,7 @@ steps: image: grafana/build-container:1.5.9 name: lint-docs - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} + - ./bin/build build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} depends_on: - yarn-install environment: @@ -727,7 +727,7 @@ steps: image: grafana/build-container:1.5.9 name: lint-docs - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} + - ./bin/build build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} depends_on: - yarn-install environment: @@ -1049,7 +1049,7 @@ steps: image: grafana/build-container:1.5.9 name: build-frontend - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} + - ./bin/build build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} depends_on: - yarn-install environment: @@ -1743,7 +1743,7 @@ steps: image: grafana/build-container:1.5.9 name: build-frontend - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition oss ${DRONE_TAG} + - ./bin/build build-frontend-packages --jobs 8 --edition oss ${DRONE_TAG} depends_on: - yarn-install environment: @@ -2367,7 +2367,7 @@ steps: image: grafana/build-container:1.5.9 name: build-frontend - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition enterprise ${DRONE_TAG} + - ./bin/build build-frontend-packages --jobs 8 --edition enterprise ${DRONE_TAG} depends_on: - yarn-install environment: @@ -3712,7 +3712,7 @@ steps: image: grafana/build-container:1.5.9 name: build-frontend - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} + - ./bin/build build-frontend-packages --jobs 8 --edition oss --build-id ${DRONE_BUILD_NUMBER} depends_on: - yarn-install environment: @@ -4282,8 +4282,7 @@ steps: image: grafana/build-container:1.5.9 name: build-frontend - commands: - - ./bin/grabpl build-frontend-packages --jobs 8 --edition enterprise --build-id - ${DRONE_BUILD_NUMBER} + - ./bin/build build-frontend-packages --jobs 8 --edition enterprise --build-id ${DRONE_BUILD_NUMBER} depends_on: - yarn-install environment: @@ -5093,6 +5092,6 @@ kind: secret name: packages_secret_access_key --- kind: signature -hmac: fb770c3cc5bd204e3aceae01d35997818ee210efb54c8ccbe37a410773f1ce73 +hmac: 2689da88c9d55f8949f4f9a26a850661fdec1ecf8f5da5c3713bde4ca481b8dd ... diff --git a/pkg/build/cmd/buildfrontendpackages.go b/pkg/build/cmd/buildfrontendpackages.go new file mode 100644 index 00000000000..eb329428e0a --- /dev/null +++ b/pkg/build/cmd/buildfrontendpackages.go @@ -0,0 +1,38 @@ +package main + +import ( + "log" + "strings" + + "github.com/grafana/grafana/pkg/build/errutil" + "github.com/grafana/grafana/pkg/build/frontend" + "github.com/grafana/grafana/pkg/build/syncutil" + "github.com/urfave/cli/v2" +) + +func BuildFrontendPackages(c *cli.Context) error { + version := "" + if c.NArg() == 1 { + version = strings.TrimPrefix(c.Args().Get(0), "v") + } + + cfg, mode, err := frontend.GetConfig(c, version) + if err != nil { + return err + } + + p := syncutil.NewWorkerPool(cfg.NumWorkers) + defer p.Close() + + g, _ := errutil.GroupWithContext(c.Context) + if err := frontend.BuildFrontendPackages(cfg.PackageVersion, mode, frontend.GrafanaDir, p, g); err != nil { + return cli.Exit(err.Error(), 1) + } + if err := g.Wait(); err != nil { + return cli.Exit(err.Error(), 1) + } + + log.Println("Successfully built Grafana front-end packages!") + + return nil +} diff --git a/pkg/build/cmd/main.go b/pkg/build/cmd/main.go index 9beb0b8d3ed..c2beba847d5 100644 --- a/pkg/build/cmd/main.go +++ b/pkg/build/cmd/main.go @@ -25,6 +25,18 @@ func main() { &buildIDFlag, }, }, + { + Name: "build-frontend-packages", + Usage: "Build front-end packages", + ArgsUsage: "[version]", + Action: BuildFrontendPackages, + Flags: []cli.Flag{ + &jobsFlag, + &editionFlag, + &buildIDFlag, + &noInstallDepsFlag, + }, + }, { Name: "e2e-tests", Usage: "Run Grafana e2e tests", diff --git a/pkg/build/config/version.go b/pkg/build/config/version.go index 6c01c2a1d3e..bf49087af58 100644 --- a/pkg/build/config/version.go +++ b/pkg/build/config/version.go @@ -100,15 +100,6 @@ func GetVersion(mode VersionMode) (*Version, error) { return nil, fmt.Errorf("mode '%s' not found in version list", mode) } -func shortenBuildID(buildID string) string { - buildID = strings.ReplaceAll(buildID, "-", "") - if len(buildID) < 9 { - return buildID - } - - return buildID[0:8] -} - // GetGrafanaVersion gets the Grafana version from the package.json func GetGrafanaVersion(buildID, grafanaDir string) (string, error) { pkgJSONPath := filepath.Join(grafanaDir, "package.json") @@ -184,3 +175,12 @@ func GetDroneCommit() (string, error) { } return commit, nil } + +func shortenBuildID(buildID string) string { + buildID = strings.ReplaceAll(buildID, "-", "") + if len(buildID) < 9 { + return buildID + } + + return buildID[0:8] +} diff --git a/pkg/build/frontend/build.go b/pkg/build/frontend/build.go index 5d65ec2d183..c4259c25e57 100644 --- a/pkg/build/frontend/build.go +++ b/pkg/build/frontend/build.go @@ -9,9 +9,23 @@ import ( "github.com/grafana/grafana/pkg/build/config" "github.com/grafana/grafana/pkg/build/errutil" + "github.com/grafana/grafana/pkg/build/lerna" "github.com/grafana/grafana/pkg/build/syncutil" ) +func BuildFrontendPackages(version string, edition config.Edition, grafanaDir string, p syncutil.WorkerPool, g *errutil.Group) error { + p.Schedule(g.Wrap(func() error { + if err := lerna.BuildFrontendPackages(version, edition, grafanaDir); err != nil { + return fmt.Errorf("failed to build %s frontend packages: %v", edition, err) + } + + log.Printf("Finished building %s frontend packages", string(edition)) + return nil + })) + + return nil +} + // Build builds the Grafana front-end func Build(edition config.Edition, grafanaDir string, p syncutil.WorkerPool, g *errutil.Group) error { log.Printf("Building %s frontend in %q", edition, grafanaDir) diff --git a/pkg/build/lerna/lerna.go b/pkg/build/lerna/lerna.go new file mode 100644 index 00000000000..de0cbbfe461 --- /dev/null +++ b/pkg/build/lerna/lerna.go @@ -0,0 +1,58 @@ +package lerna + +import ( + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "github.com/grafana/grafana/pkg/build/config" +) + +// BuildFrontendPackages will bump the version for the package to the latest canary build +// and build the packages so they are ready for being published, used for generating docs etc. +func BuildFrontendPackages(version string, mode config.Edition, grafanaDir string) error { + err := bumpLernaVersion(version, grafanaDir) + if err != nil { + return err + } + cmd := exec.Command("yarn", "run", "packages:build") + cmd.Dir = grafanaDir + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("failed to build %s frontend packages: %s", mode, output) + } + + return nil +} + +func bumpLernaVersion(version string, grafanaDir string) error { + //nolint:gosec + cmd := exec.Command("yarn", "run", "lerna", "version", version, "--exact", "--no-git-tag-version", "--no-push", "--force-publish", "-y") + cmd.Dir = grafanaDir + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("failed to bump version for frontend packages: %s\n%s", err, output) + } + + return nil +} + +func GetLernaVersion(grafanaDir string) (string, error) { + lernaJSONPath := filepath.Join(grafanaDir, "lerna.json") + //nolint:gosec + lernaJSONB, err := os.ReadFile(lernaJSONPath) + if err != nil { + return "", fmt.Errorf("failed to read %q: %w", lernaJSONPath, err) + } + pkgObj := map[string]interface{}{} + if err := json.Unmarshal(lernaJSONB, &pkgObj); err != nil { + return "", fmt.Errorf("failed decoding %q: %w", lernaJSONPath, err) + } + + version := pkgObj["version"].(string) + if version == "" { + return "", fmt.Errorf("failed to read version from %q", lernaJSONPath) + } + return strings.TrimSpace(version), nil +} diff --git a/scripts/drone/steps/lib.star b/scripts/drone/steps/lib.star index f06e5e02802..01fdb1f943b 100644 --- a/scripts/drone/steps/lib.star +++ b/scripts/drone/steps/lib.star @@ -415,12 +415,12 @@ def build_frontend_package_step(edition, ver_mode): # TODO: Use percentage for num jobs if ver_mode == 'release': cmds = [ - './bin/grabpl build-frontend-packages --jobs 8 ' + \ + './bin/build build-frontend-packages --jobs 8 ' + \ '--edition {} ${{DRONE_TAG}}'.format(edition), ] else: cmds = [ - './bin/grabpl build-frontend-packages --jobs 8 --edition {} '.format(edition) + \ + './bin/build build-frontend-packages --jobs 8 --edition {} '.format(edition) + \ '--build-id {}'.format(build_no), ]