Skip to content

Commit

Permalink
Merge branch 'master' into mironoff/encrypt-pipeline-env
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ron0xFF authored Aug 9, 2023
2 parents 57edc39 + 97edad0 commit 8e87cd3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 46 deletions.
19 changes: 14 additions & 5 deletions cmd/cnbBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
)

const (
creatorPath = "/cnb/lifecycle/creator"
platformPath = "/tmp/platform"
creatorPath = "/cnb/lifecycle/creator"
platformPath = "/tmp/platform"
platformAPIVersion = "0.11"
)

type cnbBuildUtilsBundle struct {
Expand Down Expand Up @@ -325,13 +326,18 @@ func callCnbBuild(config *cnbBuildOptions, telemetryData *telemetry.CustomData,
return errors.Wrap(err, "failed to process config")
}

buildSummary := cnbutils.NewBuildSummary(dockerImage, utils)
for _, c := range mergedConfigs {
err = runCnbBuild(&c, telemetry, utils, commonPipelineEnvironment, httpClient)
imageSummary := &cnbutils.ImageSummary{}
err = runCnbBuild(&c, telemetry, imageSummary, utils, commonPipelineEnvironment, httpClient)
if err != nil {
return err
}
buildSummary.Images = append(buildSummary.Images, imageSummary)
}

buildSummary.Print()

if config.CreateBOM {
err = syft.GenerateSBOM(config.SyftDownloadURL, filepath.Dir(config.DockerConfigJSON), utils, utils, httpClient, commonPipelineEnvironment.container.registryURL, commonPipelineEnvironment.container.imageNameTags)
if err != nil {
Expand All @@ -343,7 +349,7 @@ func callCnbBuild(config *cnbBuildOptions, telemetryData *telemetry.CustomData,
return telemetry.Export()
}

func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, utils cnbutils.BuildUtils, commonPipelineEnvironment *cnbBuildCommonPipelineEnvironment, httpClient piperhttp.Sender) error {
func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, imageSummary *cnbutils.ImageSummary, utils cnbutils.BuildUtils, commonPipelineEnvironment *cnbBuildCommonPipelineEnvironment, httpClient piperhttp.Sender) error {
err := cleanDir("/layers", utils)
if err != nil {
log.SetErrorCategory(log.ErrorBuild)
Expand Down Expand Up @@ -382,6 +388,7 @@ func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, utils
log.SetErrorCategory(log.ErrorConfiguration)
return errors.Wrap(err, "failed to check if project descriptor exists")
}
imageSummary.ProjectDescriptor = projDescPath

var projectID string
if projDescPath != "" {
Expand Down Expand Up @@ -438,6 +445,7 @@ func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, utils

if config.BuildEnvVars != nil && len(config.BuildEnvVars) > 0 {
log.Entry().Infof("Setting custom environment variables: '%v'", config.BuildEnvVars)
imageSummary.AddEnv(config.BuildEnvVars)
err = cnbutils.CreateEnvFiles(utils, platformPath, config.BuildEnvVars)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
Expand Down Expand Up @@ -526,7 +534,7 @@ func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, utils
}

utils.AppendEnv([]string{fmt.Sprintf("CNB_REGISTRY_AUTH=%s", cnbRegistryAuth)})
utils.AppendEnv([]string{"CNB_PLATFORM_API=0.9"})
utils.AppendEnv([]string{fmt.Sprintf("CNB_PLATFORM_API=%s", platformAPIVersion)})

creatorArgs := []string{
"-no-color",
Expand Down Expand Up @@ -570,6 +578,7 @@ func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, utils
}
commonPipelineEnvironment.container.imageDigest = digest
commonPipelineEnvironment.container.imageDigests = append(commonPipelineEnvironment.container.imageDigests, digest)
imageSummary.ImageRef = fmt.Sprintf("%s@%s", containerImage, digest)

if len(config.PreserveFiles) > 0 {
if pathType != buildpacks.PathEnumArchive {
Expand Down
86 changes: 45 additions & 41 deletions cmd/cnbBuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assertLifecycleCalls(t, runner, 1)
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[0].Params, "-run-image")
assert.Contains(t, runner.Calls[0].Params, "my-run-image")
assert.Contains(t, runner.Calls[0].Params, "-process-type")
assert.Contains(t, runner.Calls[0].Params, "my-process")
assertLifecycleCalls(t, runner, 2)
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[1].Params, "-run-image")
assert.Contains(t, runner.Calls[1].Params, "my-run-image")
assert.Contains(t, runner.Calls[1].Params, "-process-type")
assert.Contains(t, runner.Calls[1].Params, "my-process")
assert.Equal(t, config.ContainerRegistryURL, commonPipelineEnvironment.container.registryURL)
assert.Equal(t, "my-image:0.0.1", commonPipelineEnvironment.container.imageNameTag)
assert.Equal(t, `{"cnbBuild":[{"dockerImage":"paketobuildpacks/builder:base"}]}`, commonPipelineEnvironment.custom.buildSettingsInfo)
Expand Down Expand Up @@ -169,8 +169,8 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assertLifecycleCalls(t, runner, 1)
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, "io-buildpacks-my-app", config.ContainerImageTag))
assertLifecycleCalls(t, runner, 2)
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, "io-buildpacks-my-app", config.ContainerImageTag))
assert.Equal(t, config.ContainerRegistryURL, commonPipelineEnvironment.container.registryURL)
assert.Equal(t, "io-buildpacks-my-app:0.0.1", commonPipelineEnvironment.container.imageNameTag)

