Support for onbuild and unit test
This commit is contained in:
parent
c63a03d123
commit
27f964957b
|
|
@ -23,6 +23,8 @@ import (
|
|||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/image"
|
||||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/snapshot"
|
||||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
|
||||
"github.com/containers/image/manifest"
|
||||
"github.com/docker/docker/builder/dockerfile/instructions"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -141,6 +143,9 @@ func execute() error {
|
|||
imageConfig := sourceImage.Config()
|
||||
// Currently only supports single stage builds
|
||||
for _, stage := range stages {
|
||||
if err := resolveOnBuild(&stage, imageConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, cmd := range stage.Commands {
|
||||
dockerCommand, err := commands.GetCommand(cmd, srcContext)
|
||||
if err != nil {
|
||||
|
|
@ -173,6 +178,21 @@ func execute() error {
|
|||
return image.PushImage(sourceImage, destination)
|
||||
}
|
||||
|
||||
func resolveOnBuild(stage *instructions.Stage, config *manifest.Schema2Config) error {
|
||||
if config.OnBuild == nil {
|
||||
return nil
|
||||
}
|
||||
// Otherwise, parse into commands
|
||||
cmds, err := dockerfile.ParseCommands(config.OnBuild)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Append to the beginning of the commands in the stage
|
||||
stage.Commands = append(cmds, stage.Commands...)
|
||||
logrus.Infof("Executing %v build triggers", len(cmds))
|
||||
return nil
|
||||
}
|
||||
|
||||
// setDefaultEnv sets default values for HOME and PATH so that
|
||||
// config.json and docker-credential-gcr can be accessed
|
||||
func setDefaultEnv() error {
|
||||
|
|
|
|||
|
|
@ -56,6 +56,8 @@ func GetCommand(cmd instructions.Command, buildcontext string) (DockerCommand, e
|
|||
return &LabelCommand{cmd: c}, nil
|
||||
case *instructions.UserCommand:
|
||||
return &UserCommand{cmd: c}, nil
|
||||
case *instructions.OnbuildCommand:
|
||||
return &OnBuildCommand{cmd: c}, nil
|
||||
}
|
||||
return nil, errors.Errorf("%s is not a supported command", cmd.Name())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
Copyright 2018 Google LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
|
||||
"github.com/containers/image/manifest"
|
||||
"github.com/docker/docker/builder/dockerfile/instructions"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type OnBuildCommand struct {
|
||||
cmd *instructions.OnbuildCommand
|
||||
}
|
||||
|
||||
func (o *OnBuildCommand) ExecuteCommand(config *manifest.Schema2Config) error {
|
||||
logrus.Info("cmd: ONBUILD")
|
||||
resolvedExpression, err := util.ResolveEnvironmentReplacement(o.cmd.Expression, config.Env, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if config.OnBuild == nil {
|
||||
config.OnBuild = []string{resolvedExpression}
|
||||
} else {
|
||||
config.OnBuild = append(config.OnBuild, resolvedExpression)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// No files have changed, this command only touches metadata.
|
||||
func (o *OnBuildCommand) FilesToSnapshot() []string {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// CreatedBy returns some information about the command for the image config history
|
||||
func (o *OnBuildCommand) CreatedBy() string {
|
||||
return o.cmd.Expression
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
Copyright 2018 Google LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/GoogleCloudPlatform/k8s-container-builder/testutil"
|
||||
"github.com/containers/image/manifest"
|
||||
"github.com/docker/docker/builder/dockerfile/instructions"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var onbuildTests = []struct {
|
||||
expression string
|
||||
onbuildArray []string
|
||||
expectedArray []string
|
||||
}{
|
||||
{
|
||||
expression: "RUN echo \\\"hi\\\" > $dir",
|
||||
onbuildArray: nil,
|
||||
expectedArray: []string{
|
||||
"RUN echo \"hi\" > /some/dir",
|
||||
},
|
||||
},
|
||||
{
|
||||
expression: "COPY foo foo",
|
||||
onbuildArray: []string{
|
||||
"RUN echo \"hi\" > /some/dir",
|
||||
},
|
||||
expectedArray: []string{
|
||||
"RUN echo \"hi\" > /some/dir",
|
||||
"COPY foo foo",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestExecuteOnbuild(t *testing.T) {
|
||||
for _, test := range onbuildTests {
|
||||
cfg := &manifest.Schema2Config{
|
||||
Env: []string{
|
||||
"dir=/some/dir",
|
||||
},
|
||||
OnBuild: test.onbuildArray,
|
||||
}
|
||||
|
||||
onbuildCmd := &OnBuildCommand{
|
||||
&instructions.OnbuildCommand{
|
||||
Expression: test.expression,
|
||||
},
|
||||
}
|
||||
|
||||
err := onbuildCmd.ExecuteCommand(cfg)
|
||||
testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedArray, cfg.OnBuild)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ package dockerfile
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/builder/dockerfile/instructions"
|
||||
"github.com/docker/docker/builder/dockerfile/parser"
|
||||
|
|
@ -35,3 +36,21 @@ func Parse(b []byte) ([]instructions.Stage, error) {
|
|||
}
|
||||
return stages, err
|
||||
}
|
||||
|
||||
// ParseCommands parses an array of commands into an array of instructions.Command; used for onbuild
|
||||
func ParseCommands(cmdArray []string) ([]instructions.Command, error) {
|
||||
var cmds []instructions.Command
|
||||
cmdString := strings.Join(cmdArray, "\n")
|
||||
ast, err := parser.Parse(strings.NewReader(cmdString))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, child := range ast.AST.Children {
|
||||
cmd, err := instructions.ParseCommand(child)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
return cmds, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue