Skip to content

Commit

Permalink
chore(ci): add buildifier to lint bazel files (#9909)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayocream authored Dec 8, 2022
1 parent fdfaf9e commit a59deae
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/buildifier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Buildifier

on:
pull_request:
paths:
- '**/*.bzl'
- '**/*.bazel'
- 'BUILD*'
- 'WORKSPACE*'
push:
paths:
- '**/*.bzl'
- '**/*.bazel'
- 'BUILD*'
- 'WORKSPACE*'
branches:
- master
- release/*

jobs:

autoformat:
name: Auto-format and Check
runs-on: ubuntu-22.04

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Install Dependencies
run: |
sudo wget -O /bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/5.1.0/buildifier-linux-amd64
sudo chmod +x /bin/buildifier
- name: Run buildifier
run: |
buildifier -mode=fix $(find . -name 'BUILD*' -o -name 'WORKSPACE*' -o -name '*.bzl' -o -name '*.bazel' -type f)
- name: Verify buildifier
shell: bash
run: |
# From: https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/
# This is to leverage this workaround:
# https://github.com/actions/toolkit/issues/193#issuecomment-605394935
function urlencode() {
sed ':begin;$!N;s/\n/%0A/;tbegin'
}
if [[ $(git diff-index --name-only HEAD --) ]]; then
for x in $(git diff-index --name-only HEAD --); do
echo "::error file=$x::Please run buildifier.%0A$(git diff $x | urlencode)"
done
echo "${{ github.repository }} is out of style. Please run buildifier."
exit 1
fi
echo "${{ github.repository }} is formatted correctly."
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ kong_directory_genrule(
srcs = [
":kong",
],
output_dir = "pkg",
cmd = """
export KONG_VERSION=`bash scripts/grep-kong-version.sh`;
nfpm pkg -f build/package/nfpm.yaml -p $PACKAGE_TYPE -t $GENRULE_OUTPUT_DIR
""",
output_dir = "pkg",
visibility = ["//visibility:public"],
)
5 changes: 4 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
workspace(name = "kong")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "bazel_skylib",
sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz",
],
sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506",
)

load("//build:kong_bindings.bzl", "load_bindings")

load_bindings(name = "kong_bindings")

load("//build/openresty:repositories.bzl", "openresty_repositories")

openresty_repositories()
21 changes: 16 additions & 5 deletions build/kong_bindings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ def _load_vars(ctx):
# Read env from .requirements
requirements = ctx.read(Label("@kong//:.requirements"))
content = ctx.execute(["bash", "-c", "echo '%s' | " % requirements +
"""grep -E '^(\\w*)=(.+)$' | sed -E 's/^(.*)=(.*)$/"\\1": "\\2",/'"""]).stdout
"""grep -E '^(\\w*)=(.+)$' | sed -E 's/^(.*)=(.*)$/"\\1": "\\2",/'"""]).stdout
content = content.replace('""', '"')

# Workspace path
content += '"WORKSPACE_PATH": "%s",' % ctx.path(Label("@//:WORKSPACE")).dirname

# Local env
# Temporarily fix for https://github.com/bazelbuild/bazel/issues/14693#issuecomment-1079006291
for key in ["PATH", "INSTALL_PATH", "DOWNLOAD_ROOT",
"LUAROCKS_DESTDIR", "OPENRESTY_DESTDIR", "OPENSSL_DESTDIR",
"OPENRESTY_PREFIX", "OPENRESTY_RPATH", "OPENSSL_PREFIX", "LUAROCKS_PREFIX",
"PACKAGE_TYPE", "SSL_PROVIDER", "GITHUB_TOKEN"]:
for key in [
"PATH",
"INSTALL_PATH",
"DOWNLOAD_ROOT",
"LUAROCKS_DESTDIR",
"OPENRESTY_DESTDIR",
"OPENSSL_DESTDIR",
"OPENRESTY_PREFIX",
"OPENRESTY_RPATH",
"OPENSSL_PREFIX",
"LUAROCKS_PREFIX",
"PACKAGE_TYPE",
"SSL_PROVIDER",
"GITHUB_TOKEN",
]:
value = ctx.os.environ.get(key, "")
if value:
content += '"%s": "%s",' % (key, value)
Expand Down
2 changes: 1 addition & 1 deletion build/openresty/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ kong_directory_genrule(
srcs = [
"@kong_build_tools//:srcs",
],
output_dir = "root",
cmd = """
export BAZEL_OUTPUT_DIR=${INSTALL_ROOT:-$PWD/$GENRULE_OUTPUT_DIR};
export KONG_DISTRIBUTION_PATH=$PWD/distribution;
Expand All @@ -24,5 +23,6 @@ kong_directory_genrule(
--work $DOWNLOAD_ROOT \
${DEBUG:+--debug}
""",
output_dir = "root",
visibility = ["//visibility:public"],
)

4 comments on commit a59deae

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:a59deaeb6201099d34f21045f1a0f2d29dda0cf9
Artifacts available https://github.com/Kong/kong/actions/runs/3646051885

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker image available kong/kong:master-apk
Artifacts avaialble https://github.com/Kong/kong/actions/runs/3646051885

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker image available kong/kong:master-deb
Artifacts avaialble https://github.com/Kong/kong/actions/runs/3646051885

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker image available kong/kong:master-rpm
Artifacts avaialble https://github.com/Kong/kong/actions/runs/3646051885

Please sign in to comment.