From 506f4a52f6c5284d546b1c574ef639e7c1537fe3 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 29 Jan 2021 11:25:04 -0800 Subject: [PATCH 1/3] Add docker container for C++ format tools To run the tools use: `docker run --rm --privileged=true --volume ${PWD}:/otel cpp_format_tools:latest` Signed-off-by: Bogdan Drutu --- .github/workflows/cpp_format_tools.yml | 34 ++++++++++++++++++++++++++ cpp_format_tools/Dockerfile | 12 +++++++++ cpp_format_tools/README.md | 16 ++++++++++++ cpp_format_tools/format.sh | 24 ++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 .github/workflows/cpp_format_tools.yml create mode 100644 cpp_format_tools/Dockerfile create mode 100644 cpp_format_tools/README.md create mode 100755 cpp_format_tools/format.sh diff --git a/.github/workflows/cpp_format_tools.yml b/.github/workflows/cpp_format_tools.yml new file mode 100644 index 00000000..6dce17b8 --- /dev/null +++ b/.github/workflows/cpp_format_tools.yml @@ -0,0 +1,34 @@ +name: C++ Format Tools Docker Image +on: + push: + tags: [ '**' ] + branches: [ main ] + pull_request: + branches: [ main ] + paths: + - .github/workflows/cpp_format_tools.yml + - cpp_format_tools/Dockerfile + - cpp_format_tools/format.sh + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: Build the Docker image + run: docker build cpp_format_tools/. -t cpp_format_tools + - name: Push the Docker image + if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') + run: | + echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + function tag_and_push { + docker tag cpp_format_tools "otel/cpp_format_tools:${1}" && docker push "otel/cpp_format_tools:${1}" + } + if [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then + tag_and_push "latest" + elif [[ "${GITHUB_REF}" =~ refs/tags/v[0-9]+\.[0-9]+\.[0-9]+ ]]; then + TAG="${GITHUB_REF#"refs/tags/v"}" + tag_and_push "${TAG}" + else + tag_and_push "${GITHUB_REF#"refs/tags/"}" + fi \ No newline at end of file diff --git a/cpp_format_tools/Dockerfile b/cpp_format_tools/Dockerfile new file mode 100644 index 00000000..78399a00 --- /dev/null +++ b/cpp_format_tools/Dockerfile @@ -0,0 +1,12 @@ +ARG ALPINE_VERSION=3.13 + +FROM alpine:${ALPINE_VERSION} +LABEL maintainer="The OpenTelemetry Authors" +RUN apk update +RUN apk add --no-cache clang=10.0.1-r0 python3 py3-pip git curl +RUN pip3 install cmake_format==0.6.13 +RUN curl -L -o /usr/local/bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/2.2.1/buildifier +RUN chmod +x /usr/local/bin/buildifier + +COPY format.sh / +ENTRYPOINT ["sh", "/format.sh"] diff --git a/cpp_format_tools/README.md b/cpp_format_tools/README.md new file mode 100644 index 00000000..2ac95296 --- /dev/null +++ b/cpp_format_tools/README.md @@ -0,0 +1,16 @@ +# C++ Format Tools + Docker + +A lightweight Docker image, published as otel/cpp_format_tools to Docker Hub, +with all dependencies and tools built-in, to format C++ code. + +## What's included in the image + +- clang-format +- cmake-format +- buildifier + +## Usage + +```bash +docker run --rm --privileged=true --volume ${PWD}:/otel otel/cpp_format_tools +``` diff --git a/cpp_format_tools/format.sh b/cpp_format_tools/format.sh new file mode 100755 index 00000000..cbab5b29 --- /dev/null +++ b/cpp_format_tools/format.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e + +FIND="find /otel -name third_party -prune -o -name tools -prune -o -name .git -prune -o -name _deps -prune -o -name .build -prune -o -name out -prune -o -name .vs -prune -o" + +echo "Running sed: " +echo "-> Correct common miscapitalizations." +sed -i 's/Open[t]elemetry/OpenTelemetry/g' $($FIND -type f -print) +echo "-> No CRLF line endings, except Windows files." +sed -i 's/\r$//' $($FIND -name '*.ps1' -prune -o \ + -name '*.cmd' -prune -o -type f -print) +echo "-> No trailing spaces." +sed -i 's/ \+$//' $($FIND -type f -print) + +echo "Running clang-format $(clang-format --version 2>&1)." +clang-format -i -style=file $($FIND -name '*.cc' -print -o -name '*.h' -print) + +echo "Running cmake-format $(cmake-format --version 2>&1)." +cmake-format -i $($FIND -name 'CMakeLists.txt' -print -name '*.cmake' -print -name '*.cmake.in' -print) + +echo "Running buildifier" +buildifier $($FIND -name WORKSPACE -print -o -name BUILD -print -o \ + -name '*.BUILD' -o -name '*.bzl' -print) From 57af2a1ce7f5e3b90ded1bc469217831f8d18d77 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 29 Jan 2021 12:05:08 -0800 Subject: [PATCH 2/3] Fix review feedback Signed-off-by: Bogdan Drutu --- .github/workflows/cpp_format_tools.yml | 15 +++++++++++---- cpp_format_tools/format.sh | 3 +-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cpp_format_tools.yml b/.github/workflows/cpp_format_tools.yml index 6dce17b8..d916c5b8 100644 --- a/.github/workflows/cpp_format_tools.yml +++ b/.github/workflows/cpp_format_tools.yml @@ -7,20 +7,27 @@ on: branches: [ main ] paths: - .github/workflows/cpp_format_tools.yml - - cpp_format_tools/Dockerfile - - cpp_format_tools/format.sh + - cpp_format_tools/* jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 + - name: Build the Docker image run: docker build cpp_format_tools/. -t cpp_format_tools + + - name: Login to GitHub Package Registry + if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Push the Docker image if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') run: | - echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin function tag_and_push { docker tag cpp_format_tools "otel/cpp_format_tools:${1}" && docker push "otel/cpp_format_tools:${1}" } @@ -31,4 +38,4 @@ jobs: tag_and_push "${TAG}" else tag_and_push "${GITHUB_REF#"refs/tags/"}" - fi \ No newline at end of file + fi diff --git a/cpp_format_tools/format.sh b/cpp_format_tools/format.sh index cbab5b29..29d2d8ff 100755 --- a/cpp_format_tools/format.sh +++ b/cpp_format_tools/format.sh @@ -20,5 +20,4 @@ echo "Running cmake-format $(cmake-format --version 2>&1)." cmake-format -i $($FIND -name 'CMakeLists.txt' -print -name '*.cmake' -print -name '*.cmake.in' -print) echo "Running buildifier" -buildifier $($FIND -name WORKSPACE -print -o -name BUILD -print -o \ - -name '*.BUILD' -o -name '*.bzl' -print) +buildifier $($FIND -name WORKSPACE -print -o -name BUILD -print -o -name '*.BUILD' -o -name '*.bzl' -print) From 28a781c3418b6d0703c92bfa36107c742c50eeeb Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 29 Jan 2021 13:33:34 -0800 Subject: [PATCH 3/3] Extract versions as args, update buildifier to 3.5.0 Signed-off-by: Bogdan Drutu --- cpp_format_tools/Dockerfile | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cpp_format_tools/Dockerfile b/cpp_format_tools/Dockerfile index 78399a00..3d99a3f5 100644 --- a/cpp_format_tools/Dockerfile +++ b/cpp_format_tools/Dockerfile @@ -1,11 +1,21 @@ ARG ALPINE_VERSION=3.13 +ARG BUILDIFIER_VERSION=3.5.0 +ARG CLANG_VERSION=10.0.1-r0 +ARG CMAKE_FORMAT_VERSION=0.6.13 + FROM alpine:${ALPINE_VERSION} LABEL maintainer="The OpenTelemetry Authors" RUN apk update -RUN apk add --no-cache clang=10.0.1-r0 python3 py3-pip git curl -RUN pip3 install cmake_format==0.6.13 -RUN curl -L -o /usr/local/bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/2.2.1/buildifier + +ARG CLANG_VERSION +RUN apk add --no-cache clang=${CLANG_VERSION} python3 py3-pip git curl + +ARG CMAKE_FORMAT_VERSION +RUN pip3 install cmake_format==${CMAKE_FORMAT_VERSION} + +ARG BUILDIFIER_VERSION +RUN curl -L -o /usr/local/bin/buildifier https://github.com/bazelbuild/buildtools/releases/download/${BUILDIFIER_VERSION}/buildifier RUN chmod +x /usr/local/bin/buildifier COPY format.sh /