Skip to content

Commit

Permalink
feat(build): add image and cloudbuild configs for pre-commit (#4898)
Browse files Browse the repository at this point in the history
* feat(build): add image and cloudbuild configs for pre-commit

* fix(build): clean up lint shellcheck

* chore: only fetch primary branch from origin
  • Loading branch information
shahms committed Apr 23, 2021
1 parent b0c64e1 commit 63fa149
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
18 changes: 18 additions & 0 deletions kythe/release/cloudbuild/lint.yaml
@@ -0,0 +1,18 @@
steps:
- name: gcr.io/cloud-builders/git
args:
- fetch
- '--unshallow'
- '--no-tags'
- origin
- master
- name: gcr.io/kythe-repo/pre-commit
args:
- run
- '--hook-stage'
- commit
- '--show-diff-on-failure'
- '--from-ref'
- origin/master
- '--to-ref'
- HEAD
53 changes: 53 additions & 0 deletions kythe/release/cloudbuild/pre-commit/Dockerfile
@@ -0,0 +1,53 @@
# Copyright 2020 The Kythe 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.

# docker build -t gcr.io/kythe-repo/pre-commit .
FROM ubuntu:focal

RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends \
curl ca-certificates \
# pre-commit dependencies
python3 python3-dev python3-pip \
# Linter dependencies
golang-go shellcheck clang-format-11 openjdk-11-jre-headless git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Make clang-format-11 the default
RUN update-alternatives \
--install /usr/bin/clang-format clang-format /usr/bin/clang-format-11 100

# Install pip-packages
RUN pip3 install --upgrade pip \
&& pip3 install pre-commit

# Install extra linters
RUN go get github.com/bazelbuild/buildtools/buildifier \
&& go get golang.org/x/lint/golint \
&& go get honnef.co/go/tools/cmd/staticcheck

# Fetch the latest version of google-java-format from GitHub
RUN curl -s https://api.github.com/repos/google/google-java-format/releases/latest \
| sed -n '/browser_download_url/s/[^:]*:[^"]*\("[^"]*"\).*/url = \1/p' \
| egrep 'google-java-format-[^-]*-all-deps.jar' \
| curl -L -o /usr/bin/google-java-format.jar -K - \
&& /bin/echo -e '#!/bin/sh\nexec java -jar /usr/bin/google-java-format.jar "$@"' >/usr/bin/google-java-format \
&& chmod +x /usr/bin/google-java-format

# Install go wrapper script
ADD go /usr/local/bin/go
ENV PATH=$PATH:/root/go/bin
ENTRYPOINT ["pre-commit"]
12 changes: 12 additions & 0 deletions kythe/release/cloudbuild/pre-commit/cloudbuild.yaml
@@ -0,0 +1,12 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args: ['-c', 'docker pull gcr.io/$PROJECT_ID/pre-commit:latest || exit 0']
- name: 'gcr.io/cloud-builders/docker'
args: [
'build',
'-t', 'gcr.io/$PROJECT_ID/pre-commit:latest',
'--cache-from', 'gcr.io/$PROJECT_ID/pre-commit:latest',
'.'
]
images: ['gcr.io/$PROJECT_ID/pre-commit:latest']
72 changes: 72 additions & 0 deletions kythe/release/cloudbuild/pre-commit/go
@@ -0,0 +1,72 @@
#!/bin/bash -e
#
# Copyright 2019 The Kythe 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.
#
# Wrapper script to run the Go version specified by a go.mod file.

CACHE="$HOME/.cache/goversions"

GO="$(command -v go | xargs -L1 realpath | grep -v "$(realpath "$0")" | head -n1)"
if ! [[ -x "$GO" ]]; then
echo "ERROR: failed to find ambient go tool" >&2
exit 1
fi

find_gomod() {
local dir="$PWD"
local f="$dir/go.mod"
while true; do
if [[ -r "$f" ]]; then
echo "$f"
return
elif [[ "/" == "$dir" ]]; then
return 1
fi
dir="$(dirname "$dir")"
done
}

if ! gomod="$(find_gomod)"; then
echo "WARNING: could not find go.mod" >&2
exec "$GO" "$@"
fi

if ! version="$(grep -Po '(?<=^go\s)\d+(\.\d+)+$' "$gomod")"; then
echo "WARNING: could not find version in go.mod" >&2
exec "$GO" "$@"
fi

GOROOT="${CACHE}/${version}/go"

if [[ -x "$GOROOT/bin/go" ]]; then
# Cache hit: exec go tool
exec "$GOROOT/bin/go" "$@"
fi

# Setup temporary location for archive
archive="$(mktemp)"
trap "rm '$archive'" EXIT ERR INT

# Download and extract Go compiler
url="https://dl.google.com/go/go${version}.linux-amd64.tar.gz"
curl -L "$url">"$archive"
dir="$(dirname "$GOROOT")"
rm -rf "$dir"
mkdir -p "$dir"
tar xf "$archive" -C "$dir"
rm -f "$archive"

# Run go tool
exec "$GOROOT/bin/go" "$@"

0 comments on commit 63fa149

Please sign in to comment.