Skip to content
This repository was archived by the owner on Jan 29, 2019. It is now read-only.

Commit 783235e

Browse files
authored
Replace publishing rules with ghrelease rules (#1)
* Impl proper version incrementing * Impl changelog generator * Impl docs publisher * Impl check_local_tracks_authoritative_branch * Remove k8s repo * Migrate to new publisher * Update example docs
1 parent 76421a3 commit 783235e

37 files changed

+1130
-1719
lines changed

WORKSPACE

-10
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ git_repository(
1111
remote = "[email protected]:bazelbuild/rules_docker.git",
1212
)
1313

14-
git_repository(
15-
name = "io_bazel_rules_k8s",
16-
commit = "d6e1b65317246fe044482f9e042556c77e6893b8",
17-
remote = "[email protected]:bazelbuild/rules_k8s.git",
18-
)
19-
2014
load("@io_bazel_rules_docker//container:container.bzl", "repositories")
2115

2216
repositories()
@@ -28,10 +22,6 @@ load(
2822

2923
_py_image_repos()
3024

31-
load("@io_bazel_rules_k8s//k8s:k8s.bzl", "k8s_defaults", "k8s_repositories")
32-
33-
k8s_repositories()
34-
3525
load("//terraform:dependencies.bzl", "terraform_repositories")
3626

3727
terraform_repositories()

examples/README.md

+36-20
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,46 @@ build:publish --define IMAGE_CHROOT=index.docker.io/netchris
9393

9494
### [`release/BUILD`](release/BUILD)
9595
```python
96-
load("//terraform:def.bzl", "terraform_module_publisher")
96+
load("//terraform/experimental/ghrelease:def.bzl", "ghrelease", "ghrelease_assets", "ghrelease_test_suite")
97+
98+
VERSION = "0.2"
9799

98100
# To make our terraform module available to others we configure a
99-
# 'terraform_module_publisher' which will
100-
# - Run configured tests
101-
# - Publish relevant docker images
102-
# - Output each module into its own subdirectory in this repo (note
103-
# that we can optionally output to a different repo)
104-
terraform_module_publisher(
105-
name = "publish",
106-
bazelrc_config = "publish",
107-
prepublish_tests = [
108-
# bazel excludes all tests tagged as 'manual' from wildcard
109-
# patterns, so we explicitly include our e2e test.
110-
"//...", # <- means 'all tests' (which aren't tagged 'manual')
111-
"//examples/test:e2e_integration_test",
101+
# 'ghrelease' which will:
102+
# - Run "preflight-checks" (dependant test suites, etc)
103+
# - Push associated docker images
104+
# - Generate release notes, including a changelog from the previous
105+
# version
106+
# - Increment the current tag/version's patch version (or prerelease
107+
# version if the `--prerelease` flag is used)
108+
# - Create a new GitHub Release with release notes and any extra docs
109+
# - Attach any assets to the new Release
110+
ghrelease(
111+
name = "release",
112+
version = VERSION,
113+
deps = [
114+
":prerelease-tests",
115+
":tf-modules",
116+
],
117+
)
118+
119+
ghrelease_assets(
120+
name = "tf-modules",
121+
bazel_flags = ["--config=publish"],
122+
data = [
123+
"//examples/src:hello-world_ecs",
124+
"//examples/src:hello-world_k8s",
112125
],
113-
published_modules = {
114-
"mymodule": "//examples/src:hello-world_k8s",
115-
"mymodule-ecs": "//examples/src:hello-world_ecs",
116-
},
117-
# remote = "[email protected]:my-org-terraform-modules/terraform-myproject-modules.git",
118-
# remote_path = "modules",
119126
)
127+
128+
ghrelease_test_suite(
129+
name = "prerelease-tests",
130+
tests = [
131+
"//examples/...",
132+
"//examples/test:k8s-e2e_integration_test",
133+
],
134+
)
135+
120136
```
121137

122138
Check out the [release directory](release/mymodule) to see the published module!

examples/release/BUILD

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
load("//terraform:def.bzl", "terraform_module_publisher")
1+
load("//terraform/experimental/ghrelease:def.bzl", "ghrelease", "ghrelease_assets", "ghrelease_test_suite")
22

3-
terraform_module_publisher(
4-
name = "publish",
5-
bazelrc_config = "publish",
6-
prepublish_tests = [
3+
VERSION = "0.2"
4+
5+
ghrelease(
6+
name = "release",
7+
args = ["--draft"],
8+
version = VERSION,
9+
deps = [
10+
":prerelease-tests",
11+
":tf-modules",
12+
],
13+
)
14+
15+
ghrelease_assets(
16+
name = "tf-modules",
17+
bazel_flags = ["--config=publish"],
18+
data = [
19+
"//examples/src:hello-world_ecs",
20+
"//examples/src:hello-world_k8s",
21+
],
22+
)
23+
24+
ghrelease_test_suite(
25+
name = "prerelease-tests",
26+
tests = [
727
"//...",
828
"//examples/test:k8s-e2e_integration_test",
9-
#"//examples/test:ecs-e2e_integration_test",
29+
# "//examples/test:ecs-e2e_integration_test"
1030
],
11-
published_modules = {
12-
"mymodule": "//examples/src:hello-world_k8s",
13-
"mymodule-ecs": "//examples/src:hello-world_ecs",
14-
},
1531
)

examples/src/BUILD

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//terraform:def.bzl", "terraform_module", "terraform_workspace")
1+
load("//terraform:def.bzl", "terraform_module")
22
load("//terraform:container.bzl", "image_embedder", "terraform_k8s_manifest")
33
load("@io_bazel_rules_docker//python:image.bzl", "py_image")
44

@@ -32,7 +32,6 @@ terraform_module(
3232
"k8s.tf",
3333
"main.tf",
3434
],
35-
description = "A hello-world service that is designed to run on Kubernetes",
3635
embed = [":k8s-deployment"],
3736
visibility = ["//visibility:public"],
3837
)
@@ -44,7 +43,6 @@ terraform_module(
4443
"ecs-plumbing.tf",
4544
"main.tf",
4645
],
47-
description = "A hello-world service that is designed to run on ECS",
4846
embed = [":ecs-task-containers"],
4947
visibility = ["//visibility:public"],
5048
)

examples/src/ecs.tf

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* A hello-world service that is designed to run on ECS!
3+
*/
14
variable replicas {
25
description = "Desired # of ECS replicas"
36
default = 1

examples/src/k8s.tf

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
1+
/**
2+
* A hello-world service that is designed to run on Kubernetes
3+
*/
24
data kubectl_namespace current {}
35

46
resource kubernetes_config_map hello_world_server {

terraform/container.bzl

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
load("//terraform:providers.bzl", _ModuleInfo = "ModuleInfo")
2-
load("//terraform/internal:k8s.bzl", _terraform_k8s_manifest = "terraform_k8s_manifest")
3-
load("//terraform/internal:image_embedder.bzl", _image_publisher = "image_publisher")
2+
load("//terraform/internal:k8s_manifest.bzl", "terraform_k8s_manifest")
3+
load("//terraform/internal:image_embedder.bzl", "image_publisher")
44
load(
55
"//terraform/internal:image_embedder_lib.bzl",
66
_embed_images = "embed_images",
77
_image_embedder_attrs = "image_embedder_attrs",
88
)
99

10-
terraform_k8s_manifest = _terraform_k8s_manifest
11-
image_publisher = _image_publisher
12-
1310
def _image_embedder_impl(ctx):
1411
providers = []
1512

terraform/def.bzl

+4-94
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,4 @@
1-
load(
2-
"//terraform/internal:terraform.bzl",
3-
"terraform_plugin",
4-
_terraform_module = "terraform_module",
5-
_terraform_workspace = "terraform_workspace",
6-
)
7-
load("//terraform/internal:test.bzl", "terraform_integration_test")
8-
load(
9-
"//terraform/internal:distribution.bzl",
10-
"terraform_distribution_publisher",
11-
"terraform_module_publisher",
12-
_terraform_distribution_dir = "terraform_distribution_dir",
13-
)
14-
load("//terraform:providers.bzl", "tf_workspace_files_prefix")
15-
16-
def terraform_distribution_dir(name, deps, **kwargs):
17-
srcs_name = "%s.srcs-list" % name
18-
module_name = "%s.module" % name
19-
20-
# change "relative" deps to absolute deps
21-
deps_abs = [
22-
"//" + native.package_name() + dep if dep.startswith(":") else dep
23-
for dep in deps
24-
]
25-
native.genquery(
26-
name = srcs_name,
27-
opts = ["--noimplicit_deps"],
28-
expression = """kind("source file", deps(set(%s)))""" % " ".join(deps_abs),
29-
scope = deps_abs,
30-
)
31-
32-
terraform_module(
33-
name = module_name,
34-
deps = deps_abs,
35-
)
36-
37-
_terraform_distribution_dir(
38-
name = name,
39-
srcs_list = ":" + srcs_name,
40-
module = ":" + module_name,
41-
**kwargs
42-
)
43-
44-
def _flip_modules_attr(modules):
45-
"""
46-
Translate modules attr from a 'name=>label' dict to 'label=>name'
47-
"""
48-
flipped = {}
49-
for name, label in modules.items():
50-
if not (label.startswith("@") or label.startswith("//") or label.startswith(":")):
51-
fail("Modules are now specified as 'name=>label'", attr="modules")
52-
# append package path & workspace name as necessary
53-
abs_label = "//" + native.package_name() + label if label.startswith(":") else label
54-
abs_label = native.repository_name() + abs_label if abs_label.startswith("//") else abs_label
55-
if abs_label in flipped:
56-
fail("Modules may only be specified once (%s)" % label, attr = "modules")
57-
flipped[abs_label] = name
58-
return flipped
59-
60-
def terraform_module(name, modules = {}, **kwargs):
61-
_terraform_module(
62-
name = name,
63-
modules = _flip_modules_attr(modules),
64-
**kwargs
65-
)
66-
67-
def terraform_workspace(name, modules = {}, **kwargs):
68-
_terraform_workspace(
69-
name = name,
70-
modules = _flip_modules_attr(modules),
71-
**kwargs
72-
)
73-
74-
# create a convenient destroy target which
75-
# CDs to the package dir and runs terraform destroy
76-
native.genrule(
77-
name = "%s.destroy" % name,
78-
outs = ["%s.destroy.sh" % name],
79-
cmd = """echo '
80-
#!/bin/sh
81-
set -eu
82-
tf_workspace_dir="$$BUILD_WORKSPACE_DIRECTORY/{package}/{tf_workspace_files_prefix}"
83-
if [ -e "$$tf_workspace_dir" ]; then
84-
cd "$$tf_workspace_dir"
85-
exec terraform destroy "$$@" .terraform/tfroot
86-
else
87-
>&2 echo "Could not find terraform workspace dir, so there is nothing to destroy ($$tf_workspace_dir)"
88-
fi
89-
' > $@""".format(
90-
package = native.package_name(),
91-
tf_workspace_files_prefix = tf_workspace_files_prefix(name),
92-
),
93-
executable = True,
94-
)
1+
load("//terraform/internal:terraform.bzl", "terraform_plugin")
2+
load("//terraform/internal:workspace.bzl", "terraform_workspace")
3+
load("//terraform/internal:module.bzl", "terraform_module")
4+
load("//terraform/internal:integration_test.bzl", "terraform_integration_test")

terraform/dependencies.bzl

+17-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _EXTERNAL_BINARIES = {
2727
"hub": dict(
2828
url = "https://github.com/github/hub/releases/download/v{version}/hub-{platform}-amd64-{version}.tgz",
2929
path = "hub-{platform}-amd64-{version}/bin/hub",
30-
version = "2.5.1",
30+
version = "2.6.0",
3131
),
3232
}
3333

@@ -52,8 +52,21 @@ py_library(
5252
visibility = ["//visibility:public"],
5353
)""",
5454
sha256 = "592766c6303207a20efc445587778322d7f73b161bd994f227adaa341ba212ab",
55-
url = ("https://pypi.python.org/packages/4a/85/" +
56-
"db5a2df477072b2902b0eb892feb37d88ac635d36245a72a6a69b23b383a" +
57-
"/PyYAML-3.12.tar.gz"),
55+
url = ("https://pypi.python.org/packages/4a/85/db5a2df477072b2902b0eb892feb37d88ac635d36245a72a6a69b23b383a/PyYAML-3.12.tar.gz"),
5856
strip_prefix = "PyYAML-3.12/lib/yaml",
5957
)
58+
if "py_semver" not in native.existing_rules():
59+
native.new_http_archive(
60+
name = "py_semver",
61+
build_file_content = """
62+
py_library(
63+
name = "py_semver",
64+
srcs = glob(["*.py"]),
65+
visibility = ["//visibility:public"],
66+
imports = ["semver"],
67+
)
68+
""",
69+
sha256 = "5b09010a66d9a3837211bb7ae5a20d10ba88f8cb49e92cb139a69ef90d5060d8",
70+
url = "https://files.pythonhosted.org/packages/47/13/8ae74584d6dd33a1d640ea27cd656a9f718132e75d759c09377d10d64595/semver-2.8.1.tar.gz",
71+
strip_prefix = "semver-2.8.1",
72+
)

terraform/experimental/ghrelease/BUILD

Whitespace-only changes.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//terraform/experimental/ghrelease/internal:publisher.bzl", "ghrelease")
2+
load("//terraform/experimental/ghrelease/internal:assets.bzl", "ghrelease_assets")
3+
load("//terraform/experimental/ghrelease/internal:test_suite.bzl", "ghrelease_test_suite")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[py_binary(
2+
name = f[:-len(".py")],
3+
srcs = [f],
4+
visibility = ["//visibility:public"],
5+
deps = [":lib"],
6+
) for f in glob(["*_runner.py"])]
7+
8+
py_library(
9+
name = "lib",
10+
srcs = ["lib.py"],
11+
imports = [
12+
"rules_terraform/terraform/experimental/ghrelease/internal",
13+
],
14+
deps = [
15+
"@py_semver",
16+
],
17+
)
18+
19+
py_test(
20+
name = "test",
21+
size = "small",
22+
srcs = ["test.py"],
23+
deps = [":lib"],
24+
)

0 commit comments

Comments
 (0)