Skip to content

Commit

Permalink
feat: add appendDockerfileInstructions to image config that are injec…
Browse files Browse the repository at this point in the history
…ted during build time
  • Loading branch information
FabianKramm committed Jun 18, 2020
1 parent 9048139 commit 03962fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/devspace/build/builder/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ func (b *Builder) BuildImage(contextPath, dockerfilePath string, entrypoint []st
}

// Check if we should overwrite entrypoint
if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper {
dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, options.Target, b.helper.ImageConf.InjectRestartHelper, log)
if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper || len(b.helper.ImageConf.AppendDockerfileInstructions) > 0 {
dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, b.helper.ImageConf.AppendDockerfileInstructions, options.Target, b.helper.ImageConf.InjectRestartHelper, log)
if err != nil {
return err
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/devspace/build/builder/helper/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ func OverwriteDockerfileInBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.

// RewriteDockerfile rewrites the given dockerfile contents with the new entrypoint cmd and target. It does also inject the restart
// helper if specified
func RewriteDockerfile(dockerfile string, entrypoint []string, cmd []string, target string, injectHelper bool, log logpkg.Logger) (string, error) {
if len(entrypoint) == 0 && len(cmd) == 0 && !injectHelper {
func RewriteDockerfile(dockerfile string, entrypoint []string, cmd []string, additionalInstructions []string, target string, injectHelper bool, log logpkg.Logger) (string, error) {
if len(entrypoint) == 0 && len(cmd) == 0 && !injectHelper && len(additionalInstructions) == 0 {
return "", nil
}
if additionalInstructions == nil {
additionalInstructions = []string{}
}

additionalLines := []string{}
if injectHelper {
data, err := ioutil.ReadFile(dockerfile)
if err != nil {
Expand Down Expand Up @@ -142,16 +144,16 @@ func RewriteDockerfile(dockerfile string, entrypoint []string, cmd []string, tar
}

entrypoint = append([]string{restart.ScriptPath}, entrypoint...)
additionalLines = append(additionalLines, "COPY /.devspace /")
additionalInstructions = append(additionalInstructions, "COPY /.devspace /")
}

return CreateTempDockerfile(dockerfile, entrypoint, cmd, additionalLines, target)
return CreateTempDockerfile(dockerfile, entrypoint, cmd, additionalInstructions, target)
}

// CreateTempDockerfile creates a new temporary dockerfile that appends a new entrypoint and cmd
func CreateTempDockerfile(dockerfile string, entrypoint []string, cmd []string, additionalLines []string, target string) (string, error) {
if entrypoint == nil && cmd == nil {
return "", errors.New("Entrypoint & cmd are empty")
if entrypoint == nil && cmd == nil && len(additionalLines) == 0 {
return "", errors.New("entrypoint, cmd & additional lines are empty")
}

data, err := ioutil.ReadFile(dockerfile)
Expand Down
4 changes: 2 additions & 2 deletions pkg/devspace/build/builder/kaniko/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ func (b *Builder) BuildImage(contextPath, dockerfilePath string, entrypoint []st
}

// Check if we should overwrite entrypoint
if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper {
dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, options.Target, b.helper.ImageConf.InjectRestartHelper, log)
if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper || len(b.helper.ImageConf.AppendDockerfileInstructions) > 0 {
dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, b.helper.ImageConf.AppendDockerfileInstructions, options.Target, b.helper.ImageConf.InjectRestartHelper, log)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/devspace/config/versions/latest/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ type ImageConfig struct {
// dockerfile for this image, otherwise devspace will fail.
InjectRestartHelper bool `yaml:"injectRestartHelper,omitempty"`

// These instructions will be appended to the Dockerfile that is build at the current build target
// and are appended before the entrypoint and cmd instructions
AppendDockerfileInstructions []string `yaml:"appendDockerfileInstructions,omitempty"`

// Specific build options how to build the specified image
Build *BuildConfig `yaml:"build,omitempty"`
}
Expand Down

0 comments on commit 03962fb

Please sign in to comment.