-
Notifications
You must be signed in to change notification settings - Fork 3
73 lines (69 loc) · 2.76 KB
/
reusable-pre-commit.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
name: Reusable pre-commit
# The pre-commit configuration is in .pre-commit-config.yaml
# we don't use the pre-commit action because it doesn't support local hooks in its virtual environment
on:
workflow_call:
inputs:
ros_distro:
description: 'ROS2 distribution name'
required: true
type: string
os_name:
description: 'On which OS to run the linter'
required: false
default: 'ubuntu-latest'
type: string
container:
description: '(optional) Docker container to run the job in, e.g. ubuntu:noble'
required: false
default: ''
type: string
jobs:
pre-commit:
runs-on: ${{ inputs.os_name }}
container: ${{ inputs.container }}
env:
# this will be src/{repo-owner}/{repo-name}
path: src/${{ github.repository }}
steps:
- name: "Determine prerequisites"
id: prereq
run: |
command -v sudo >/dev/null 2>&1 || (apt update && apt install -y sudo)
DEBIAN_FRONTEND=noninteractive sudo apt update && sudo apt upgrade -y
echo "need_node=$(command -v node >/dev/null 2>&1 && echo 0 || echo 1)" >> $GITHUB_OUTPUT
# needed for github actions, and only if a bare ubuntu image is used
- uses: actions/setup-node@v4
if: ${{ steps.prereq.outputs.need_node == '1' && !env.ACT }}
- name: Install node
# Consider switching to https://github.com/actions/setup-node when it works
# https://github.com/nektos/act/issues/973
if: ${{ steps.prereq.outputs.need_node == '1' && env.ACT }}
run: |
sudo apt install -y curl
curl -sS https://webi.sh/node | sh
echo ~/.local/opt/node/bin >> $GITHUB_PATH
# needed only if a non-ros image is used
- uses: ros-tooling/[email protected]
- uses: actions/checkout@v4
with:
fetch-depth: 0
path: ${{ env.path }}
- uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit-3|${{ inputs.ros_distro }}|${{ hashFiles('.pre-commit-config.yaml') }}
- name: Install pre-commit and system hooks
shell: bash
run: |
sudo apt-get install -qq ros-${{ inputs.ros_distro }}-ament-cppcheck ros-${{ inputs.ros_distro }}-ament-cpplint ros-${{ inputs.ros_distro }}-ament-lint-cmake ros-${{ inputs.ros_distro }}-ament-copyright python3-venv
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install pre-commit
- name: Run pre-commit
shell: bash
run: |
source .venv/bin/activate
source /opt/ros/${{ inputs.ros_distro }}/setup.bash
cd ${{ env.path }}
pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual