Skip to content

Commit c3c159f

Browse files
Add github workflows for pre-commit running+update
- Switch away from pre-commit.ci, as autoupdate updates hook versions *and* automatically fixes issues, which may create conflicts with prs - Import (i.e. copy-paste) workflows from the great source that is https://github.com/ros-controls/ros2_control_ci (Thanks!!) - Remove mergify configuration for auto-merging pre-commit prs and checking there is at least one approval on a pr before merging allowed
1 parent 92de04b commit c3c159f

6 files changed

+248
-16
lines changed

.github/workflows/ci-pre-commit.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Pre-commit
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
7+
jobs:
8+
pre-commit:
9+
uses: ./.github/workflows/reusable-pre-commit.yml
10+
with:
11+
ros_distro: humble

.github/workflows/docker.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Create and publish a Docker image
2+
3+
on:
4+
push:
5+
tags: ["*"]
6+
7+
env:
8+
REGISTRY: ghcr.io
9+
IMAGE_NAME: ${{ github.repository }}
10+
ROS_DISTRO: humble
11+
12+
jobs:
13+
build-and-push-image:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
19+
steps:
20+
- name: Set up Docker Buildx
21+
uses: docker/setup-buildx-action@v3
22+
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
26+
- name: Log in to the Container registry
27+
uses: docker/login-action@v3
28+
with:
29+
registry: ${{ env.REGISTRY }}
30+
username: ${{ github.actor }}
31+
password: ${{ secrets.GITHUB_TOKEN }}
32+
33+
- name: Docker meta
34+
id: meta
35+
uses: docker/metadata-action@v5
36+
with:
37+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
38+
tags: |
39+
type=ref,event=branch
40+
type=semver,pattern={{version}}
41+
type=semver,pattern={{major}}.{{minor}}
42+
43+
- name: Build and push Docker image
44+
uses: docker/build-push-action@v5
45+
with:
46+
context: .
47+
push: true
48+
tags: ${{ steps.meta.outputs.tags }}
49+
labels: ${{ steps.meta.outputs.labels }}
50+
file: Dockerfile
51+
target: runtime
52+
build-args: ROS_DISTRO=${{ env.ROS_DISTRO }}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Reusable pre-commit
2+
# The pre-commit configuration is in .pre-commit-config.yaml
3+
# OG author: Christoph Fröhlich <[email protected]>[ROS2 Control CI]
4+
# UoE editor: Alejandro Bordallo <[email protected]>
5+
6+
on:
7+
workflow_call:
8+
inputs:
9+
ros_distro:
10+
description: 'ROS2 distribution name'
11+
required: true
12+
type: string
13+
14+
jobs:
15+
pre-commit:
16+
runs-on: ubuntu-latest
17+
container: ros:${{ inputs.ros_distro }}
18+
env:
19+
# this will be src/{repo-owner}/{repo-name}
20+
path: src/${{ github.repository }}
21+
steps:
22+
- name: "Determine prerequisites"
23+
id: prereq
24+
run: |
25+
command -v sudo >/dev/null 2>&1 || (apt update && apt install -y sudo)
26+
sudo apt update
27+
echo "need_node=$(command -v node >/dev/null 2>&1 && echo 0 || echo 1)" >> $GITHUB_OUTPUT
28+
echo "need_ros2=$(if [ -d "/opt/ros/${{ inputs.ros_distro }}" ]; then echo 0; else echo 1; fi)" \
29+
>> $GITHUB_OUTPUT
30+
31+
# needed for github actions, and only if a bare ubuntu image is used
32+
- uses: actions/setup-node@v4
33+
if: ${{ steps.prereq.outputs.need_node == '1' && !env.ACT }}
34+
- name: Install node
35+
# Consider switching to https://github.com/actions/setup-node when it works
36+
# https://github.com/nektos/act/issues/973
37+
if: ${{ steps.prereq.outputs.need_node == '1' && env.ACT }}
38+
run: |
39+
sudo apt install -y curl
40+
curl -sS https://webi.sh/node | sh
41+
echo ~/.local/opt/node/bin >> $GITHUB_PATH
42+
43+
# needed only if a non-ros image is used
44+
- uses: ros-tooling/[email protected]
45+
if: ${{ steps.prereq.outputs.need_ros2 == '1' }}
46+
with:
47+
use-ros2-testing: true
48+
49+
- uses: actions/checkout@v4
50+
with:
51+
fetch-depth: 0
52+
path: ${{ env.path }}
53+
- uses: actions/cache@v4
54+
with:
55+
path: ~/.cache/pre-commit
56+
key: pre-commit|${{ inputs.ros_distro }}|${{ hashFiles( format('{0}/.pre-commit-config.yaml', env.path) ) }}
57+
- name: Install pre-commit and system hooks
58+
shell: bash
59+
run: |
60+
sudo apt-get install -qq \
61+
ros-${{ inputs.ros_distro }}-ament-cppcheck \
62+
ros-${{ inputs.ros_distro }}-ament-cpplint \
63+
ros-${{ inputs.ros_distro }}-ament-lint-cmake \
64+
ros-${{ inputs.ros_distro }}-ament-copyright \
65+
python3-venv
66+
python3 -m venv .venv
67+
source .venv/bin/activate
68+
python3 -m pip install pre-commit
69+
- name: Run pre-commit
70+
shell: bash
71+
run: |
72+
source .venv/bin/activate
73+
source /opt/ros/${{ inputs.ros_distro }}/setup.bash
74+
cd ${{ env.path }}
75+
pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage manual
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Reusable Update pre-commit
2+
# Update pre-commit config and create PR if changes are detected
3+
# OG author: Christoph Fröhlich <[email protected]>[ROS2 Control CI]
4+
# UoE editor: Alejandro Bordallo <[email protected]>
5+
6+
on:
7+
workflow_call:
8+
inputs:
9+
ref_for_scheduled_build:
10+
description: |
11+
'Reference on which the repo should be checkout for scheduled build.
12+
Usually is this name of a branch or a tag.'
13+
default: ''
14+
required: false
15+
type: string
16+
secrets:
17+
precommit-pr-token:
18+
description: 'PAT from GreatAlexander for PR auto-approval'
19+
required: true
20+
21+
jobs:
22+
auto_update_and_create_pr:
23+
runs-on: ubuntu-latest
24+
env:
25+
# this will be src/{repo-owner}/{repo-name}
26+
path: src/${{ github.repository }}
27+
28+
steps:
29+
- name: Checkout code
30+
uses: actions/checkout@v4
31+
with:
32+
fetch-depth: 0
33+
path: ${{ env.path }}
34+
ref: ${{ github.event.inputs.ref_for_scheduled_build }}
35+
36+
- name: Install pre-commit
37+
run: |
38+
sudo apt-get install -qq python3-venv
39+
python3 -m venv .venv
40+
source .venv/bin/activate
41+
python3 -m pip install pre-commit
42+
43+
- name: Auto-update with pre-commit
44+
run: |
45+
source .venv/bin/activate
46+
cd ${{ env.path }}
47+
pre-commit autoupdate || true # Ignoring errors
48+
49+
- name: Check for changes
50+
id: git_status
51+
run: |
52+
cd ${{ env.path }}
53+
git diff --quiet && echo "changed=false" >> $GITHUB_OUTPUT || echo "changed=true" >> $GITHUB_OUTPUT
54+
55+
- name: There are changes
56+
if: steps.git_status.outputs.changed == 'true'
57+
run: |
58+
cd ${{ env.path }}
59+
git diff --exit-code || true
60+
61+
- name: No changes!
62+
if: steps.git_status.outputs.changed == 'false'
63+
run: |
64+
echo "No changes detected"
65+
66+
- name: Create Pull Request
67+
id: cpr
68+
if: steps.git_status.outputs.changed == 'true'
69+
uses: peter-evans/create-pull-request@v7
70+
with:
71+
token: ${{ secrets.GITHUB_TOKEN }}
72+
branch: auto-update-${{ github.event.inputs.ref_for_scheduled_build }}
73+
base: main
74+
commit-message: Bump version of pre-commit hooks
75+
title: Bump version of pre-commit hooks
76+
body: This pull request contains auto-updated files of the pre-commit config.
77+
delete-branch: true
78+
draft: false
79+
path: ${{ env.path }}
80+
81+
- name: Enable Pull Request Automerge
82+
if: steps.cpr.outputs.pull-request-operation == 'created'
83+
run: |
84+
cd ${{ env.path }}
85+
gh pr merge --squash --auto "${{ steps.cpr.outputs.pull-request-number }}"
86+
env:
87+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
88+
89+
- name: Auto approve
90+
if: steps.cpr.outputs.pull-request-operation == 'created'
91+
run: |
92+
cd ${{ env.path }}
93+
gh pr review --approve "${{ steps.cpr.outputs.pull-request-number }}"
94+
env:
95+
GH_TOKEN: ${{ secrets.precommit-pr-token }}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Auto Update pre-commit
2+
# Update pre-commit config and create PR if changes are detected
3+
# OG author: Christoph Fröhlich <[email protected]>[ROS2 Control CI]
4+
# UoE editor: Alejandro Bordallo <[email protected]>
5+
6+
on:
7+
workflow_dispatch:
8+
schedule:
9+
- cron: '5 * * * *' # Runs at 00:00, on day 2 of the month
10+
11+
jobs:
12+
auto_update_and_create_pr:
13+
uses: ./.github/workflows/reusable-update-pre-commit.yml
14+
secrets:
15+
precommit-pr-token: ${{ secrets.PRECOMMIT_AUTOUPDATE_PR_TOKEN }}

.mergify.yml

-16
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,3 @@ pull_request_rules:
1414
users:
1515
- GreatAlexander
1616
- hect95
17-
- name: automatic merge for pre-commit ci updates
18-
conditions:
19-
- author=pre-commit-ci[bot]
20-
- title=[pre-commit.ci] pre-commit autoupdate
21-
actions:
22-
merge:
23-
method: squash
24-
merge_protections:
25-
- name: Require approval
26-
description: Require at least one review approval before merging is allowed (If
27-
not a pre-commit PR)
28-
if:
29-
- -author = pre-commit-ci[bot]
30-
- base = main
31-
success_conditions:
32-
- "#approved-reviews-by >= 1"

0 commit comments

Comments
 (0)