Skip to content

Commit

Permalink
feat(docker): download and cache specific checkstyle version on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKraus authored and VadimKraus committed Apr 20, 2021
1 parent 814b6da commit 4dd42b5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.pre-commit
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ repos:
rev: main
hooks:
- id: checkstyle
args:
- "-c=test/checkstyle.xml"
args: ["8.41", "-c=test/checkstyle.xml"]
stages:
- commit
2 changes: 1 addition & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
name: Checkstyle
description: "Runs checkstyle check on Java files"
language: docker
entry: "java -jar /opt/checkstyle.jar"
entry: "checkstyle"
files: \.java$
require_serial: true
20 changes: 11 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# hadolint ignore=DL3006
FROM alpine as build
ARG LATEST_URL=https://api.github.com/repos/checkstyle/checkstyle/releases/latest
SHELL ["/bin/ash","-o", "pipefail","-c"]
FROM openjdk:11.0.4-jre-slim-buster as build
ARG RELEASE_URL=https://api.github.com/repos/checkstyle/checkstyle/releases/latest
SHELL ["/bin/bash","-o", "pipefail","-c"]

# hadolint ignore=DL3018
RUN apk add --no-cache curl jq
RUN curl $LATEST_URL | jq ."assets[] | .browser_download_url" | xargs curl -L -o checkstyle.jar
# hadolint ignore=DL3008
RUN apt-get update && apt-get install -yq curl jq --no-install-recommends && rm -rf /var/lib/apt/lists/*
#RUN apk add --no-cache curl jq
#RUN curl $RELEASE_URL | jq ."assets[] | .browser_download_url" | xargs curl -L -o /opt/checkstyle.jar

COPY execute_checkstyle.sh /bin/checkstyle

FROM gcr.io/distroless/java:11
#FROM openjdk:11.0.4-jre-slim-buster
WORKDIR /opt
COPY --from=build checkstyle.jar checkstyle.jar
ENTRYPOINT ["java", "-jar", "/opt/checkstyle.jar"]
#COPY --from=build checkstyle.jar checkstyle.jar
ENTRYPOINT ["checkstyle"]
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
# pre-commit-checkstyle
A docker based pre-commit hook for Java checkstyle

#Using this hook
A docker based pre-commit hook for Java checkstyle

This hook will create a folder named `.pre-commit` in you working directory, where the used checkstyle jar file will be located.
Changing the first argument of the hook allows to select a different version.
Make sure to add the cache folder to you gitignore file:

```gitignore
.pre-commit
```

# Using this hook

```yaml
- repo: https://github.com/HotSprings-GmbH/pre-commit-checkstyle
rev: '' # A Tag or commit hash
- hooks:
- id: checkstyle
args:
- "-c=checkstyle.xml" # configure checkstyle
stages:
- commit
- repo: https://github.com/HotSprings-GmbH/pre-commit-checkstyle
rev: "" # A Tag or commit hash
hooks:
- id: checkstyle
args:
- "8.41" # the checkstyle version
- "-c=checkstyle.xml" # configure checkstyle
stages:
- commit
```
# Dependencies
## Checkstyle
This hook uses the latest provided version of [checkstyle](https://checkstyle.org/).
- [Source Code](https://github.com/checkstyle/checkstyle).
- [License: LGPL-2.1+](http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt)
- [Checkstyle Source Code](https://github.com/checkstyle/checkstyle).
- [Checkstyle License: LGPL-2.1+](http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt)
31 changes: 31 additions & 0 deletions execute_checkstyle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

VERSION=$1
RELEASE_URL="https://api.github.com/repos/checkstyle/checkstyle/releases?per_page=100"
CHECKSTYLE_FOLDER=/src/.pre-commit/
CHECKSTYLE_JAR=$CHECKSTYLE_FOLDER/checkstyle-$VERSION.jar

mkdir -p $CHECKSTYLE_FOLDER


if [ -z "$VERSION" ]; then
VERSION=$(curl https://api.github.com/repos/checkstyle/checkstyle/releases/latest | jq ".tag_name" | sed -r "s/.*e-(.*)\"/\1/")
echo "No version provided, using latest version=\"$VERSION\""
fi


if test -f "$CHECKSTYLE_JAR"; then
echo "$CHECKSTYLE_JAR exists."
else
echo "Downloading checkstyle"
VERSION_FOUND=$(curl -s $RELEASE_URL | jq ".[] | .assets[] | .browser_download_url" | grep "\-$VERSION/")
if [ -z "$VERSION_FOUND" ]; then
echo "$VERSION is could not found within the last 100 releases"
exit 1
else
echo $VERSION_FOUND | xargs curl -L -o $CHECKSTYLE_JAR
fi

fi

java -jar $CHECKSTYLE_JAR ${@:2}

0 comments on commit 4dd42b5

Please sign in to comment.