Expand Down Expand Up @@ -204,8 +204,8 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assertLifecycleCalls(t, runner, 1)
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, config.ContainerImageName, config.ContainerImageTag))
assertLifecycleCalls(t, runner, 2)
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, config.ContainerImageName, config.ContainerImageTag))
assert.Equal(t, config.ContainerRegistryURL, commonPipelineEnvironment.container.registryURL)
assert.Equal(t, "my-image:0.0.1", commonPipelineEnvironment.container.imageNameTag)
})
Expand All @@ -229,8 +229,8 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assertLifecycleCalls(t, runner, 1)
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assertLifecycleCalls(t, runner, 2)
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Equal(t, fmt.Sprintf("https://%s", config.ContainerRegistryURL), commonPipelineEnvironment.container.registryURL)
assert.Equal(t, "my-image:0.0.1", commonPipelineEnvironment.container.imageNameTag)
})
Expand Down Expand Up @@ -258,11 +258,11 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assert.Equal(t, creatorPath, runner.Calls[0].Exec)
assert.Contains(t, runner.Calls[0].Params, "/tmp/buildpacks")
assert.Contains(t, runner.Calls[0].Params, "/tmp/buildpacks/order.toml")
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:latest", config.ContainerRegistryURL, config.ContainerImageName))
assert.Equal(t, creatorPath, runner.Calls[1].Exec)
assert.Contains(t, runner.Calls[1].Params, "/tmp/buildpacks")
assert.Contains(t, runner.Calls[1].Params, "/tmp/buildpacks/order.toml")
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:latest", config.ContainerRegistryURL, config.ContainerImageName))

copiedFileExists, _ := utils.FileExists("/tmp/config.json")
assert.True(t, copiedFileExists)
Expand Down Expand Up @@ -293,11 +293,11 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assert.Equal(t, creatorPath, runner.Calls[0].Exec)
assert.Contains(t, runner.Calls[0].Params, "/tmp/buildpacks")
assert.Contains(t, runner.Calls[0].Params, "/tmp/buildpacks/order.toml")
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:latest", config.ContainerRegistryURL, config.ContainerImageName))
assert.Equal(t, creatorPath, runner.Calls[1].Exec)
assert.Contains(t, runner.Calls[1].Params, "/tmp/buildpacks")
assert.Contains(t, runner.Calls[1].Params, "/tmp/buildpacks/order.toml")
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:latest", config.ContainerRegistryURL, config.ContainerImageName))

