Skip to content

Commit 1efc7c0

Browse files
committed
ci: run clang-format action inside custom docker
1 parent e987517 commit 1efc7c0

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

.github/workflows/clang_format.yml

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,55 @@ on: # yamllint disable-line rule:truthy
77
jobs:
88
clang-format:
99
runs-on: ubuntu-latest
10+
container:
11+
image: dtors/base-cpp:latest
1012

1113
steps:
1214
- name: Checkout code
13-
uses: actions/checkout@v3
14-
- name: Get changed files
15-
uses: jitterbit/get-changed-files@v1
16-
id: changed_files
17-
with:
18-
format: space-delimited
19-
token: ${{ secrets.GITHUB_TOKEN }}
20-
- name: Set up clang-format
21-
run: sudo apt-get install clang-format
22-
- name: Run clang-format
23-
run: |-
24-
IFS=' ' read -r -a files <<< "${{ steps.changed_files.outputs.all }}"
25-
cpp_files=()
26-
for file in "${files[@]}"; do
27-
if [[ "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp || "$file" == *.cc || "$file" == *.cxx || "$file" == *.hxx ]]; then
28-
cpp_files+=("$file")
29-
fi
30-
done
31-
32-
if [ ${#cpp_files[@]} -gt 0 ]; then
33-
echo -e "Running clang-format"
34-
clang-format --dry-run --Werror "${cpp_files[@]}"
35-
else
36-
echo -e "Skipping clang-format since no source files were changed."
37-
fi
15+
uses: actions/checkout@v4
16+
17+
- name: Configure Git Safe Directory
18+
run: |
19+
git config --global --add safe.directory $(pwd)
20+
21+
- name: Find affected files
22+
run: |
23+
git fetch origin ${{ github.base_ref }} --depth=1
24+
git diff --name-only origin/${{ github.base_ref }} > affected_files.txt
25+
echo "Affected files:"
26+
cat affected_files.txt
27+
28+
- name: Find affected cpp files
29+
run: |
30+
> affected_cpp_files.txt
31+
32+
while read file; do
33+
if [[ "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp || "$file" == *.cc || "$file" == *.cxx || "$file" == *.hxx ]]; then
34+
echo "$file" >> affected_cpp_files.txt
35+
fi
36+
done < affected_files.txt
37+
38+
sort -u affected_cpp_files.txt -o affected_cpp_files.txt
39+
echo "Affected cpp files:"
40+
cat affected_cpp_files.txt
41+
echo "cpp_files=$(cat affected_cpp_files.txt | tr '\n' ' ')" >> $GITHUB_ENV
42+
shell: bash
43+
44+
- name: Run Clang-Format
45+
if: env.cpp_files != ''
46+
run: |
47+
echo "Running: clang-format --dry-run --Werror ${cpp_files}"
48+
clang-format --dry-run --Werror ${cpp_files}
49+
50+
- name: Skip Clang-Format
51+
if: env.cpp_files == ''
52+
run: echo "No affected cpp files found. Skipping Clang-Format."
53+
3854
- name: Show failure message
3955
if: failure()
4056
run: |-
4157
echo -e "To automatically apply suggested fixes, run the following command:
4258
======================================================================================
43-
1. Install clang-format:
44-
sudo apt-get install clang-format
45-
2. Run clang-format:
59+
1. Run clang-format:
4660
find . -name \"*.cpp\" -o -name \"*.h\" -o -name \"*.hpp\" -o -name \"*.cc\" -o -name \"*.cxx\" -o -name \"*.hxx\" | xargs clang-format -i
4761
======================================================================================"

0 commit comments

Comments
 (0)