Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker container for C++ format tools #28

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/cpp_format_tools.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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/*

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: |
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
22 changes: 22 additions & 0 deletions cpp_format_tools/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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

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 /
ENTRYPOINT ["sh", "/format.sh"]
16 changes: 16 additions & 0 deletions cpp_format_tools/README.md
Original file line number Diff line number Diff line change
@@ -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
```
23 changes: 23 additions & 0 deletions cpp_format_tools/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/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)