copiedFileExists, _ := utils.FileExists("/tmp/config.json")
assert.True(t, copiedFileExists)
Expand Down Expand Up @@ -327,11 +327,11 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assert.Equal(t, creatorPath, runner.Calls[0].Exec)
assert.Contains(t, runner.Calls[0].Params, "/tmp/buildpacks")
assert.Contains(t, runner.Calls[0].Params, "/tmp/buildpacks/order.toml")
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:latest", config.ContainerRegistryURL, config.ContainerImageName))
assert.Equal(t, creatorPath, runner.Calls[1].Exec)
assert.Contains(t, runner.Calls[1].Params, "/tmp/buildpacks")
assert.Contains(t, runner.Calls[1].Params, "/tmp/buildpacks/order.toml")
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:latest", config.ContainerRegistryURL, config.ContainerImageName))

copiedFileExists, _ := utils.FileExists("/tmp/config.json")
assert.True(t, copiedFileExists)
Expand Down Expand Up @@ -372,8 +372,8 @@ func TestRunCnbBuild(t *testing.T) {
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assert.Contains(t, runner.Env, fmt.Sprintf("SSL_CERT_FILE=%s", caCertsTmpFile))
assertLifecycleCalls(t, runner, 1)
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assertLifecycleCalls(t, runner, 2)
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
})

t.Run("success case (additionalTags)", func(t *testing.T) {
Expand All @@ -394,11 +394,11 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)

runner := utils.ExecMockRunner
assertLifecycleCalls(t, runner, 1)
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:3", config.ContainerRegistryURL, config.ContainerImageName))
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:3.1", config.ContainerRegistryURL, config.ContainerImageName))
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:3.1.5", config.ContainerRegistryURL, config.ContainerImageName))
assertLifecycleCalls(t, runner, 2)
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:3", config.ContainerRegistryURL, config.ContainerImageName))
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:3.1", config.ContainerRegistryURL, config.ContainerImageName))
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:3.1.5", config.ContainerRegistryURL, config.ContainerImageName))
})

