Skip to content

Commit

Permalink
ci: experimenting with actions
Browse files Browse the repository at this point in the history
  • Loading branch information
nishaq503 committed Feb 19, 2024
1 parent 865635a commit 05b1517
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 8 deletions.
147 changes: 147 additions & 0 deletions .github/workflows/filter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
name: Filter

on:
workflow_call:
inputs:
ignore-missing-dev:
description: "If true, the action will warn if updated crates/packages do not have a dev version. Otherwise, the action will fail if any updated crate/package does not have a dev version."
required: true
default: false
type: boolean
outputs:
rs-matrix:
description: "The directories containing the updated Rust crates"
value: ${{ jobs.filter.outputs.rs-matrix }}
py-matrix:
description: "The directories containing the updated Python packages"
value: ${{ jobs.filter.outputs.py-matrix }}

permissions:
contents: read

jobs:
filter:
name: Filter for updated packages or crates
runs-on: ubuntu-latest
outputs:
rs-matrix: ${{ steps.filter.outputs.rs-matrix }}
py-matrix: ${{ steps.filter.outputs.py-matrix }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
persist-credentials: false
- name: Find Updated Packages and crates
id: filter
run: |
CRATE_DIRS=""
PACKAGE_DIRS=""
# echo the base ref
base_ref=${{ github.base_ref }}
if [ -z "$base_ref" ]
then
base_ref="master"
echo "::warning::Action not running on PR, defaulting to base branch to master"
fi
echo "The base ref is $base_ref"
# Get the comparison point in the repo
comparison_point="origin/${base_ref}"
for changed_file in $(git diff --name-only ${comparison_point}...)
do
pkg_dir=$(dirname ${changed_file})
file_name=$(basename ${changed_file})
# Exclude the root directory
if [ "$pkg_dir" == "." ]
then
continue
fi
# Check if the changed file is a Cargo.toml or pyproject.toml file
if [ "${file_name}" == *"Cargo.toml"* ] | [ "${file_name}" == *"pyproject.toml"* ]
then
# Check that the version is a dev version
echo "Checking for dev version in $pkg_dir"
if [ $(cat $pkg_dir/VERSION) != *"dev"* ]
then
msg="${pkg_dir} does not have a dev version"
if [ ${{ inputs.ignore-missing-dev }} ]
then
echo "::warning::${msg}"
else
echo "::error::${msg}" && exit 1
fi
fi
# If the package directory string contains the substring "crates", add it to the CRATE_DIRS string
if [ "$pkg_dir" =~ "crates"* ]
then
CRATE_DIRS="$CRATE_DIRS ${pkg_dir}"
else
PACKAGE_DIRS="$PACKAGE_DIRS ${pkg_dir}"
fi
fi
done
# Trim leading whitespace
CRATE_DIRS=$(echo $CRATE_DIRS | xargs)
PACKAGE_DIRS=$(echo $PACKAGE_DIRS | xargs)
# Check if any crates or packages were found
echo "The updated crates are $CRATE_DIRS"
echo "The updated packages are $PACKAGE_DIRS"
if [ -z "$CRATE_DIRS" ] && [ -z "$PACKAGE_DIRS" ]
then
echo "::error::No updated crates or packages were found" && exit 1
fi
# Convert the crates directories to json for the output matrix
RS_JSON="{\"include\": ["
for crate_dir in $CRATE_DIRS
do
package_name=$(basename $crate_dir)
RS_JSON_LINE="{\"crate_dir\": \"${crate_dir}\", \"package_name\": \"${package_name}\"},"
# Add the RS_JSON line to the RS_JSON string if it is not already included
if [ ! "$RS_JSON" == *"$RS_JSON_LINE"* ]
then
RS_JSON="$RS_JSON$RS_JSON_LINE"
fi
done
# Remove trailing comma and add closing brackets
if [ "$RS_JSON" == *"," ]
then
RS_JSON="${RS_JSON%?}"
fi
RS_JSON="$RS_JSON]}"
# Convert the packages directories to json for the output matrix
PY_JSON="{\"include\": ["
for package_dir in $PACKAGE_DIRS
do
package_name=$(basename $package_dir)
PY_JSON_LINE="{\"package_dir\": \"${package_dir}\", \"package_name\": \"${package_name}\"},"
# Add the PY_JSON line to the PY_JSON string if it is not already included
if [ ! "$PY_JSON" == *"$PY_JSON_LINE"* ]
then
PY_JSON="$PY_JSON$PY_JSON_LINE"
fi
done
# Remove trailing comma and add closing brackets
if [ "$PY_JSON" == *"," ]
then
PY_JSON="${PY_JSON%?}"
fi
PY_JSON="$PY_JSON]}"
# Set the output
echo "rs-matrix=$( echo "$RS_JSON" )" >> $GITHUB_OUTPUT
echo "py-matrix=$( echo "$PY_JSON" )" >> $GITHUB_OUTPUT
24 changes: 16 additions & 8 deletions .github/workflows/py-release-distances.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
name: CI

on:
push:
branches:
- main
- master
tags:
- '*'
pull_request:
workflow_dispatch:
workflow_call:
inputs:
pkg-dir:
description: 'The directory of the package to test'
required: true
type: string

# on:
# push:
# branches:
# - main
# - master
# tags:
# - '*'
# pull_request:
# workflow_dispatch:

permissions:
contents: read
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/py-test-all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Python Tests

on:
pull_request:
branches:
- main
- master
- dev
push:
branches-ignore:
- main
- master
- dev

permissions:
contents: read

jobs:
filter:
name: Filter for updated crates and packages
uses: ./.github/workflows/filter.yml
with:
ignore-missing-dev: false

tests:
name: Test | ${{ matrix.package_name }}
needs: filter
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.filter.outputs.py-matrix) }}
runs-on: ubuntu-latest
uses: ./github/workflows/py-tests.yml
with:
pkg-dir: ${{ matrix.package_dir }}
120 changes: 120 additions & 0 deletions .github/workflows/py-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: Test Python package

