Skip to content

Commit bca9795

Browse files
committed
[docker] Support metadata in Docker packages
1 parent 7eafa8f commit bca9795

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ config:
168168
config:
169169
# Dockerfile is the name of the Dockerfile to build. Automatically added to the package sources.
170170
dockerfile: "Dockerfile"
171+
# Metadata produces a metadata.yaml file in the resulting package tarball.
172+
metadata:
173+
foo: bar
171174
# build args are Docker build arguments. Often we just pass leeway build arguments along here.
172175
buildArgs:
173176
- arg=value

pkg/leeway/build.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package leeway
22

33
import (
44
"context"
5+
"encoding/base64"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -16,6 +17,7 @@ import (
1617
log "github.com/sirupsen/logrus"
1718
"golang.org/x/sync/semaphore"
1819
"golang.org/x/xerrors"
20+
"gopkg.in/yaml.v3"
1921
)
2022

2123
// PkgNotBuiltErr is used when a package's dependency hasn't been built yet
@@ -66,14 +68,18 @@ const (
6668
// dockerImageNamesFiles is the name of the file store in poushed Docker build artifacts
6769
// which contains the names of the Docker images we just pushed
6870
dockerImageNamesFiles = "imgnames.txt"
71+
72+
// dockerMetadataFile is the name of the file we YAML seralize the DockerPkgConfig.Metadata field to
73+
// when building Docker images. We use this mechanism to produce the version manifest as part of the Gitpod build.
74+
dockerMetadataFile = "metadata.yaml"
6975
)
7076

7177
// buildProcessVersions contain the current version of the respective build processes.
7278
// Increment this value if you change any of the build procedures.
7379
var buildProcessVersions = map[PackageType]int{
7480
YarnPackage: 6,
7581
GoPackage: 2,
76-
DockerPackage: 1,
82+
DockerPackage: 2,
7783
GenericPackage: 1,
7884
}
7985

@@ -1028,7 +1034,15 @@ func (p *Package) buildDocker(buildctx *buildContext, wd, result string) (err er
10281034
for _, img := range cfg.Image {
10291035
commands = append(commands, []string{"sh", "-c", fmt.Sprintf("echo %s >> %s", img, dockerImageNamesFiles)})
10301036
}
1031-
commands = append(commands, []string{"tar", "cfz", result, dockerImageNamesFiles})
1037+
// In addition to the imgnames.txt we also produce a file that contains the configured metadata,
1038+
// which provides a sensible way to add metadata to the image names.
1039+
consts, err := yaml.Marshal(cfg.Metadata)
1040+
if err != nil {
1041+
return err
1042+
}
1043+
commands = append(commands, []string{"sh", "-c", fmt.Sprintf("echo %s | base64 -d > %s", base64.StdEncoding.EncodeToString(consts), dockerMetadataFile)})
1044+
1045+
commands = append(commands, []string{"tar", "cfz", result, dockerImageNamesFiles, dockerMetadataFile})
10321046
}
10331047

10341048
return executeCommandsForPackage(buildctx, p, wd, commands)

pkg/leeway/package.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ type DockerPkgConfig struct {
456456
Image []string `yaml:"image,omitempty"`
457457
BuildArgs map[string]string `yaml:"buildArgs,omitempty"`
458458
Squash bool `yaml:"squash,omitempty"`
459+
Metadata map[string]string `yaml:"metadata,omitempty"`
459460
}
460461

461462
// AdditionalSources returns a list of unresolved sources coming in through this configuration

0 commit comments

Comments
 (0)