t.Run("success case: build environment variables", func(t *testing.T) {
Expand Down Expand Up @@ -434,7 +434,7 @@ func TestRunCnbBuild(t *testing.T) {
err := callCnbBuild(&config, &telemetryData, &utils, &commonPipelineEnvironment, &piperhttp.Client{})

require.NoError(t, err)
assertLifecycleCalls(t, utils.ExecMockRunner, 1)
assertLifecycleCalls(t, utils.ExecMockRunner, 2)

assetBuildEnv(t, utils, "OPTIONS_KEY", "OPTIONS_VALUE")
assetBuildEnv(t, utils, "PROJECT_DESCRIPTOR_KEY", "PROJECT_DESCRIPTOR_VALUE")
Expand All @@ -461,7 +461,7 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)

runner := utils.ExecMockRunner
assertLifecycleCalls(t, runner, 1)
assertLifecycleCalls(t, runner, 2)

assert.True(t, utils.FilesMock.HasCreatedSymlink("/jenkins/target", "/workspace/target"))
})
Expand All @@ -485,7 +485,7 @@ func TestRunCnbBuild(t *testing.T) {
require.NoError(t, err)

runner := utils.ExecMockRunner
assertLifecycleCalls(t, runner, 1)
assertLifecycleCalls(t, runner, 2)

assert.False(t, utils.FilesMock.HasCreatedSymlink("/jenkins/target", "/workspace/target"))
})
Expand Down Expand Up @@ -682,7 +682,7 @@ uri = "some-buildpack"`))
require.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, config.ContainerImageName, config.ContainerImageTag))
assert.Contains(t, runner.Calls[1].Params, fmt.Sprintf("%s/%s:%s", imageRegistry, config.ContainerImageName, config.ContainerImageTag))
assert.Equal(t, config.ContainerRegistryURL, commonPipelineEnvironment.container.registryURL)
assert.Equal(t, "my-image:3.1.5", commonPipelineEnvironment.container.imageNameTag)
})
Expand Down Expand Up @@ -772,11 +772,15 @@ uri = "some-buildpack"
require.Equal(t, expectedImageCount, len(customData.Data))

runner := utils.ExecMockRunner
require.Equal(t, expectedImageCount, len(runner.Calls))
require.Equal(t, expectedImageCount, len(runner.Calls)-1)
for i, call := range runner.Calls {
assert.Equal(t, 4, len(customData.Data[i].AdditionalTags))
if i == 0 { // first call is -version
continue
}
lifecycleCall := i - 1
assert.Equal(t, 4, len(customData.Data[lifecycleCall].AdditionalTags))
assertLifecycleCalls(t, runner, i+1)
containerImageName := fmt.Sprintf("my-image-%d", i)
containerImageName := fmt.Sprintf("my-image-%d", lifecycleCall)
assert.Contains(t, call.Params, fmt.Sprintf("%s/%s:%s", config.ContainerRegistryURL, containerImageName, config.ContainerImageTag))
assert.Contains(t, call.Params, fmt.Sprintf("%s/%s:3", config.ContainerRegistryURL, containerImageName))
assert.Contains(t, call.Params, fmt.Sprintf("%s/%s:3.1", config.ContainerRegistryURL, containerImageName))
Expand Down
24 changes: 24 additions & 0 deletions integration/integration_cnb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ func TestCNBIntegrationProjectDescriptor(t *testing.T) {
)
container.terminate(t)
}
func TestCNBIntegrationBuildSummary(t *testing.T) {
t.Parallel()
ctx := context.Background()
registryContainer := setupDockerRegistry(t, ctx)
defer registryContainer.Terminate(ctx)

container := givenThisContainer(t, IntegrationTestDockerExecRunnerBundle{
Image: baseBuilder,
User: "cnb",
TestDir: []string{"testdata", "TestCnbIntegration", "project"},
Network: fmt.Sprintf("container:%s", registryContainer.GetContainerID()),
})

err := container.whenRunningPiperCommand("cnbBuild", "--noTelemetry", "--verbose", "--containerImageName", "not-found", "--containerImageTag", "0.0.1", "--containerRegistryUrl", registryURL)
assert.NoError(t, err)

container.assertHasOutput(t, "*** Build Summary ***",
" Builder: \"paketobuildpacks/builder:base\"",
" Lifecycle: \"0.16.4+683e1b46\"",
" Image: \"localhost:5000/not-found@sha256:",
" Project descriptor: \"/project/project.toml\"",
" Env: \"TMPDIR, BP_NODE_VERSION\"")
container.terminate(t)
}

func TestCNBIntegrationZipPath(t *testing.T) {
t.Parallel()
Expand Down
65 changes: 65 additions & 0 deletions pkg/cnbutils/summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cnbutils

import (
"bytes"
"strings"

"github.com/SAP/jenkins-library/pkg/log"
)

type BuildSummary struct {
Builder string
LifecycleVersion string

Images []*ImageSummary
}

func (bs *BuildSummary) Print() {
log.Entry().Infoln("")
log.Entry().Info("*** Build Summary ***")
log.Entry().Infof(" Builder: %q", bs.Builder)
log.Entry().Infof(" Lifecycle: %q", bs.LifecycleVersion)
log.Entry().Infof(" %d image(s) build:", len(bs.Images))
log.Entry().Infoln("")
for _, image := range bs.Images {
image.Print()
log.Entry().Infoln("")
}
}

func NewBuildSummary(builder string, utils BuildUtils) *BuildSummary {
return &BuildSummary{
Builder: builder,
LifecycleVersion: lifecycleVersion(utils),
}
}

type ImageSummary struct {
ImageRef string
ProjectDescriptor string
Buildpacks []string
EnvVars []string
}

func (is *ImageSummary) Print() {
log.Entry().Infof(" Image: %q", is.ImageRef)
log.Entry().Infof(" Project descriptor: %q", is.ProjectDescriptor)
log.Entry().Infof(" Env: %q", strings.Join(is.EnvVars, ", "))
}

func (is *ImageSummary) AddEnv(env map[string]interface{}) {
for key := range env {
is.EnvVars = append(is.EnvVars, key)
}
}

func lifecycleVersion(utils BuildUtils) string {
currentStdout := utils.GetStdout()

buf := bytes.NewBufferString("")
utils.Stdout(buf)
_ = utils.RunExecutable("/cnb/lifecycle/lifecycle", "-version")
utils.Stdout(currentStdout)

return strings.Trim(buf.String(), "\n")
}

0 comments on commit 8e87cd3

Please sign in to comment.