Skip to content

Commit

Permalink
Fix tests and add pixi
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw committed Jan 28, 2024
1 parent bf6984b commit 7036487
Show file tree
Hide file tree
Showing 12 changed files with 3,462 additions and 118 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# GitHub syntax highlighting
pixi.lock linguist-language=YAML

3 changes: 2 additions & 1 deletion .github/workflows/bump-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ jobs:
from pathlib import Path
tools = {}
skip_actions = []
# incompatible with subdirectory actions
skip_actions = ["Quantco/ui-actions/version-metadata"]
for file in Path("template/.github/workflows").glob("*"):
content = file.read_text()
Expand Down
120 changes: 5 additions & 115 deletions .github/workflows/ci-copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,131 +6,21 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

defaults:
run:
shell: bash -el {0}

jobs:
linux-unittests:
name: Check project generation
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- name: Checkout branch
- name: Checkout
uses: actions/checkout@v4
- name: Set up Conda env
uses: mamba-org/setup-micromamba@8767fb704bd78032e9392f0386bf46950bdd1194
with:
environment-file: environment.yml
cache-environment: true
create-args: >-
python=3.11
pytest-md
pytest-emoji
- name: Cache pre-commit envs
uses: actions/cache@v4
with:
path: |
~/.cache/pre-commit
key: project-generation-pre-commit-${{ hashFiles('template/.pre-commit-config.yaml') }}
fetch-depth: 0 # copier doesn't like shallow clones
- name: Set up pixi
uses: prefix-dev/setup-pixi@ca3b9ac762955fad216e3d8e0bbf22087071c89c
- name: Run unittests
uses: quantco/pytest-action@v2
with:
report-title: Check project generation
click-to-expand: false

pre-commit-checks:
name: Pre-commit checks - Python 3.11
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- name: Checkout branch
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetching the entire branch history is necessary for the 'pre-commit-insert-license' hook to recognize the actual git lifespan of a file.
- name: Run pre-commit-conda
uses: quantco/pre-commit-conda@v1
with:
python-version: "3.11"

test-generated-package-ci:
name: Test CI of generated package (rattler-build = ${{ matrix.use-rattler-build }}) (mypy = ${{ matrix.use-mypy }})
timeout-minutes: 30
runs-on: ubuntu-latest
concurrency: test-generated-package-ci-${{ github.sha }}-${{ matrix.use-rattler-build }}-${{ matrix.use-mypy }}
strategy:
matrix:
include:
- use-rattler-build: false
use-mypy: true
- use-rattler-build: true
use-mypy: true
- use-rattler-build: true
use-mypy: false

steps:
- name: Checkout branch
uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
ref: ${{ github.head_ref }}
- name: Set up Conda env
uses: mamba-org/setup-micromamba@8767fb704bd78032e9392f0386bf46950bdd1194
with:
environment-file: environment.yml
cache-environment: true
- name: Test generated package CI
run: |
# Name of the generated package.
PKG=quantcore.test-package
# Authentication for pushing to $REPO.
AUTH='authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
eval $(ssh-agent)
ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}"
# Set up local Git so that copier can run "git commit".
git config --global user.email "[email protected]"
git config --global user.name "Forrest Quant"
# Generate package with default settings + Windows CI.
copier copy \
--data project_name="Package" \
--data project_short_description="Example Package" \
--data use_rattler_build="${{ matrix.use-rattler-build }}" \
--data use_mypy="${{ matrix.use-mypy }}" \
--data github_user="ForrestQuant" \
--data project_slug="$PKG" \
--defaults \
--trust \
. out
cd out
# Push the generated package's HEAD commit to a `ci/*` branch
cid=$(git rev-parse HEAD)
git push -f "${GITHUB_SERVER_URL/https:\/\//git@}:$GITHUB_REPOSITORY" $cid:refs/heads/ci/$GITHUB_SHA-${{ matrix.use-rattler-build }}-${{ matrix.use-mypy }}
# Use the GitHub API to wait for the generated package's CI to complete (success or failure).
# We look for a GitHub Actions run for the HEAD commit ID.
WORKFLOW_URL="$GITHUB_API_URL/repos/${GITHUB_REPOSITORY}/actions/runs?branch=ci/${GITHUB_SHA}-${{ matrix.use-rattler-build }}-${{ matrix.use-mypy }}&head_sha=${cid}"
echo "Waiting for inner CI to start"
while (( $(curl -Ls --header "$AUTH" "$WORKFLOW_URL" | jq -r ".workflow_runs | length") < 1 )); do
sleep 10
done
echo "Waiting for inner CI to complete"
while curl -Ls --header "$AUTH" "$WORKFLOW_URL" | jq -r ".workflow_runs | .[] | .status" | grep --invert-match completed > /dev/null; do
sleep 10
done
# Fail unless CI was successful.
if curl -Ls --header "$AUTH" "$WORKFLOW_URL" | jq -r ".workflow_runs | .[] | .conclusion" | grep --invert-match success > /dev/null; then
echo "CI pipeline failed"
exit 1
fi
- name: Clean up CI branch
if: always()
run: |
AUTH='authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
eval $(ssh-agent)
ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}"
git push -d "${GITHUB_SERVER_URL/https:\/\//git@}:$GITHUB_REPOSITORY" refs/heads/ci/$GITHUB_SHA-${{ matrix.use-rattler-build }}-${{ matrix.use-mypy }}
for line in $(curl -Ls --header "$AUTH" "$GITHUB_API_URL/repos/${GITHUB_REPOSITORY}/actions/runs?branch=ci/${GITHUB_SHA}-${{ matrix.use-rattler-build }}-${{ matrix.use-mypy }}&head_sha=${cid}" | jq -r ".workflow_runs | .[] | select(.status != \"completed\") | .id")
do
curl -Ls --header "$AUTH" --request POST "$GITHUB_API_URL/repos/${GITHUB_REPOSITORY}/actions/runs/$line/cancel" > /dev/null
done
custom-pytest: pixi run pytest
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# pixi environments
.pixi

__pycache__
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# copier-template-pre-commit-mirrors

This is a template for our pre-commit mirrors.

To create a new mirror, you can run the following command

```bash
pixi run generate /path/to/mirror
```
17 changes: 15 additions & 2 deletions copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ _min_copier_version: '9.1.1'
tool:
type: str
help: What is the name of the tool?
validator: >-
{% if entry == '' %}
Please provide a valid tool name
{% endif %}
url:
type: str
help: What is the URL of the tool?
validator: >-
{% if not url.startswith('https://') %}
Please provide a URL
{% endif %}
entry:
type: str
help: What is the pre-commit entry?
help: What is the pre-commit entry (for example `ruff --fix --exit-non-zero-on-fix`)?
validator: >-
{% if entry == '' %}
Please provide an entry
{% endif %}
_tasks:
- sed -i "s/TOOL_VERSION/$(micromamba search -c conda-forge {{ tool }} --json | jq -r '.result.pkgs[0].version')/g" environment.yml
- git init
- gsed -i "s/TOOL_VERSION/$(micromamba search {{ tool }} --json | jq -r '.result.pkgs[0].version')/g" environment.yml
- git branch -M main
Loading

0 comments on commit 7036487

Please sign in to comment.