Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #64 from GoogleCloudPlatform/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
codrienne authored May 1, 2018
2 parents 5485e3f + 96d462f commit 7b17d1f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 110 deletions.
54 changes: 2 additions & 52 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ const (
// in the face of gcr.io DNS lookup errors.
maxPushRetries = 10

// builderOutputFile is the name of the file inside each per-step
// temporary output directory where builders can write arbitrary output
// information.
builderOutputFile = "output"

// stepOutputPath is the path to step output files produced by a step
// execution. This is the path that's exposed to builders. It's backed by a
// tempdir in the worker.
stepOutputPath = "/builder/outputs"

// maxOutputBytes is the max length of outputs persisted from builder
// outputs. Data beyond this length is ignored.
maxOutputBytes = 4 * 1024 // 4 KB
)

var (
Expand Down Expand Up @@ -160,10 +147,6 @@ type Build struct {
// filesystem, in tests it's an in-memory filesystem.
fs afero.Fs

// stepOutputs contains builder outputs produced by each step, if any.
// It is initialized once using initStepOutputsOnce.
stepOutputs [][]byte
initStepOutputsOnce sync.Once
}

// TimingInfo holds timing information for build execution phases.
Expand Down Expand Up @@ -971,10 +954,7 @@ func (b *Build) runStep(ctx context.Context, idx int) error {
runTarget = stripTagDigest(step.Name) + "@" + digest
}

stepOutputDir := afero.GetTempDir(b.fs, fmt.Sprintf("step-%d", idx))
if err := b.fs.MkdirAll(stepOutputDir, os.FileMode(os.O_CREATE)); err != nil {
return fmt.Errorf("failed to create temp dir for step outputs: %v", err)
}
var stepOutputDir string

args := b.dockerRunArgs(path.Clean(step.Dir), stepOutputDir, idx)
for _, env := range step.Env {
Expand Down Expand Up @@ -1034,37 +1014,11 @@ func (b *Build) runStep(ctx context.Context, idx int) error {
if err := b.Runner.Run(ctx, args, nil, outWriter, errWriter, ""); err != nil {
return err
}
return b.captureStepOutput(idx, stepOutputDir)
}

func (b *Build) captureStepOutput(idx int, stepOutputDir string) error {
fn := path.Join(stepOutputDir, builderOutputFile)
if exists, _ := afero.Exists(b.fs, fn); !exists {
// Step didn't write an output file.
return nil
}

b.initStepOutputsOnce.Do(func() {
b.stepOutputs = make([][]byte, len(b.Request.Steps))
})

// Grab any outputs reported by the builder.
f, err := b.fs.Open(path.Join(stepOutputDir, builderOutputFile))
if err != nil {
log.Printf("failed to open step %d output file: %v", idx, err)
return err
}

var buf bytes.Buffer
if _, err := io.Copy(&buf, io.LimitReader(f, maxOutputBytes)); err != nil {
log.Printf("failed to read step %d output file: %v", idx, err)
return err
}

b.stepOutputs[idx] = buf.Bytes()
return nil
}


func (b *Build) runBuildSteps(ctx context.Context) error {
// Create BuildTotal TimeSpan with Start time. End time has zero value.
b.mu.Lock()
Expand Down Expand Up @@ -1216,10 +1170,6 @@ func (b *Build) dockerRunArgs(stepDir, stepOutputDir string, idx int) []string {
// Run in privileged mode per discussion in b/31267381.
"--privileged",

// Mount the step output dir.
"--volume", stepOutputDir+":"+stepOutputPath,
// Communicate the step output path to the builder via env var.
"--env", "BUILDER_OUTPUT="+stepOutputPath,
)
if !b.local {
args = append(args,
Expand Down
56 changes: 0 additions & 56 deletions build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"path"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -2574,60 +2572,6 @@ func TestWorkdir(t *testing.T) {
}
}

func TestBuildOutput(t *testing.T) {
r := newMockRunner(t, "desc")
r.dockerRunHandler = func([]string, io.Writer, io.Writer) error { return nil }

for _, c := range []struct {
contents []string
wantOutputs [][]byte
}{{
// no contents, no outputs.
contents: nil,
wantOutputs: nil,
}, {
// one step with contents.
contents: []string{"foo"},
wantOutputs: [][]byte{[]byte("foo")},
}, {
// no contents in 0th step, contents in 1st.
contents: []string{"", "foo"},
wantOutputs: [][]byte{nil, []byte("foo")},
}, {
// contents in 0th step, no contents in 1st.
contents: []string{"foo", ""},
wantOutputs: [][]byte{[]byte("foo"), nil},
}} {
fs := afero.NewMemMapFs()
req := pb.Build{}
for i := 0; i < len(c.contents); i++ {
req.Steps = append(req.Steps, &pb.BuildStep{Name: "gcr.io/my-project/my-compiler"})
}
b := New(r, req, mockTokenSource(), nopBuildLogger{}, nopEventLogger{}, "", fs, true, false, false)

for i, contents := range c.contents {
stepOutputDir := afero.GetTempDir(fs, fmt.Sprintf("step-%d", i))
if err := b.fs.MkdirAll(stepOutputDir, os.FileMode(os.O_CREATE)); err != nil {
t.Errorf("MkDirAll(%q): %v", stepOutputDir, err)
}

if contents != "" {
p := path.Join(stepOutputDir, builderOutputFile)
if err := afero.WriteFile(fs, p, []byte(contents), os.FileMode(os.O_CREATE)); err != nil {
t.Fatalf("WriteFile(%q -> %q): %v", contents, p, err)
}
}

if err := b.captureStepOutput(i, stepOutputDir); err != nil {
t.Errorf("captureBuildOutputs failed: %v", err)
}
}

if !reflect.DeepEqual(b.stepOutputs, c.wantOutputs) {
t.Errorf("Collected outputs; got %v, want %v", b.stepOutputs, c.wantOutputs)
}
}
}

type nopBuildLogger struct{}

Expand Down
9 changes: 7 additions & 2 deletions cloudbuild_tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ steps:
- '-c'
- |
find . -type d -maxdepth 1 | grep -Ev "vendor|.git|gopath|integration_tests" | xargs /builder/bin/go.bash test
# Set TAG_NAME as version.
- name: 'ubuntu'
args: ['sed', '-i', 's/HEAD/$TAG_NAME/g', 'version.go']

# Create binaries for each pair of OS/Arch.
# Create an archive with the latest binaries; that enables a single download url.
- name: 'gcr.io/cloud-builders/go:debian'
Expand All @@ -25,5 +27,8 @@ steps:
done
done
tar -czvf container-builder-local_latest.tar.gz container-builder-local_*
- name: 'gcr.io/cloud-builders/gsutil'
args: ['-m', 'cp', 'container-builder-local_*', 'gs://container-builder-local/']
artifacts:
objects:
location: 'gs://container-builder-local/'
paths: ['container-builder-local_*']

0 comments on commit 7b17d1f

Please sign in to comment.