Skip to content

greenc-FNAL checking Python code #18

greenc-FNAL checking Python code

greenc-FNAL checking Python code #18

Workflow file for this run

name: Python Check
run-name: "${{ github.actor }} checking Python code"
on:
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
detect-python-changes:
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
has_changes: ${{ steps.act_force.outputs.matched == 'true' && 'true' || steps.filter.outputs.matched }}
changed_files: ${{ steps.act_force.outputs.matched == 'true' && steps.act_force.outputs.matched_files || steps.filter.outputs.matched_files }}
steps:
- name: Checkout code
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
path: phlex-src
- name: Detect act environment
id: detect_act
run: |
if [ "${GITHUB_ACTOR}" = "nektos/act" ] || [ "${ACT:-}" = "true" ] || [ "${ACT:-}" = "1" ]; then
echo "is_act=true" >> "$GITHUB_OUTPUT"
else
echo "is_act=false" >> "$GITHUB_OUTPUT"
fi
- name: Force python check execution when running under act
id: act_force
if: ${{ steps.detect_act.outputs.is_act == 'true' }}
run: |
echo "matched=true" >> "$GITHUB_OUTPUT"
echo "matched_files=act-run: forced python check execution" >> "$GITHUB_OUTPUT"
- name: Detect Python changes
id: filter
if: ${{ steps.detect_act.outputs.is_act != 'true' }}
uses: Framework-R-D/phlex/.github/actions/detect-relevant-changes@main
with:
repo-path: phlex-src
base-ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
head-ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
file-type: python
- name: Report detection outcome
env:
IS_ACT: ${{ steps.detect_act.outputs.is_act }}
ACT_MATCHED: ${{ steps.act_force.outputs.matched }}
ACT_FILES: ${{ steps.act_force.outputs.matched_files }}
FILTER_MATCHED: ${{ steps.filter.outputs.matched }}
FILTER_FILES: ${{ steps.filter.outputs.matched_files }}
run: |
if [ "${IS_ACT}" = "true" ]; then
echo "::notice::Running under act; forcing python check workflow to execute."
if [ -n "${ACT_FILES}" ]; then
printf '%s\n' "${ACT_FILES}"
fi
elif [ "${FILTER_MATCHED}" != "true" ]; then
echo "::notice::No python check relevant changes detected; workflow will be skipped."
else
echo "::group::Python check relevant files"
printf '%s\n' "${FILTER_FILES}"
echo "::endgroup::"
fi
python-check:
needs: detect-python-changes
if: ${{ needs.detect-python-changes.outputs.has_changes == 'true' || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
permissions:
contents: read
container:
image: ghcr.io/framework-r-d/phlex-ci:latest
steps:
- name: Checkout code
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
path: phlex-src
- name: Run ruff and mypy checks
working-directory: phlex-src
env:
FORCE_COLOR: 1 # `ruff`/`colored` crate
run: |
. /entrypoint.sh
failed=0
echo "➡️ Checking Python code with ruff..."
echo "::group::Running ruff check"
if ! ruff check; then
failed=1
echo "::endgroup::"
echo "❌ Ruff check failed."
else
echo "::endgroup::"
echo "✅ Ruff check passed."
fi
echo "➡️ Checking Python code with mypy..."
echo "::group::Running mypy"
if ! mypy --color-output .; then
failed=1
echo "::endgroup::"
echo "❌ MyPy check failed."
else
echo "::endgroup::"
echo "✅ MyPy check passed."
fi
exit $failed
python-check-skipped:
needs: detect-python-changes
if: ${{ needs.detect-python-changes.outputs.has_changes != 'true' && github.event_name != 'workflow_dispatch' }}
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: No relevant Python changes detected
run: echo "No Python relevant changes detected; check skipped."