Skip to content

Commit

Permalink
look for version in bazel-bin
Browse files Browse the repository at this point in the history
bazel-bin and bazel-genfiles have pointed to the same location since
0.25. -genfiles is deprecated.

This was test running:

../release/push-build.sh --release-type=devel \
  --bucket=kubernetes-release-mikedanese --private-bucket --nomock \
  --version-suffix=mikedanese

With bazel version 2.2.0 and 0.23.2.

Issue: bazelbuild/bazel#8651
  • Loading branch information
mikedanese committed Mar 24, 2020
1 parent ec90dee commit e41da78
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
9 changes: 8 additions & 1 deletion pkg/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package release
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -47,7 +48,7 @@ const (
versionDirtyRE = `(-dirty)`
dockerBuildPath = "_output/release-tars"
bazelBuildPath = "bazel-bin/build/release-tars"
bazelVersionPath = "bazel-genfiles/version"
bazelVersionPath = "bazel-bin/version"
dockerVersionPath = "kubernetes/version"
kubernetesTar = "kubernetes.tar.gz"

Expand Down Expand Up @@ -131,6 +132,12 @@ func BuiltWithBazel(workDir string) (bool, error) {
// ReadBazelVersion reads the version from a Bazel build.
func ReadBazelVersion(workDir string) (string, error) {
version, err := ioutil.ReadFile(filepath.Join(workDir, bazelVersionPath))
if os.IsNotExist(err) {
// The check for version in bazel-genfiles can be removed once everyone is
// off of versions before 0.25.0.
// https://github.com/bazelbuild/bazel/issues/8651
version, err = ioutil.ReadFile(filepath.Join(workDir, "bazel-genfiles/version"))
}
return string(version), err
}

Expand Down
51 changes: 30 additions & 21 deletions pkg/release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,40 +204,33 @@ func TestBuiltWithBazel(t *testing.T) {
}

func TestReadBazelVersion(t *testing.T) {
baseTmpDir, err := ioutil.TempDir("", "")
require.Nil(t, err)

version := "1.1.1"

// Build directories.
require.Nil(t, os.MkdirAll(filepath.Join(baseTmpDir, "bazel-genfiles"), os.ModePerm))

bazelVersionFile := filepath.Join(baseTmpDir, bazelVersionPath)
require.Nil(t, ioutil.WriteFile(
bazelVersionFile,
[]byte(version),
os.FileMode(0644),
))

defer cleanupTmps(t, baseTmpDir)
const version = "1.1.1"

type want struct {
r string
rErr bool
}

cases := map[string]struct {
path string
want want
outdir string
want want
}{
"ReadLegacyVersion": {
outdir: "bazel-genfiles",
want: want{
r: version,
rErr: false,
},
},
"ReadVersion": {
path: baseTmpDir,
outdir: "bazel-bin",
want: want{
r: version,
rErr: false,
},
},
"ReadVersionError": {
path: "notadir",
outdir: "bazel-random",
want: want{
rErr: true,
},
Expand All @@ -246,7 +239,23 @@ func TestReadBazelVersion(t *testing.T) {

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res, err := ReadBazelVersion(tc.path)
baseTmpDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
defer cleanupTmps(t, baseTmpDir)

// Build directories.
require.Nil(t, os.MkdirAll(filepath.Join(baseTmpDir, tc.outdir), os.ModePerm))

bazelVersionFile := filepath.Join(baseTmpDir, tc.outdir, "version")
require.Nil(t, ioutil.WriteFile(
bazelVersionFile,
[]byte(version),
os.FileMode(0644),
))

res, err := ReadBazelVersion(baseTmpDir)
require.Equal(t, tc.want.rErr, err != nil)
require.Equal(t, tc.want.r, res)
})
Expand Down
9 changes: 8 additions & 1 deletion push-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ KUBE_ROOT=$(pwd -P)
USE_BAZEL=false
if release::was_built_with_bazel $KUBE_ROOT $FLAGS_release_kind; then
USE_BAZEL=true
LATEST=$(cat $KUBE_ROOT/bazel-genfiles/version)
# The check for version in bazel-genfiles can be removed once everyone is off
# of versions before 0.25.0.
# https://github.com/bazelbuild/bazel/issues/8651
if [[ -r "$KUBE_ROOT/bazel-genfiles/version" ]]; then
LATEST=$(cat $KUBE_ROOT/bazel-genfiles/version)
else
LATEST=$(cat $KUBE_ROOT/bazel-bin/version)
fi
else
LATEST=$(tar -O -xzf $KUBE_ROOT/_output/release-tars/$FLAGS_release_kind.tar.gz $FLAGS_release_kind/version)
fi
Expand Down

0 comments on commit e41da78

Please sign in to comment.