docker 名称错误:名称“pytest”未定义- GitHub操作

bnlyeluc  于 2023-01-12  发布在  Docker
关注(0)|答案(1)|浏览(108)

我有一个简单的Docker映像,其中包含一些python测试用例。
这是我的Dockerfile

ARG PYTHON_VERSION=3.8.3-alpine

# Use the python image as the base image
FROM python:${PYTHON_VERSION}

# upgrade pip 
RUN pip install --upgrade pip

# set CUSTOMER_NAME as environment variables
ENV CUSTOMER_NAME=${CUSTOMER_NAME:-A}

# Set the working directory 
WORKDIR /app

# Create a non-root user and add the permissions
RUN adduser -D "${USERNAME:-jananath}"

# set the username - HERE WE ARE USING A DEFAULT VALUE FOR THE USERNAME, WHICH IS "jananath"
USER "${USERNAME:-jananath}"

# copy the requirements file and install the dependencies
COPY --chown="${USERNAME:-jananath}":"${USERNAME:-jananath}" requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade --user -r requirements.txt

ENV PATH="/home/${USERNAME:-jananath}/.local/bin:${PATH}"

# copy the app code
COPY --chown=${USERNAME:-jananath}:${USERNAME:-jananath} . .

# expose the default Flask port
EXPOSE 80 

# set the entrypoint to run the app
CMD ["uvicorn", "main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"]

然后我构建图像:

docker build -t hello:v1 .

然后我运行应用程序

docker run --rm -itd -p 80:80 -e CUSTOMER_NAME=A hello:v1

然后执行pytest

docker exec -it 570e3153ab90 pytest

它成功地给出了如下pytest输出:

================================================= test session starts =================================================
platform linux -- Python 3.8.3, pytest-7.2.0, pluggy-1.0.0
rootdir: /app, configfile: pytest.ini
plugins: anyio-3.6.2
collected 2 items                                                                                                     

test_main.py ..                                                                                                 [100%]

================================================== 2 passed in 0.24s ==================================================

一切正常,除了我在GitHub操作中运行了相同的图像。

. . .
  container-test-job:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/<USERNAME>/<REPO>/hello:v2
      credentials:
          username: ${{ github.actor }}
          password: ${{ secrets.TOKEN_REPOSITORY }}    
      env:
        CUSTOMER_NAME: A
    steps:
      - name: Test
        shell: python
        run: |
          pytest
. . .

但我得到以下错误:
追溯(最近调用最后调用):文件"/_w/ temp/www.example.com ",第1行,pytest名称错误:598eea6e-7acf-4d7c-964f-a69440ea38c2.py进程已完成,退出代码为1。 name 'pytest' is not defined Error: Process completed with exit code 1.
有人能帮助我了解这里的问题以及如何解决它吗?
谢谢大家!

uinbv5nw

uinbv5nw1#

试试这段代码,然后你就可以建立镜像并把它推到你的注册表中。

name: build and test

on:
  push:
    branches:
      - "main"

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.10
      uses: actions/setup-python@v4
      with:
        python-version: 3.10.6
        architecture: "x64"

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

    - name: Test with pytest
      run: |
        pytest

相关问题