Skip to content

Commit

Permalink
Add nightly build support to oss publish scripts (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
guicaulada committed Sep 11, 2023
1 parent 6476be0 commit a5e270b
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 21 deletions.
18 changes: 18 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ var DockerFlags = []cli.Flag{
Usage: "The Ubuntu image to use as the base image when building the Ubuntu version of the Grafana docker image",
Value: "ubuntu:latest",
},
&cli.StringFlag{
Name: "org",
Usage: "Overrides the organization of the images",
Value: "grafana",
},
&cli.StringFlag{
Name: "repo",
Usage: "Overrides the repository of the images",
},
}

var DockerPublishFlags = []cli.Flag{
Expand All @@ -187,6 +196,15 @@ var DockerPublishFlags = []cli.Flag{
Usage: "Prefix the image name with the registry provided",
Value: "docker.io",
},
&cli.StringFlag{
Name: "org",
Usage: "Overrides the organization of the images",
Value: "grafana",
},
&cli.StringFlag{
Name: "repo",
Usage: "Overrides the repository of the images",
},
&cli.BoolFlag{
Name: "latest",
Usage: "Tags the published images as latest",
Expand Down
8 changes: 8 additions & 0 deletions containers/opts_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type DockerOpts struct {
// Password is supplied to login to the docker registry when publishing images.
Password string

// Org overrides the organization when when publishing images.
Org string

// Repository overrides the repository when when publishing images.
Repository string

// Latest is supplied to also tag as latest when publishing images.
Latest bool
}
Expand All @@ -34,6 +40,8 @@ func DockerOptsFromFlags(c cliutil.CLIContext) *DockerOpts {
UbuntuBase: c.String("ubuntu-base"),
Username: c.String("username"),
Password: c.String("password"),
Org: c.String("org"),
Repository: c.String("repo"),
Latest: c.Bool("latest"),
}
}
33 changes: 20 additions & 13 deletions pipelines/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@ func ImageTag(registry, org, repo, version string) string {

// GrafanaImageTag returns the name of the grafana docker image based on the tar package name.
// To maintain backwards compatibility, we must keep this the same as it was before.
func GrafanaImageTags(base BaseImage, registry string, opts TarFileOpts) []string {
func GrafanaImageTags(base BaseImage, dockerOpts *containers.DockerOpts, tarOpts TarFileOpts) []string {
var (
org = "grafana"
repos = []string{"grafana-image-tags", "grafana-oss-image-tags"}
version = opts.Version
repos = []string{"grafana-image-tags", "grafana-oss-image-tags"}

edition = opts.Edition
)
version = tarOpts.Version
edition = tarOpts.Edition

if edition != "" {
// Non-grafana repositories only create images in 1 repository instead of 2. Reason unknown.
repos = []string{fmt.Sprintf("grafana-%s-image-tags", edition)}
}
org = dockerOpts.Org
registry = dockerOpts.Registry
repository = dockerOpts.Repository
)

// For some unknown reason, versions in docker hub do not have a 'v'.
// I think this was something that was established a long time ago and just stuck.
Expand All @@ -48,11 +46,20 @@ func GrafanaImageTags(base BaseImage, registry string, opts TarFileOpts) []strin
version += "-ubuntu"
}

if opts.Distro != "" {
arch := executil.FullArch(opts.Distro)
if tarOpts.Distro != "" {
arch := executil.FullArch(tarOpts.Distro)
version += "-" + strings.ReplaceAll(arch, "/", "")
}

if repository != "" {
return []string{ImageTag(registry, org, repository, version)}
}

if edition != "" {
// Non-grafana repositories only create images in 1 repository instead of 2. Reason unknown.
repos = []string{fmt.Sprintf("grafana-%s-image-tags", edition)}
}

tags := make([]string, len(repos))
for i, repo := range repos {
tags[i] = ImageTag(registry, org, repo, version)
Expand Down Expand Up @@ -88,7 +95,7 @@ func Docker(ctx context.Context, d *dagger.Client, args PipelineArgs) error {
for _, base := range bases {
var (
platform = executil.Platform(tarOpts.Distro)
tags = GrafanaImageTags(base, opts.Registry, tarOpts)
tags = GrafanaImageTags(base, opts, tarOpts)
baseImage = opts.AlpineBase
socket = d.Host().UnixSocket("/var/run/docker.sock")
)
Expand Down
2 changes: 1 addition & 1 deletion pipelines/docker_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func PublishDocker(ctx context.Context, d *dagger.Client, args PipelineArgs) err
base = BaseImageUbuntu
}

tags := GrafanaImageTags(base, opts.Registry, tarOpts)
tags := GrafanaImageTags(base, opts, tarOpts)
for _, tag := range tags {
// For each tag we publish an image and add the tag to the list of tags for a specific manifest
// Since each package has a maximum of 2 tags, this for loop will only run twice on a worst case scenario
Expand Down
99 changes: 92 additions & 7 deletions pipelines/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"testing"

"github.com/grafana/grafana-build/containers"
"github.com/grafana/grafana-build/pipelines"
)

Expand All @@ -17,6 +18,7 @@ func TestImageName(t *testing.T) {
Description string
Tags []string
BaseImage pipelines.BaseImage
DockerOpts *containers.DockerOpts
TarOpts pipelines.TarFileOpts
}

Expand All @@ -26,38 +28,50 @@ func TestImageName(t *testing.T) {

cases := []tc{
{
Description: "Grafana docker images are created for both the 'docker.io/grafana/grafana-image-tags' and 'docker.io/grafana/grafana-oss-image-tags' repositories. AMD64 docker images have no suffix. Alpine images also have no suffix.",
Description: "Grafana docker images are created for both the 'docker.io/grafana/grafana-image-tags' and 'docker.io/grafana/grafana-oss-image-tags' repositories. Alpine images have no suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "",
Distro: "linux/amd64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageAlpine,
Tags: []string{
"docker.io/grafana/grafana-image-tags:1.2.3-test.1.2.3-amd64",
"docker.io/grafana/grafana-oss-image-tags:1.2.3-test.1.2.3-amd64",
},
},
{
Description: "Grafana docker images are created for both the 'docker.io/grafana/grafana-image-tags' and 'docker.io/grafana/grafana-oss-image-tags' repositories. ARM64 images have a -arm64 suffix. Alpine images also have no suffix.",
Description: "Grafana docker images are created for both the 'docker.io/grafana/grafana-image-tags' and 'docker.io/grafana/grafana-oss-image-tags' repositories. ARM64 images have a -arm64 suffix. Alpine images have no suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "",
Distro: "linux/arm64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageAlpine,
Tags: []string{
"docker.io/grafana/grafana-image-tags:1.2.3-test.1.2.3-arm64",
"docker.io/grafana/grafana-oss-image-tags:1.2.3-test.1.2.3-arm64",
},
},
{
Description: "Grafana docker images are created for both the 'docker.io/grafana/grafana-image-tags' and 'docker.io/grafana/grafana-oss-image-tags' repositories. AMD64 docker images have no suffix. Ubuntu images have a '-ubuntu' suffix.",
Description: "Grafana docker images are created for both the 'docker.io/grafana/grafana-image-tags' and 'docker.io/grafana/grafana-oss-image-tags' repositories. Ubuntu images have a '-ubuntu' suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "",
Distro: "linux/amd64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageUbuntu,
Tags: []string{
"docker.io/grafana/grafana-image-tags:1.2.3-test.1.2.3-ubuntu-amd64",
Expand All @@ -71,43 +85,59 @@ func TestImageName(t *testing.T) {
Distro: "linux/arm64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageUbuntu,
Tags: []string{
"docker.io/grafana/grafana-image-tags:1.2.3-test.1.2.3-ubuntu-arm64",
"docker.io/grafana/grafana-oss-image-tags:1.2.3-test.1.2.3-ubuntu-arm64",
},
},
{
Description: "Enterprise docker images are created for only the docker.io/grafana/grafana-enterprise-image-tags repository. AMD64 docker images have no suffix. Alpine images also have no suffix.",
Description: "Enterprise docker images are created for only the docker.io/grafana/grafana-enterprise-image-tags repository. Alpine images have no suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "enterprise",
Distro: "linux/amd64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageAlpine,
Tags: []string{
"docker.io/grafana/grafana-enterprise-image-tags:1.2.3-test.1.2.3-amd64",
},
},
{
Description: "Enterprise docker images are created for only the docker.io/grafana/grafana-enterprise-image-tags repository. ARM64 images have an -arm64 suffix. Alpine images also have no suffix.",
Description: "Enterprise docker images are created for only the docker.io/grafana/grafana-enterprise-image-tags repository. ARM64 images have an -arm64 suffix. Alpine images have no suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "enterprise",
Distro: "linux/arm64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageAlpine,
Tags: []string{
"docker.io/grafana/grafana-enterprise-image-tags:1.2.3-test.1.2.3-arm64",
},
},
{
Description: "Enterprise docker images are created for only the docker.io/grafana/grafana-enterprise-image-tags repository. AMD64 docker images have no suffix. Ubuntu images have a '-ubuntu' suffix.",
Description: "Enterprise docker images are created for only the docker.io/grafana/grafana-enterprise-image-tags repository. Ubuntu images have a '-ubuntu' suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "enterprise",
Distro: "linux/amd64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageUbuntu,
Tags: []string{
"docker.io/grafana/grafana-enterprise-image-tags:1.2.3-test.1.2.3-ubuntu-amd64",
Expand All @@ -120,17 +150,72 @@ func TestImageName(t *testing.T) {
Distro: "linux/arm64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "grafana",
Registry: "docker.io",
},
BaseImage: pipelines.BaseImageUbuntu,
Tags: []string{
"docker.io/grafana/grafana-enterprise-image-tags:1.2.3-test.1.2.3-ubuntu-arm64",
},
},
{
Description: "Grafana docker images are created for both the 'registry.io/org/grafana-image-tags' and 'registry.io/org/grafana-oss-image-tags' repositories. Alpine images have no suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "",
Distro: "linux/amd64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "org",
Registry: "registry.io",
},
BaseImage: pipelines.BaseImageAlpine,
Tags: []string{
"registry.io/org/grafana-image-tags:1.2.3-test.1.2.3-amd64",
"registry.io/org/grafana-oss-image-tags:1.2.3-test.1.2.3-amd64",
},
},
{
Description: "Grafana docker images are created for only the 'registry.io/org/grafana-dev' repository. Alpine images have no suffix.",
TarOpts: pipelines.TarFileOpts{
Edition: "",
Distro: "linux/amd64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "org",
Registry: "registry.io",
Repository: "grafana-dev",
},
BaseImage: pipelines.BaseImageAlpine,
Tags: []string{
"registry.io/org/grafana-dev:1.2.3-test.1.2.3-amd64",
},
},
{
Description: "Grafana docker images are created for only the 'registry.io/org/grafana-dev' repository.",
TarOpts: pipelines.TarFileOpts{
Edition: "",
Distro: "linux/amd64",
Version: version,
},
DockerOpts: &containers.DockerOpts{
Org: "org",
Registry: "registry.io",
Repository: "grafana-dev",
},
BaseImage: pipelines.BaseImageUbuntu,
Tags: []string{
"registry.io/org/grafana-dev:1.2.3-test.1.2.3-ubuntu-amd64",
},
},
}

for n, test := range cases {
t.Run(fmt.Sprintf("[%d / %d] %s", n+1, len(cases), test.Description), func(t *testing.T) {
expect := sort.StringSlice(test.Tags)
res := sort.StringSlice(pipelines.GrafanaImageTags(test.BaseImage, "docker.io", test.TarOpts))
res := sort.StringSlice(pipelines.GrafanaImageTags(test.BaseImage, test.DockerOpts, test.TarOpts))

for i := range expect {
e := expect[i]
Expand Down
Loading

0 comments on commit a5e270b

Please sign in to comment.