Skip to content

Commit d22deda

Browse files
authored
Improve last green commit support (#614)
1 parent bb3bb3f commit d22deda

File tree

7 files changed

+35
-59
lines changed

7 files changed

+35
-59
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ Bazelisk currently understands the following formats for version labels:
6666
Additionally, a few special version names are supported for our official releases only (these formats do not work when using a fork):
6767
- `last_green` refers to the Bazel binary that was built at the most recent commit that passed [Bazel CI](https://buildkite.com/bazel/bazel-bazel).
6868
Ideally this binary should be very close to Bazel-at-head.
69-
- `last_downstream_green` points to the most recent Bazel binary that builds and tests all [downstream projects](https://buildkite.com/bazel/bazel-at-head-plus-downstream) successfully.
7069
- `last_rc` points to the most recent release candidate.
7170
If there is no active release candidate, Bazelisk uses the latest Bazel release instead.
7271
- `rolling` refers to the latest rolling release (even if there is a newer LTS release).
7372

73+
Note: `last_downstream_green` support has been removed, please use `last_green` instead.
74+
7475
## Where does Bazelisk get Bazel from?
7576

7677
By default Bazelisk retrieves Bazel releases, release candidates and binaries built at green commits from Google Cloud Storage. The downloaded artifacts are validated against the SHA256 value recorded in `BAZELISK_VERIFY_SHA256` if this variable is set in the configuration file.
@@ -219,7 +220,7 @@ Additionally, the Bazelisk home directory is also evaluated in precedence order.
219220

220221
For ease of use, the Python version of Bazelisk is written to work with Python 2.7 and 3.x and only uses modules provided by the standard library.
221222

222-
The Go version can be compiled to run natively on Linux, macOS and Windows.
223+
The Go version can be compiled to run natively on Linux, macOS and Windows.
223224

224225
To install it, run:
225226

bazelisk.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,10 @@
4646

4747
LATEST_PATTERN = re.compile(r"latest(-(?P<offset>\d+))?$")
4848

49-
LAST_GREEN_COMMIT_BASE_PATH = (
50-
"https://storage.googleapis.com/bazel-untrusted-builds/last_green_commit/"
49+
LAST_GREEN_COMMIT_PATH = (
50+
"https://storage.googleapis.com/bazel-builds/last_green_commit/github.com/bazelbuild/bazel.git/publish-bazel-binaries"
5151
)
5252

53-
LAST_GREEN_COMMIT_PATH_SUFFIXES = {
54-
"last_green": "github.com/bazelbuild/bazel.git/bazel-bazel",
55-
"last_downstream_green": "downstream_pipeline",
56-
}
57-
5853
BAZEL_GCS_PATH_PATTERN = (
5954
"https://storage.googleapis.com/bazel-builds/artifacts/{platform}/{commit}/bazel"
6055
)
@@ -119,9 +114,8 @@ def resolve_version_label_to_number_or_commit(bazelisk_directory, version):
119114
of an unreleased Bazel binary,
120115
2. An indicator for whether the returned version refers to a commit.
121116
"""
122-
suffix = LAST_GREEN_COMMIT_PATH_SUFFIXES.get(version)
123-
if suffix:
124-
return get_last_green_commit(suffix), True
117+
if version == "last_green":
118+
return get_last_green_commit(), True
125119

126120
if "latest" in version:
127121
match = LATEST_PATTERN.match(version)
@@ -140,8 +134,11 @@ def resolve_version_label_to_number_or_commit(bazelisk_directory, version):
140134
return version, False
141135

142136

143-
def get_last_green_commit(path_suffix):
144-
return read_remote_text_file(LAST_GREEN_COMMIT_BASE_PATH + path_suffix).strip()
137+
def get_last_green_commit():
138+
commit = read_remote_text_file(LAST_GREEN_COMMIT_PATH).strip()
139+
if not re.match(r"^[0-9a-f]{40}$", commit):
140+
raise Exception("Invalid commit hash: {}".format(commit))
141+
return commit
145142

146143

147144
def get_releases_json(bazelisk_directory):

bazelisk_test.sh

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,6 @@ function test_bazel_last_green() {
257257
(echo "FAIL: 'bazelisk version' of an unreleased binary must not print a build label."; exit 1)
258258
}
259259

260-
function test_bazel_last_downstream_green() {
261-
setup
262-
263-
USE_BAZEL_VERSION="last_downstream_green" \
264-
BAZELISK_HOME="$BAZELISK_HOME" \
265-
bazelisk version 2>&1 | tee log
266-
267-
! grep "Build label:" log || \
268-
(echo "FAIL: 'bazelisk version' of an unreleased binary must not print a build label."; exit 1)
269-
}
270-
271260
function test_BAZELISK_NOJDK() {
272261
setup
273262

@@ -516,10 +505,6 @@ echo "# test_bazel_last_green"
516505
test_bazel_last_green
517506
echo
518507

519-
echo "# test_bazel_last_downstream_green"
520-
test_bazel_last_downstream_green
521-
echo
522-
523508
echo "# test_BAZELISK_NOJDK"
524509
test_BAZELISK_NOJDK
525510
echo

core/repositories.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ type ForkRepo interface {
7070
// CommitRepo represents a repository that stores Bazel binaries built at specific commits.
7171
// It can also return the hashes of the most recent commits that passed Bazel CI pipelines successfully.
7272
type CommitRepo interface {
73-
// GetLastGreenCommit returns the most recent commit at which a Bazel binary passed a specific Bazel CI pipeline.
74-
// If downstreamGreen is true, the pipeline is https://buildkite.com/bazel/bazel-at-head-plus-downstream, otherwise
75-
// it's https://buildkite.com/bazel/bazel-bazel
76-
GetLastGreenCommit(bazeliskHome string, downstreamGreen bool) (string, error)
73+
// GetLastGreenCommit returns the most recent commit at which a Bazel binary is successfully built.
74+
GetLastGreenCommit(bazeliskHome string) (string, error)
7775

7876
// DownloadAtCommit downloads a Bazel binary built at the given commit into the specified location and returns the absolute path.
7977
DownloadAtCommit(commit, destDir, destFile string, config config.Config) (string, error)
@@ -122,7 +120,7 @@ func (r *Repositories) ResolveVersion(bazeliskHome, fork, version string, config
122120

123121
func (r *Repositories) resolveFork(bazeliskHome string, vi *versions.Info, config config.Config) (string, DownloadFunc, error) {
124122
if vi.IsRelative && (vi.IsCandidate || vi.IsCommit) {
125-
return "", nil, errors.New("forks do not support last_rc, last_green and last_downstream_green")
123+
return "", nil, errors.New("forks do not support last_rc and last_green")
126124
}
127125
lister := func(bazeliskHome string) ([]string, error) {
128126
return r.Fork.GetVersions(bazeliskHome, vi.Fork)
@@ -174,7 +172,7 @@ func (r *Repositories) resolveCommit(bazeliskHome string, vi *versions.Info, con
174172
version := vi.Value
175173
if vi.IsRelative {
176174
var err error
177-
version, err = r.Commits.GetLastGreenCommit(bazeliskHome, vi.IsDownstream)
175+
version, err = r.Commits.GetLastGreenCommit(bazeliskHome)
178176
if err != nil {
179177
return "", nil, fmt.Errorf("cannot resolve last green commit: %v", err)
180178
}
@@ -376,7 +374,7 @@ type noCommitRepo struct {
376374
err error
377375
}
378376

379-
func (nlgr *noCommitRepo) GetLastGreenCommit(bazeliskHome string, downstreamGreen bool) (string, error) {
377+
func (nlgr *noCommitRepo) GetLastGreenCommit(bazeliskHome string) (string, error) {
380378
return "", nlgr.err
381379
}
382380

repositories/gcs.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,7 @@ import (
2121
const (
2222
candidateBaseURL = "https://releases.bazel.build"
2323
nonCandidateBaseURL = "https://storage.googleapis.com/bazel-builds/artifacts"
24-
lastGreenBaseURL = "https://storage.googleapis.com/bazel-untrusted-builds/last_green_commit/"
25-
)
26-
27-
var (
28-
// key == includeDownstream
29-
lastGreenCommitPathSuffixes = map[bool]string{
30-
false: "github.com/bazelbuild/bazel.git/bazel-bazel",
31-
true: "downstream_pipeline",
32-
}
24+
lastGreenCommitURL = "https://storage.googleapis.com/bazel-builds/last_green_commit/github.com/bazelbuild/bazel.git/publish-bazel-binaries"
3325
)
3426

3527
// GCSRepo represents a Bazel repository on Google Cloud Storage that contains Bazel releases, release candidates and Bazel binaries built at arbitrary commits.
@@ -249,16 +241,20 @@ func (gcs *GCSRepo) DownloadCandidate(version, destDir, destFile string, config
249241

250242
// CommitRepo
251243

252-
// GetLastGreenCommit returns the most recent commit at which a Bazel binary passed a specific Bazel CI pipeline.
253-
// If downstreamGreen is true, the pipeline is https://buildkite.com/bazel/bazel-at-head-plus-downstream, otherwise
254-
// it's https://buildkite.com/bazel/bazel-bazel
255-
func (gcs *GCSRepo) GetLastGreenCommit(bazeliskHome string, downstreamGreen bool) (string, error) {
256-
pathSuffix := lastGreenCommitPathSuffixes[downstreamGreen]
257-
content, _, err := httputil.ReadRemoteFile(lastGreenBaseURL+pathSuffix, "")
244+
// GetLastGreenCommit returns the most recent commit at which a Bazel binary is successfully built.
245+
func (gcs *GCSRepo) GetLastGreenCommit(bazeliskHome string) (string, error) {
246+
content, _, err := httputil.ReadRemoteFile(lastGreenCommitURL, "")
258247
if err != nil {
259248
return "", fmt.Errorf("could not determine last green commit: %v", err)
260249
}
261-
return strings.TrimSpace(string(content)), nil
250+
251+
// Validate the content does look like a commit hash
252+
commit := strings.TrimSpace(string(content))
253+
if !versions.MatchCommitPattern(commit) {
254+
return "", fmt.Errorf("invalid commit hash: %s", commit)
255+
}
256+
257+
return commit, nil
262258
}
263259

264260
// DownloadAtCommit downloads a Bazel binary built at the given commit into the specified location and returns the absolute path.

test.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ env -u USE_BAZEL_VERSION ./bin/bazelisk-darwin-"$arch" version
99
USE_BAZEL_VERSION="latest" ./bin/bazelisk-darwin-"$arch" version
1010
USE_BAZEL_VERSION="0.28.0" ./bin/bazelisk-darwin-amd64 version
1111
USE_BAZEL_VERSION="last_green" ./bin/bazelisk-darwin-"$arch" version
12-
USE_BAZEL_VERSION="last_downstream_green" ./bin/bazelisk-darwin-"$arch" version
1312
USE_BAZEL_VERSION="last_rc" ./bin/bazelisk-darwin-"$arch" version
1413
USE_BAZEL_VERSION="bazelbuild/latest" ./bin/bazelisk-darwin-"$arch" version
1514
USE_BAZEL_VERSION="bazelbuild/0.27.0" ./bin/bazelisk-darwin-amd64 version

versions/versions.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727

2828
// Info represents a structured Bazel version identifier.
2929
type Info struct {
30-
IsRelease, IsCandidate, IsCommit, IsFork, IsRolling, IsRelative, IsDownstream bool
30+
IsRelease, IsCandidate, IsCommit, IsFork, IsRolling, IsRelative bool
3131
Fork, Value string
3232
LatestOffset, TrackRestriction int
3333
}
@@ -68,10 +68,6 @@ func Parse(fork, version string) (*Info, error) {
6868
} else if version == "last_green" {
6969
vi.IsCommit = true
7070
vi.IsRelative = true
71-
} else if version == "last_downstream_green" {
72-
vi.IsCommit = true
73-
vi.IsRelative = true
74-
vi.IsDownstream = true
7571
} else if rollingPattern.MatchString(version) {
7672
vi.IsRolling = true
7773
} else if version == "rolling" {
@@ -106,7 +102,11 @@ func GetInAscendingOrder(versions []string) []string {
106102
return sorted
107103
}
108104

105+
func MatchCommitPattern(version string) bool {
106+
return commitPattern.MatchString(version)
107+
}
108+
109109
// IsCommit returns whether the given version refers to a commit.
110110
func IsCommit(version string) bool {
111-
return version == "last_green" || version == "last_downstream_green" || commitPattern.MatchString(version)
111+
return version == "last_green" || commitPattern.MatchString(version)
112112
}

0 commit comments

Comments
 (0)