CI: move grabpl build-plugins from grabpl to grafana (#53071)

* add grabpl command to build internal plugins

* grabpl build-plugins -> ./bin/build build-plugins
This commit is contained in:
Kevin Minehart
2022-08-04 11:39:36 -05:00
committed by GitHub
parent bf3fa4a445
commit 138f03aad1
11 changed files with 609 additions and 12 deletions
+77 -1
View File
@@ -5,8 +5,12 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
type Metadata struct {
@@ -83,5 +87,77 @@ func GetVersion(mode VersionMode) (*Version, error) {
return &v, nil
}
return nil, fmt.Errorf("mode not found in version list")
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")
//nolint:gosec
pkgJSONB, err := ioutil.ReadFile(pkgJSONPath)
if err != nil {
return "", fmt.Errorf("failed to read %q: %w", pkgJSONPath, err)
}
pkgObj := map[string]interface{}{}
if err := json.Unmarshal(pkgJSONB, &pkgObj); err != nil {
return "", fmt.Errorf("failed decoding %q: %w", pkgJSONPath, err)
}
version := pkgObj["version"].(string)
if version == "" {
return "", fmt.Errorf("failed to read version from %q", pkgJSONPath)
}
if buildID != "" {
buildID = shortenBuildID(buildID)
verComponents := strings.Split(version, "-")
version = verComponents[0]
if len(verComponents) > 1 {
buildID = fmt.Sprintf("%s%s", buildID, verComponents[1])
}
version = fmt.Sprintf("%s-%s", version, buildID)
}
return version, nil
}
func CheckDroneTargetBranch() (VersionMode, error) {
reRlsBranch := regexp.MustCompile(`^v\d+\.\d+\.x$`)
target := os.Getenv("DRONE_TARGET_BRANCH")
if target == "" {
return "", fmt.Errorf("failed to get DRONE_TARGET_BRANCH environmental variable")
} else if target == string(MainMode) {
return MainMode, nil
}
if reRlsBranch.MatchString(target) {
return ReleaseBranchMode, nil
}
return "", fmt.Errorf("unrecognized target branch: %s", target)
}
func CheckSemverSuffix() (VersionMode, error) {
reBetaRls := regexp.MustCompile(`beta.*`)
reTestRls := regexp.MustCompile(`test.*`)
tagSuffix, ok := os.LookupEnv("DRONE_SEMVER_PRERELEASE")
if !ok || tagSuffix == "" {
fmt.Println("DRONE_SEMVER_PRERELEASE doesn't exist for a tag, this is a release event...")
return ReleaseMode, nil
}
switch {
case reBetaRls.MatchString(tagSuffix):
return BetaReleaseMode, nil
case reTestRls.MatchString(tagSuffix):
return TestReleaseMode, nil
default:
fmt.Printf("DRONE_SEMVER_PRERELEASE is custom string, release event with %s suffix\n", tagSuffix)
return ReleaseMode, nil
}
}