on:
workflow_call:
inputs:
pkg-dir:
description: 'The directory of the package to test'
required: true
type: string

permissions:
contents: read

jobs:
pre-commit:
name: Pre-commit | ${{ inputs.pkg-dir }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Install pre-commit
run: |
pip install pre-commit
- name: Run pre-commit hooks and check for changes
run: |
cd "${{ inputs.pkg-dir }}"
pre-commit run --files ./**/**
if [[ $(git status --porcelain) ]]
then
echo "::error::pre-commit hooks failed for ${{ inputs.pkg-dir }}" && exit 1
fi
linux:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.pkg-dir }}
strategy:
matrix:
target: [x86_64, x86, aarch64, armv7, s390x, ppc64le]
python: ['3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
architecture: ${{ matrix.target }}
- name: Install package
uses: PyO3/maturin-action@v1
with:
command: develop
target: ${{ matrix.target }}
args: --release --extras=dev
sccache: 'true'
manylinux: auto
- name: Run tests
run: |
python -m pip install pytest
python -m pytest
windows:
runs-on: windows-latest
defaults:
run:
working-directory: ${{ inputs.pkg-dir }}
strategy:
matrix:
target: [x64, x86]
python: ['3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
architecture: ${{ matrix.target }}
- name: Install package
uses: PyO3/maturin-action@v1
with:
command: develop
target: ${{ matrix.target }}
args: --release --extras=dev
sccache: 'true'
manylinux: auto
- name: Run tests
run: |
python -m pip install pytest
python -m pytest
macos:
runs-on: macos-latest
defaults:
run:
working-directory: ${{ inputs.pkg-dir }}
strategy:
matrix:
target: [x86_64, aarch64]
python: ['3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
architecture: ${{ matrix.target }}
- name: Install package
uses: PyO3/maturin-action@v1
with:
command: develop
target: ${{ matrix.target }}
args: --release --extras=dev
sccache: 'true'
manylinux: auto
- name: Run tests
run: |
python -m pip install pytest
python -m pytest

0 comments on commit 05b1517

Please sign in to comment.