Add files to build site directly on Google Cloud Container Builder
Using `bazel build //site` will now build the site (needs the proper
version of Jekyll in the path :/), `bazel run //site` will serve it
using Jekyll.
This change also contains the yaml file to build and deploy the
site on Google Cloud Container Builder. This will allow to remove
that special hook from our CI.
Note: this is copied from https://github.com/bazelbuild/bazel-blog
Change-Id: I6bb04fea0fa80623bd5d25a5f191ae49e8074e92
PiperOrigin-RevId: 174459256
diff --git a/scripts/docs/BUILD b/scripts/docs/BUILD
index 53452e2..f3bb798 100644
--- a/scripts/docs/BUILD
+++ b/scripts/docs/BUILD
@@ -15,3 +15,5 @@
visibility = ["//site:__pkg__"],
deps = [":dot-converter"],
)
+
+exports_files(["jekyll_build.sh.tpl"])
diff --git a/scripts/docs/Dockerfile b/scripts/docs/Dockerfile
new file mode 100644
index 0000000..6ee4e5a
--- /dev/null
+++ b/scripts/docs/Dockerfile
@@ -0,0 +1,2 @@
+FROM gcr.io/cloud-builders/bazel
+RUN apt-get upgrade && apt-get install -y jekyll
diff --git a/scripts/docs/cloudbuild.yaml b/scripts/docs/cloudbuild.yaml
new file mode 100644
index 0000000..5bd1214
--- /dev/null
+++ b/scripts/docs/cloudbuild.yaml
@@ -0,0 +1,11 @@
+steps:
+- name: gcr.io/cloud-builders/docker
+ args: ['build', '--tag=gcr.io/$PROJECT_ID/bazel-jekyll', 'scripts']
+- name: gcr.io/$PROJECT_ID/bazel-jekyll
+ args: ['build', '//site']
+- name: gcr.io/cloud-builders/gsutil
+ args: ['-m', 'rsync', '-r', '-c', '-d', '/workspace/bazel-bin/site/site-build', 'gs://docs.bazel.build']
+- name: gcr.io/cloud-builders/gsutil
+ args: ['web', 'set', '-m', 'index.html', '-e', '404.html', 'gs://docs.bazel.build']
+- name: gcr.io/cloud-builders/gsutil
+ args: ['-m', 'acl', 'ch', '-R', '-u', 'AllUsers:R', 'gs://docs.bazel.build']
diff --git a/scripts/docs/jekyll.bzl b/scripts/docs/jekyll.bzl
new file mode 100644
index 0000000..3552f4a
--- /dev/null
+++ b/scripts/docs/jekyll.bzl
@@ -0,0 +1,83 @@
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Quick rule to build a Jekyll site."""
+
+def _bucket_from_workspace_name(wname):
+ """Try to assert the bucket name from the workspace name.
+
+ E.g. it will answer www.bazel.build if the workspace name is build_bazel_www.
+
+ Args:
+ wname: workspace name
+
+ Returns:
+ the guessed name of the bucket for this workspace.
+ """
+ revlist = []
+ for part in wname.split("_"):
+ revlist.insert(0, part)
+ return ".".join(revlist)
+
+def _impl(ctx):
+ """Quick and non-hermetic rule to build a Jekyll site."""
+ source = ctx.actions.declare_directory(ctx.attr.name + "-srcs")
+ output = ctx.actions.declare_directory(ctx.attr.name + "-build")
+
+ ctx.actions.run_shell(inputs = ctx.files.srcs,
+ outputs = [source],
+ command = ("mkdir -p %s\n" % (source.path)) +
+ "\n".join([
+ "tar xf %s -C %s" % (src.path, source.path) for src in ctx.files.srcs])
+ )
+ ctx.actions.run(
+ inputs = [source],
+ outputs = [output],
+ executable = "jekyll",
+ use_default_shell_env = True,
+ arguments = ["build", "-q", "-s", source.path, "-d", output.path]
+ )
+ ctx.actions.run(
+ inputs = [output],
+ outputs = [ctx.outputs.out],
+ executable = "tar",
+ arguments = ["cf", ctx.outputs.out.path, "-C", output.path, "."]
+ )
+
+ # Create a shell script to serve the site locally or push with the --push
+ # flag.
+ bucket = ctx.attr.bucket if ctx.attr.bucket else _bucket_from_workspace_name(ctx.workspace_name)
+
+ ctx.actions.expand_template(
+ template=ctx.file._jekyll_build_tpl,
+ output=ctx.outputs.executable,
+ substitutions={
+ "%{workspace_name}": ctx.workspace_name,
+ "%{source_dir}": source.short_path,
+ "%{prod_dir}": output.short_path,
+ "%{bucket}": bucket,
+ },
+ is_executable=True)
+ return [DefaultInfo(runfiles=ctx.runfiles(files=[source, output]))]
+
+jekyll_build = rule(
+ implementation = _impl,
+ executable = True,
+ attrs = {
+ "srcs": attr.label_list(allow_empty=False),
+ "bucket": attr.string(),
+ "_jekyll_build_tpl": attr.label(
+ default=":jekyll_build.sh.tpl",
+ allow_files=True,
+ single_file=True)},
+ outputs = {"out": "%{name}.tar"})
diff --git a/scripts/docs/jekyll_build.sh.tpl b/scripts/docs/jekyll_build.sh.tpl
new file mode 100644
index 0000000..0528794
--- /dev/null
+++ b/scripts/docs/jekyll_build.sh.tpl
@@ -0,0 +1,48 @@
+#!/bin/sh
+HOST="${HOST-localhost}"
+PORT="${PORT-12345}"
+RUNFILES=$(cd ${JAVA_RUNFILES-$0.runfiles}/%{workspace_name} && pwd -P)
+SOURCE_DIR="$RUNFILES/%{source_dir}"
+prod_dir="$RUNFILES/%{prod_dir}"
+bucket="%{bucket}"
+
+function serve() {
+ TDIR=$(mktemp -d)
+ RDIR=$(mktemp -d)
+ trap "rm -fr $RDIR $TDIR" EXIT
+ (cd $RDIR && \
+ jekyll serve --host "$HOST" --port "$PORT" -s "$SOURCE_DIR" -d "$TDIR")
+}
+
+function push() {
+ # Get gsutil
+ gs="${GSUTIL:-$(which gsutil 2>/dev/null || : )}"
+ if [ ! -x "${gs}" ]; then
+ echo "Please set GSUTIL to the path the gsutil binary." >&2
+ echo "gsutil (https://cloud.google.com/storage/docs/gsutil/) is the" >&2
+ echo "command-line interface to google cloud." >&2
+ exit 1
+ fi
+
+ # Rsync:
+ # -r: recursive
+ # -c: compute checksum even though the input is from the filesystem
+ # -d: remove deleted files
+ cd "${prod_dir}"
+ "${gs}" -m rsync -r -c -d . "gs://${bucket}"
+ "${gs}" web set -m index.html -e 404.html "gs://${bucket}"
+ "${gs}" -m acl ch -R -u AllUsers:R "gs://${bucket}"
+}
+
+case "${1-}" in
+ --push)
+ push
+ ;;
+ --serve|"")
+ serve
+ ;;
+ *)
+ echo "Usage: $0 [--push|--serve]" >&2
+ exit 1
+ ;;
+esac
diff --git a/site/BUILD b/site/BUILD
index fde005a..84cf645 100644
--- a/site/BUILD
+++ b/site/BUILD
@@ -1,4 +1,5 @@
load("//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
+load("//scripts/docs:jekyll.bzl", "jekyll_build")
exports_files(
[
@@ -145,3 +146,9 @@
"jekyll-tree.sh",
],
)
+
+jekyll_build(
+ name = "site",
+ srcs = [":jekyll-tree"],
+ bucket = "docs.bazel.build",
+)