Skip to content

Commit

Permalink
CI: run tests inside the container
Browse files Browse the repository at this point in the history
  • Loading branch information
Parnassius committed Mar 7, 2024
1 parent 7bfd1b6 commit 91ed5f0
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 162 deletions.
139 changes: 139 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
name: ci/cd

on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: '0 0 1 * *' # monthly container rebuild
- cron: '0 6 * * 6' # weekly integration tests

jobs:
test:
runs-on: ubuntu-latest
if: github.event_name != 'schedule' || github.event.schedule != '0 6 * * 6'
outputs:
coverage-percentage: ${{ steps.percentage.outputs.percentage }}
steps:
- uses: actions/checkout@v4
- name: Cache veekun database
uses: actions/cache@v4
with:
path: data/veekun.sqlite
key: veekun-${{ hashFiles('cerbottana/data/veekun/*.csv', 'cerbottana/databases/veekun.py', 'cerbottana/tasks/veekun.py') }}
- name: Cache pokedex database
uses: actions/cache@v4
with:
path: data/pokedex.sqlite
key: pokedex-${{ hashFiles('poetry.lock') }}
restore-keys: |
pokedex-
- name: Create folders
run: mkdir -p coverage data
- name: Lint and test in container
run: podman build --volume "$PWD"/coverage:/coverage:z --volume "$PWD"/data:/data:z --target test .
- name: Output coverage percentage
id: percentage
if: github.event_name == 'push' && github.ref_name == 'main'
run: echo "percentage=$(jq '.totals.percent_covered_display' coverage/coverage.json)" >> $GITHUB_OUTPUT
- name: Save coverage report
if: github.event_name != 'schedule'
run: cat coverage/coverage.md >> $GITHUB_STEP_SUMMARY

integration:
runs-on: ubuntu-latest
if: github.event_name != 'schedule' || github.event.schedule == '0 6 * * 6'
steps:
- uses: actions/checkout@v4
- name: Cache veekun database
uses: actions/cache@v4
with:
path: data/veekun.sqlite
key: veekun-${{ hashFiles('cerbottana/data/veekun/*.csv', 'cerbottana/databases/veekun.py', 'cerbottana/tasks/veekun.py') }}
- name: Cache pokedex database
uses: actions/cache@v4
with:
path: data/pokedex.sqlite
key: pokedex-${{ hashFiles('poetry.lock') }}
restore-keys: |
pokedex-
- name: Cache pokemon-showdown instance
uses: actions/cache@v4
with:
path: pokemon-showdown
key: pokemon-showdown-${{ github.run_id }}
restore-keys: |
pokemon-showdown-
- name: Create folders
run: mkdir -p data pokemon-showdown
- name: Run integration tests
run: podman build --env USERNAME --env PASSWORD --env TESTS_MOD_USERNAME --env TESTS_MOD_PASSWORD --volume "$PWD"/pokemon-showdown:/pokemon-showdown:z --volume "$PWD"/data:/data:z --target integration .
env:
USERNAME: ${{ secrets.TESTS_BOT_USERNAME }}
PASSWORD: ${{ secrets.TESTS_BOT_PASSWORD }}
TESTS_MOD_USERNAME: ${{ secrets.TESTS_MOD_USERNAME }}
TESTS_MOD_PASSWORD: ${{ secrets.TESTS_MOD_PASSWORD }}

coverage-badge:
runs-on: ubuntu-latest
needs: test
if: github.event_name == 'push' && github.ref_name == 'main'
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
ref: "coverage-badge"
- name: Calculate badge color
id: color
run: |
echo "color=$(python3 -Ic 'print(
min(
(percentage, color)
for percentage, color in [
(100, "brightgreen"),
(90, "green"),
(70, "yellowgreen"),
(50, "yellow"),
(30, "orange"),
(0, "red"),
]
if percentage >= int(${{ needs.test.outputs.coverage-percentage }})
)[1]
)')" >> $GITHUB_OUTPUT
- name: Update JSON file
run: |
jq -n \
--argjson schemaVersion 1 \
--arg label coverage \
--arg message ${{ needs.test.outputs.coverage-percentage }}% \
--arg color ${{ steps.color.outputs.color }} \
'$ARGS.named' > coverage-badge.json
- name: Create commit
run: |
git config user.name 'prns'
git config user.email '[email protected]'
git commit -am "Update coverage" && git push || true
publish:
permissions:
contents: read
packages: write
needs: test
if: (contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) || (github.event_name == 'schedule' && github.event.schedule == '0 0 1 * *')) && github.ref_name == 'main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get target image name
id: image
run: echo "image_name=${GITHUB_REPOSITORY,,}" >> $GITHUB_OUTPUT
- name: Build image
run: podman build --tag ${{ steps.image.outputs.image_name }}:latest .
- name: Push to ghcr.io
run: podman push --creds=${{ github.actor }}:${{ github.token }} ${{ steps.image.outputs.image_name }}:latest ghcr.io/${{ steps.image.outputs.image_name }}:latest
- name: Delete old image versions
uses: actions/delete-package-versions@v5
with:
package-name: ${{ github.event.repository.name }}
package-type: container
min-versions-to-keep: 10
122 changes: 0 additions & 122 deletions .github/workflows/main.yml

This file was deleted.

40 changes: 0 additions & 40 deletions .github/workflows/publish-container.yml

This file was deleted.

30 changes: 30 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ RUN /opt/poetry-venv/bin/poetry build --no-interaction --format wheel
RUN .venv/bin/pip install ./dist/*.whl


FROM builder as test-base

RUN apk add --no-cache gcc musl-dev linux-headers
RUN mkdir -p /data

RUN /opt/poetry-venv/bin/poetry install --no-interaction --no-root


FROM test-base as test

RUN /opt/poetry-venv/bin/poetry run poe black --check
RUN /opt/poetry-venv/bin/poetry run poe darglint
RUN /opt/poetry-venv/bin/poetry run poe mypy
RUN /opt/poetry-venv/bin/poetry run poe ruff
RUN /opt/poetry-venv/bin/poetry run poe pytest --cov

RUN /opt/poetry-venv/bin/poetry run coverage json -o /coverage/coverage.json
RUN /opt/poetry-venv/bin/poetry run coverage report --format=markdown > /coverage/coverage.md


FROM test-base as integration

ENV CERBOTTANA_SHOWDOWN_PATH=/pokemon-showdown

RUN apk add --no-cache git nodejs npm
RUN mkdir -p /pokemon-showdown

RUN /opt/poetry-venv/bin/poetry run poe pytest-real-ps-instance


FROM base as final

ENV PATH="/app/.venv/bin:$PATH"
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def showdown_server(xprocess) -> Generator[int, None, None]:
return

cwd = Path(__file__).parent.parent / "pokemon-showdown"
if cwd_ := env.str("CERBOTTANA_SHOWDOWN_PATH", default=""):
cwd = Path(cwd_)
port = unused_port()

# Clone pokemon showdown
Expand Down

0 comments on commit 91ed5f0

Please sign in to comment.