Skip to content

Commit 560fd1d

Browse files
authored
feat(ci): replace broken clang-format action (#456)
Replace jidicula/clang-format-action (whose Docker image is no longer available on ghcr.io) with an in-repo GitHub Action backed by a minimal Docker image (~11 MB) containing a statically built clang-format 19 on Alpine. Add automated image build and push workflow for ghcr.io. Split formatting check into separate C and Protobuf jobs with configurable include patterns and exclude regex.
1 parent e33444f commit 560fd1d

File tree

5 files changed

+154
-7
lines changed

5 files changed

+154
-7
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
ARG ALPINE_TAG=3.21
2+
3+
FROM alpine:${ALPINE_TAG} AS builder
4+
5+
ARG VERSION=19
6+
ARG BRANCH=release/${VERSION}.x
7+
8+
RUN set -x \
9+
&& apk add --no-cache git cmake ninja g++ python3 \
10+
&& git clone --depth 1 --branch "${BRANCH}" https://github.com/llvm/llvm-project.git /src \
11+
&& ln -s ../../clang /src/llvm/tools/ \
12+
&& mkdir /src/llvm/_build \
13+
&& cd /src/llvm/_build \
14+
&& cmake .. \
15+
-G Ninja \
16+
-DCMAKE_BUILD_TYPE=Release \
17+
-DCMAKE_C_FLAGS="-static-libgcc" \
18+
-DCMAKE_CXX_FLAGS="-static-libgcc -static-libstdc++" \
19+
&& ninja clang-format \
20+
&& strip bin/clang-format
21+
22+
FROM alpine:${ALPINE_TAG}
23+
24+
COPY --from=builder /src/llvm/_build/bin/clang-format /usr/bin/clang-format
25+
COPY entrypoint.sh /entrypoint.sh
26+
27+
ENTRYPOINT ["/bin/sh", "/entrypoint.sh"]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: 'clang-format check'
2+
description: 'Run clang-format on files matching given patterns'
3+
4+
inputs:
5+
include:
6+
description: 'Newline-separated file name patterns for find -name (e.g., *.h)'
7+
required: true
8+
exclude-regex:
9+
description: 'Extended regex to exclude file paths (grep -Ev)'
10+
required: false
11+
default: ''
12+
style:
13+
description: 'clang-format style'
14+
required: false
15+
default: 'file'
16+
17+
runs:
18+
using: 'docker'
19+
image: 'docker://ghcr.io/yanet-platform/clang-format:19'
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
2+
# clang-format check entrypoint.
3+
# Inputs (via environment variables set by GitHub Actions):
4+
# INPUT_INCLUDE - newline-separated file name patterns (for find -name)
5+
# INPUT_EXCLUDE_REGEX - extended regex to exclude file paths (grep -Ev)
6+
# INPUT_STYLE - clang-format style (default: file)
7+
set -e
8+
9+
STYLE="${INPUT_STYLE:-file}"
10+
WORKSPACE="${GITHUB_WORKSPACE:-.}"
11+
12+
cd "$WORKSPACE" || { echo "ERROR: cannot cd to $WORKSPACE"; exit 1; }
13+
14+
# Collect files matching the include patterns.
15+
FILELIST="$(mktemp)"
16+
trap 'rm -f "$FILELIST"' EXIT
17+
18+
echo "$INPUT_INCLUDE" | while IFS= read -r pattern; do
19+
# Skip empty lines.
20+
[ -z "$pattern" ] && continue
21+
find . -type f -name "$pattern"
22+
done | sort -u > "$FILELIST"
23+
24+
# Apply exclude regex if provided.
25+
if [ -n "$INPUT_EXCLUDE_REGEX" ]; then
26+
FILTERED="$(mktemp)"
27+
grep -Ev "$INPUT_EXCLUDE_REGEX" "$FILELIST" > "$FILTERED" || true
28+
mv "$FILTERED" "$FILELIST"
29+
fi
30+
31+
TOTAL="$(wc -l < "$FILELIST" | tr -d ' ')"
32+
if [ "$TOTAL" -eq 0 ]; then
33+
echo "No files matched the include patterns."
34+
exit 0
35+
fi
36+
37+
echo "Checking $TOTAL file(s) with clang-format (style=$STYLE)..."
38+
39+
FAILED=0
40+
while IFS= read -r file; do
41+
if ! /usr/bin/clang-format --dry-run --Werror --style="$STYLE" "$file" 2>/dev/null; then
42+
FAILED=$((FAILED + 1))
43+
echo "--- Formatting diff for $file ---"
44+
/usr/bin/clang-format --style="$STYLE" "$file" | diff -u "$file" - || true
45+
echo ""
46+
fi
47+
done < "$FILELIST"
48+
49+
if [ "$FAILED" -ne 0 ]; then
50+
echo "clang-format: $FAILED file(s) need formatting."
51+
exit 1
52+
fi
53+
54+
echo "clang-format: all files are properly formatted."
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Build and push clang-format image
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: ["main"]
7+
paths:
8+
- ".github/actions/clang-format/**"
9+
10+
permissions:
11+
packages: write
12+
13+
jobs:
14+
build-push:
15+
runs-on: ubuntu-24.04
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Login to GitHub Container Registry
20+
uses: docker/login-action@v3
21+
with:
22+
registry: ghcr.io
23+
username: ${{ github.actor }}
24+
password: ${{ secrets.GITHUB_TOKEN }}
25+
26+
- name: Set up Docker Buildx
27+
uses: docker/setup-buildx-action@v3
28+
29+
- name: Build and push
30+
uses: docker/build-push-action@v6
31+
with:
32+
context: .github/actions/clang-format
33+
push: true
34+
tags: ghcr.io/yanet-platform/clang-format:19

.github/workflows/check-format.yml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,35 @@ on:
77
- "**.h"
88
- "**.c"
99
- "**.proto"
10-
- "**/meson.build"
1110
pull_request:
1211
branches: ["main"]
1312
paths:
1413
- "**.h"
1514
- "**.c"
1615
- "**.proto"
17-
- "**/meson.build"
1816

1917
jobs:
20-
fmt:
18+
c-cpp-fmt:
19+
name: C formatting
2120
runs-on: ubuntu-24.04
2221
steps:
23-
- uses: actions/checkout@v3
22+
- uses: actions/checkout@v4
2423
- name: Run clang-format
25-
uses: jidicula/clang-format-action@v4.14.0
24+
uses: ./.github/actions/clang-format
2625
with:
27-
clang-format-version: 19
28-
exclude-regex: subprojects/.*
26+
include: |
27+
*.h
28+
*.c
29+
exclude-regex: '^./subprojects/'
30+
31+
proto-fmt:
32+
name: Protobuf formatting
33+
runs-on: ubuntu-24.04
34+
steps:
35+
- uses: actions/checkout@v4
36+
- name: Run clang-format
37+
uses: ./.github/actions/clang-format
38+
with:
39+
include: |
40+
*.proto
41+
exclude-regex: '^./subprojects/'

0 commit comments

Comments
 (0)