浏览代码

Pipeline improvements (#3893)

This PR implements a few improvements for the GitHub Action workflows
test and publish:

* use `GITHUB_OUTPUT` environment variable instead of deprecated
`set-output` command
* simplify getting version
* remove unused ids for steps
* update action versions where possible
* add a cache for the docker layers
* fix typo in description
* add concurrency group to cancel test runs on the same commit
* set up Poetry cache in the test workflow
* delete uploading screenshots
* add conftest.py to three examples to fix warning about unknown pytest
mark
* fix deprecated `datetime.utcnow()` usage
* add `asyncio_default_fixture_loop_scope` setting for pytests (set to
`function`)
* minor deprecation changes to the `release.dockerfile` that Docker
warns about

---------

Co-authored-by: Falko Schindler <falko@zauberzeug.com>
Paula Kammler 7 月之前
父节点
当前提交
6dc472ed32

+ 28 - 26
.github/workflows/publish.yml

@@ -9,7 +9,6 @@ on:
 jobs:
   pypi:
     runs-on: ubuntu-latest
-
     steps:
       - uses: actions/checkout@v4
       - name: set up Python
@@ -19,9 +18,8 @@ jobs:
       - name: set up Poetry
         uses: abatilo/actions-poetry@v3
       - name: get version
-        id: get_version
-        run: echo "VERSION=$(echo ${GITHUB_REF/refs\/tags\//})" >> $GITHUB_ENV
-      - name: set version
+        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
+      - name: Set version
         run: poetry version ${{ env.VERSION }}
       - name: publish
         env:
@@ -29,7 +27,6 @@ jobs:
         run: poetry publish --build
       - name: Create GitHub release entry
         uses: softprops/action-gh-release@v2
-        id: create_release
         with:
           draft: true
           prerelease: false
@@ -44,7 +41,6 @@ jobs:
   docker:
     needs: pypi
     runs-on: ubuntu-latest
-
     steps:
       - uses: actions/checkout@v4
       - name: Prepare
@@ -68,13 +64,11 @@ jobs:
           fi
 
           # Set output parameters.
-          echo ::set-output name=tags::${TAGS}
-          echo ::set-output name=docker_image::${DOCKER_IMAGE}
-          echo ::set-output name=version::${VERSION}
+          echo "tags=${TAGS}" >> $GITHUB_OUTPUT
+          echo "docker_image=${DOCKER_IMAGE}" >> $GITHUB_OUTPUT
+          echo "version=${VERSION}" >> $GITHUB_OUTPUT
       - name: Set up QEMU
-        uses: docker/setup-qemu-action@master
-        with:
-          platforms: all
+        uses: docker/setup-qemu-action@v3
       - name: Login to DockerHub
         if: github.event_name != 'pull_request'
         uses: docker/login-action@v3
@@ -82,11 +76,15 @@ jobs:
           username: ${{ secrets.DOCKER_USERNAME }}
           password: ${{ secrets.DOCKER_PASSWORD }}
       - name: Set up Docker Buildx
-        id: buildx
-        uses: docker/setup-buildx-action@master
+        uses: docker/setup-buildx-action@v3
+      - name: Cache Docker layers
+        uses: actions/cache@v3
         with:
-          install: true
-      - name: Build
+          path: /tmp/.buildx-cache
+          key: ${{ runner.os }}-buildx-${{ github.sha }}
+          restore-keys: |
+            ${{ runner.os }}-buildx-
+      - name: Build and push
         uses: docker/build-push-action@v6
         with:
           context: .
@@ -95,6 +93,15 @@ jobs:
           push: true
           tags: ${{ steps.prep.outputs.tags }}
           build-args: VERSION=${{ steps.prep.outputs.version }}
+          cache-from: type=local,src=/tmp/.buildx-cache
+          cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
+      # Temp fix
+      # https://github.com/docker/build-push-action/issues/252
+      # https://github.com/moby/buildkit/issues/1896
+      - name: Move cache
+        run: |
+          rm -rf /tmp/.buildx-cache
+          mv /tmp/.buildx-cache-new /tmp/.buildx-cache
 
       # Uploading the README.md is not a core feature of docker/build-push-action yet
       - name: Update README
@@ -105,25 +112,20 @@ jobs:
         with:
           destination_container_repo: zauberzeug/nicegui
           provider: dockerhub
-          short_description: "Web Based User Interface für Python with Buttons, Dialogs, Markdown, 3D Scences and Plots"
+          short_description: "Web Based User Interface for Python with Buttons, Dialogs, Markdown, 3D Scenes and Plots"
 
   update_metadata:
     needs: docker
     runs-on: ubuntu-latest
     steps:
-      - name: Checkout repository
-        uses: actions/checkout@v4
+      - uses: actions/checkout@v4
         with:
           ref: main
-
-      - name: Set up Python
-        uses: actions/setup-python@v5
+      - uses: actions/setup-python@v5
         with:
-          python-version: 3.11
-
+          python-version: "3.11"
       - name: Update version in pyproject.toml
-        run: python .github/workflows/update_pyproject.py $(echo ${GITHUB_REF/refs\/tags\//})
-
+        run: python .github/workflows/update_pyproject.py ${GITHUB_REF#refs/tags/}
       - name: Commit and push changes
         run: |
           git config --global user.name "github-actions[bot]"

+ 12 - 15
.github/workflows/test.yml

@@ -2,6 +2,10 @@ name: Run Tests
 
 on: [push]
 
+concurrency:
+  group: ${{ github.workflow }}-${{ github.sha }}
+  cancel-in-progress: true
+
 jobs:
   test:
     strategy:
@@ -12,35 +16,28 @@ jobs:
     timeout-minutes: 40
     steps:
       - uses: actions/checkout@v4
+      - name: set up Poetry
+        uses: abatilo/actions-poetry@v3
       - name: set up Python
         uses: actions/setup-python@v5
         with:
           python-version: ${{ matrix.python }}
-      - name: set up Poetry
-        uses: abatilo/actions-poetry@v3
+          cache: "poetry"
       - name: install dependencies
         run: |
           set -x
-          poetry config virtualenvs.create false
           poetry install --all-extras
           # install packages to run the examples
-          pip install opencv-python opencv-contrib-python-headless httpx isort replicate langchain openai simpy tortoise-orm
-          pip install -r tests/requirements.txt
+          poetry run pip install opencv-python opencv-contrib-python-headless httpx isort replicate langchain openai simpy tortoise-orm
+          poetry run pip install -r tests/requirements.txt
           # try fix issue with importlib_resources
-          pip install importlib-resources
+          poetry run pip install importlib-resources
       - name: test startup
-        run: ./test_startup.sh
+        run: poetry run ./test_startup.sh
       - name: setup chromedriver
         uses: nanasess/setup-chromedriver@v2.2.2
       - name: pytest
-        run: pytest
-      - name: upload screenshots
-        uses: actions/upload-artifact@v4
-        env:
-          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        with:
-          name: pytest-screenshots
-          path: tests/screenshots
+        run: poetry run pytest
 
   slack:
     needs:

+ 1 - 0
examples/authentication/conftest.py

@@ -0,0 +1 @@
+pytest_plugins = ['nicegui.testing.plugin']

+ 0 - 2
examples/authentication/test_authentication.py

@@ -6,8 +6,6 @@ from . import main
 
 # pylint: disable=missing-function-docstring
 
-pytest_plugins = ['nicegui.testing.user_plugin']
-
 
 @pytest.mark.module_under_test(main)
 async def test_login_logoff(user: User) -> None:

+ 1 - 0
examples/chat_app/conftest.py

@@ -0,0 +1 @@
+pytest_plugins = ['nicegui.testing.plugin']

+ 1 - 1
examples/chat_app/main.py

@@ -21,7 +21,7 @@ def chat_messages(own_id: str) -> None:
 @ui.page('/')
 async def main():
     def send() -> None:
-        stamp = datetime.utcnow().strftime('%X')
+        stamp = datetime.now().strftime('%X')
         messages.append((user_id, avatar, text.value, stamp))
         text.value = ''
         chat_messages.refresh()

+ 0 - 2
examples/chat_app/test_chat_app.py

@@ -7,8 +7,6 @@ from nicegui.testing import User
 
 from . import main
 
-pytest_plugins = ['nicegui.testing.user_plugin']
-
 
 @pytest.mark.module_under_test(main)
 async def test_basic_startup_appearance(user: User) -> None:

+ 1 - 0
examples/todo_list/conftest.py

@@ -0,0 +1 @@
+pytest_plugins = ['nicegui.testing.plugin']

+ 0 - 2
examples/todo_list/test_todo_list.py

@@ -6,8 +6,6 @@ from . import main
 
 # pylint: disable=missing-function-docstring
 
-pytest_plugins = ['nicegui.testing.user_plugin']
-
 
 @pytest.mark.module_under_test(main)
 async def test_checking_items(user: User) -> None:

+ 1 - 0
pyproject.toml

@@ -78,6 +78,7 @@ build-backend = "poetry.core.masonry.api"
 addopts = "--driver Chrome"
 asyncio_mode = "auto"
 testpaths = ["tests"]
+asyncio_default_fixture_loop_scope = "function"
 
 [tool.mypy]
 python_version = "3.8"

+ 3 - 3
release.dockerfile

@@ -1,4 +1,4 @@
-FROM python:3.11-slim as builder
+FROM python:3.11-slim AS builder
 
 RUN apt-get update && \
     DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC \
@@ -11,7 +11,7 @@ RUN python -m pip install --upgrade pip
 
 RUN python -m pip install --upgrade libsass
 
-FROM python:3.11-slim as release
+FROM python:3.11-slim AS release
 COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
 ARG VERSION
 
@@ -39,7 +39,7 @@ COPY docker-entrypoint.sh /resources
 RUN chmod 777 /resources/docker-entrypoint.sh
 
 EXPOSE 8080
-ENV PYTHONUNBUFFERED True
+ENV PYTHONUNBUFFERED=True
 
 ENTRYPOINT ["/resources/docker-entrypoint.sh"]
 CMD ["python", "main.py"]