Skip to content

Commit

Permalink
feat(elastic-agent-services): add another tag to docker image for age…
Browse files Browse the repository at this point in the history
…ntless use
  • Loading branch information
olegsu committed Feb 14, 2025
1 parent 45e3abf commit e60e748
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
49 changes: 38 additions & 11 deletions dev-tools/mage/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,30 @@ func (b *dockerBuilder) Build() error {
return fmt.Errorf("failed to prepare build: %w", err)
}

tag, err := b.dockerBuild()
tag, additionalTags, err := b.dockerBuild()
tries := 3
for err != nil && tries != 0 {
fmt.Println(">> Building docker images again (after 10 s)")
// This sleep is to avoid hitting the docker build issues when resources are not available.
time.Sleep(time.Second * 10)
tag, err = b.dockerBuild()
tag, additionalTags, err = b.dockerBuild()
tries--
}
if err != nil {
return fmt.Errorf("failed to build docker: %w", err)
}

if err := b.dockerSave(tag); err != nil {
if err := b.dockerSave(tag, defaultBinaryName); err != nil {
return fmt.Errorf("failed to save docker as artifact: %w", err)
}

// additional tags should not be created with
for _, tag := range additionalTags {
if err := b.dockerSave(tag, "{{.Name}}{{if .OS}}-{{.OS}}{{end}}{{if .Arch}}-{{.Arch}}{{end}}"); err != nil {
return fmt.Errorf("failed to save docker as artifact: %w", err)
}
}

return nil
}

Expand Down Expand Up @@ -173,22 +180,42 @@ func (b *dockerBuilder) expandDockerfile(templatesDir string, data map[string]in
return nil
}

func (b *dockerBuilder) dockerBuild() (string, error) {
tag := fmt.Sprintf("%s:%s", b.imageName, b.Version)
// dockerBuild runs "docker build -t t1 -t t2 ... buildDir"
// returns the main tag and the more tags
// returns an error if the command fails
func (b *dockerBuilder) dockerBuild() (string, []string, error) {
mainTag := fmt.Sprintf("%s:%s", b.imageName, b.Version)
// For Independent Agent releases, replace the "+" with a "." since the "+" character
// currently isn't allowed in a tag in Docker
// E.g., 8.13.0+build202402191057 -> 8.13.0.build202402191057
tag = strings.Replace(tag, "+", ".", 1)
mainTag = strings.Replace(mainTag, "+", ".", 1)
if b.Snapshot {
tag = tag + "-SNAPSHOT"
mainTag = mainTag + "-SNAPSHOT"
}
if repository := b.ExtraVars["repository"]; repository != "" {
tag = fmt.Sprintf("%s/%s", repository, tag)
mainTag = fmt.Sprintf("%s/%s", repository, mainTag)
}

moreTags := []string{}
if commit, found := b.ExtraVars["commit"]; found && b.DockerVariant == Service {
if len(commit) >= 12 {
moreTags = append(moreTags, fmt.Sprintf("%s:%s", b.imageName, "git-"+commit[:12]))
}
}

args := []string{
"build",
"-t", mainTag,
}
return tag, sh.Run("docker", "build", "-t", tag, b.buildDir)
for _, t := range moreTags {
args = append(args, "-t", t)
}
args = append(args, b.buildDir)

return mainTag, moreTags, sh.Run("docker", args...)
}

func (b *dockerBuilder) dockerSave(tag string) error {
func (b *dockerBuilder) dockerSave(tag string, binaryNameTemplate string) error {
if _, err := os.Stat(distributionsDir); os.IsNotExist(err) {
err := os.MkdirAll(distributionsDir, 0750)
if err != nil {
Expand All @@ -198,7 +225,7 @@ func (b *dockerBuilder) dockerSave(tag string) error {
// Save the container as artifact
outputFile := b.OutputFile
if outputFile == "" {
outputTar, err := b.Expand(defaultBinaryName+".docker.tar.gz", map[string]interface{}{
outputTar, err := b.Expand(binaryNameTemplate+".docker.tar.gz", map[string]interface{}{
"Name": b.imageName,
})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions dev-tools/packaging/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ shared:
# service build is based on previous cloud variant
- &agent_docker_service_spec
docker_variant: 'service'
extra_vars:
commit: {{ commit }}
files:
'data/service/connectors-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}.zip':
source: '{{.AgentDropPath}}/archives/{{.GOOS}}-{{.AgentArchName}}.tar.gz/connectors-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}.zip'
Expand Down

0 comments on commit e60e748

Please sign in to comment.