Skip to content

Commit

Permalink
Merge pull request #319 from Zemnmez/version-pipelining
Browse files Browse the repository at this point in the history
version pipelining
  • Loading branch information
Zemnmez authored Jun 6, 2022
2 parents bea0d27 + e41265d commit 5154c1c
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ try-import %workspace%/.bazelrc.user

build --action_env=AWS_ACCESS_KEY_ID --action_env=AWS_SECRET_ACCESS_KEY --action_env=PULUMI_ACCESS_TOKEN
test --action_env=PULUMI_ACCESS_TOKEN

# be careful with this!
test --test_tag_filters=-do_not_run_on_main
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set PR to merge automatically
run: gh pr merge --auto --merge --head main --base versioned
run: gh pr merge versioned --auto

2 changes: 1 addition & 1 deletion bzl/versioning/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports_files(
[
"version.tmpl.txt"
"bump.py"
],
visibility = ["//visibility:public"],
)
Expand Down
35 changes: 35 additions & 0 deletions bzl/versioning/bump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import argparse
import os
import subprocess
import shutil

parser = argparse.ArgumentParser(description="Performs the action of a version bump.")
parser.add_argument('--to_bump_in', help="The version file to bump, as a root-relative path.", type=str)
parser.add_argument('--to_bump_out', help="The version file to bump, as a root-relative path.", type=str)
parser.add_argument('--lockfile_build_label', help="A label that points to the generated (new) version lockfile.", type=str)
parser.add_argument('--lockfile_build_rootpath', help="The path from the repo root that the lockfile is generated into", type=str)
parser.add_argument('--lockfile_out_rootpath', help="The location to place the newly minted version lockfile at.", type=str)

# This happens directly on the real workspace -- also, needs to be
# run from bazel to have this set.
os.chdir(os.environ.get('BUILD_WORKSPACE_DIRECTORY'))

args = parser.parse_args()

number = 0

with open(args.to_bump_in, mode='r', encoding='utf-8') as f:
number = int(f.read())


with open(args.to_bump_out, mode='w', encoding='utf-8') as f:
f.write(str(number+1))

# Once the version has been bumped, generate the new version bump file.
subprocess.run(["bazelisk", "build", args.lockfile_build_label])

# Copy the newly created lockfile across
shutil.copyfile(
os.path.join("dist", "bin", args.lockfile_build_rootpath),
args.lockfile_out_rootpath
)
49 changes: 49 additions & 0 deletions bzl/versioning/rules.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
load("//bzl/hash:rules.bzl", "hashes")
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
load("@rules_python//python:defs.bzl", "py_binary")

def semver_version(name, **kwargs):
_semver_version(
Expand Down Expand Up @@ -34,3 +37,49 @@ _semver_version = rule(
"output": attr.output(mandatory = True)
}
)

def _absolute_label(label):
if label.startswith('@') or label.startswith('/'):
return label
if label.startswith(':'):
return native.repository_name() + '//' + native.package_name() + label
return native.repository_name() + '//' + native.package_name() + ':' + label

def bump_on_change_test(name, srcs = [], version_lock = None, version = None, run_on_main = False):
tags = []

if not run_on_main:
tags = [ 'do_not_run_on_main' ]

hashes_name = name + "_version_lock_validator"
hashes(
name = hashes_name,
srcs = srcs + [ version ],
)

generated_file_test(
name = name,
generated = hashes_name,
src = version_lock,
tags = tags + [ 'version_check' ]
)

py_binary(
name = name + ".bump",
srcs = [ "//bzl/versioning:bump.py" ],
main = "//bzl/versioning:bump.py",
data = [ version, hashes_name, version_lock ],
args = [
"--to_bump_in", "$(rootpath " + version + ")",
"--to_bump_out", "$(rootpath " + version + ")",
"--lockfile_build_label", _absolute_label(hashes_name),
"--lockfile_build_rootpath", "$(rootpath " + hashes_name + ")",
"--lockfile_out_rootpath", "$(rootpath " + version_lock + ")"
]
)






9 changes: 9 additions & 0 deletions bzl/versioning/test/bump_on_change_test/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("//bzl/versioning:rules.bzl", "bump_on_change_test")

bump_on_change_test(
name = "version",
srcs = [ "contents.txt" ],
version_lock = "version.lock",
version = "MAJOR",
run_on_main = True
)
1 change: 1 addition & 0 deletions bzl/versioning/test/bump_on_change_test/MAJOR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7
1 change: 1 addition & 0 deletions bzl/versioning/test/bump_on_change_test/contents.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is some content of our program
2 changes: 2 additions & 0 deletions bzl/versioning/test/bump_on_change_test/version.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
040e04c69d4995ce0f768474f63263ccc87c3670b137516fcc1213a8b0f29406 bzl/versioning/test/bump_on_change_test/contents.txt
7902699be42c8a8e46fbbb4501726517e86b22c56a189f7625a6da49081b2451 bzl/versioning/test/bump_on_change_test/MAJOR
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5154c1c

Please sign in to comment.