Skip to content

chore(ci): fix failure condition when modified files are empty #313

chore(ci): fix failure condition when modified files are empty

chore(ci): fix failure condition when modified files are empty #313

name: cppcheck-differential
on:
pull_request:
jobs:
cppcheck-differential:
runs-on: ubuntu-latest
steps:
- name: Set PR fetch depth
run: echo "PR_FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"
- name: Checkout PR branch and all PR commits
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: ${{ env.PR_FETCH_DEPTH }}
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y git
# cppcheck from apt does not yet support --check-level args, and thus install from snap
- name: Install Cppcheck from snap
run: |
sudo snap install cppcheck
- name: Fetch the base branch with enough history for a common merge-base commit
run: git fetch origin ${{ github.base_ref }}
shell: bash
- name: Get modified files
id: get-modified-files
run: |
echo "changed_files=$(git diff --name-only "origin/${{ github.base_ref }}"...HEAD | grep -E '\.(cpp|hpp)$' | tr '\n' ' ' || true)" >> $GITHUB_OUTPUT
shell: bash
- name: Run Cppcheck on changed files
continue-on-error: true
id: cppcheck
run: |
files=$(cat changed_files.txt | grep -E '\.(cpp|hpp)$' || true)
if [ -n "$files" ]; then
echo "Running Cppcheck on changed files: ${{ steps.get-modified-files.outputs.changed_files }}"
cppcheck --enable=all --inconclusive --check-level=exhaustive --error-exitcode=1 --suppressions-list=.cppcheck_suppressions ${{ steps.get-modified-files.outputs.changed_files }} 2> cppcheck-report.txt
else
echo "No C++ files changed."
touch cppcheck-report.txt
fi
shell: bash
- name: Show cppcheck-report result
run: |
cat cppcheck-report.txt
- name: Upload Cppcheck report
uses: actions/upload-artifact@v2
with:
name: cppcheck-report
path: cppcheck-report.txt
- name: Fail the job if Cppcheck failed
if: ${{ steps.get-modified-files.outputs.changed_files != '' && steps.cppcheck.outcome == 'failure' }}
run: exit 1