I have a simple Docker
image that has some python test cases.
Here's my 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"]
Then I build the image:
docker build -t hello:v1 .
Then I run the app
docker run --rm -itd -p 80:80 -e CUSTOMER_NAME=A hello:v1
Then I execute the pytest
docker exec -it 570e3153ab90 pytest
And it successfully gives the pytest
output as below:
================================================= 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 ==================================================
Everything works fine, except I run the same image in the `GitHub Actions.
. . .
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
. . .
But I get the below error:
Traceback (most recent call last): File "/__w/_temp/598eea6e-7acf-4d7c-964f-a69440ea38c2.py", line 1, in pytest NameError: name 'pytest' is not defined Error: Process completed with exit code 1.
Can someone help me understand the issue here and how to fix it?
Thank you!
try this code and after that you can build the image and push it to your registry.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.