【问题标题】:How to test my Dockerfile for my python project using GitHub actions?如何使用 GitHub 操作为我的 python 项目测试我的 Dockerfile?
【发布时间】:2021-07-12 00:31:39
【问题描述】:
  • 有时我们在编写 docker 文件时会出错。如果 Dockerfile 有错误,docker build 会失败。

  • 有时我们可能会忘记在 Dockerfile 中指定依赖项。举个例子吧。

假设,我有一个 Python 脚本,可以截取任何网页(提供其 URL)的屏幕截图。

  • 现在,在我的代码中,我使用的是 pyppeeteer(无头 chrome/chromium 自动化库(puppeteer 的非官方端口)
  • pyppeeteer 使用铬和铬驱动程序。这些已经安装在我的机器上。因此,运行 pytest 将通过我的本地开发环境。
  • 我忘记在 dockerfile 中指定 RUN 命令,这将安装 chromium 和 chrome 驱动程序。因此在容器内运行测试将失败。 (尽管docker build 会成功。)

我想自动化构建 docker 镜像和在容器中运行测试的任务。

在本地机器上,我可以运行docker build -t myproj .来构建。

为了测试,我可以运行 docker run -it myproj pytest(如果我忘记添加安装 chromium 和 chromedriver 的 RUN,那么我的 pytest 将在容器内失败)

我希望我能够解释我的目的。

通常,在github操作中,python源代码可以在ubuntu、mac、windows等上运行。 除了不同的操作系统,我还想构建和测试我的 dockerfile。

【问题讨论】:

    标签: python docker testing github-actions


    【解决方案1】:

    经过更多的实验和研究,我发现解决我的问题的方法很简单。

    .github/workflows 文件夹中创建一个docker-build-test.yml 文件。

     
    name: Docker build and test
    
    on:
      workflow_dispatch 
    # you can trigger on anything you want
    
    jobs:
      build:
       runs-on: ubuntu-latest
      
       steps:
         - uses: actions/checkout@v2
         - name: Build Docker image 
           run: docker build -t samplepy .
         - name: Run tests inside the container
           run: docker run samplepy poetry run pytest
    

    非常简单,因为 github 的 ubuntu-latest 虚拟机已经安装和配置了 docker。您只需运行 docker 命令,一切正常。

    而且,我还用一个虚拟 python 项目测试了上述工作流程。

    【讨论】:

    • 欢迎发表你的答案,我很想知道做这件事的其他方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2013-04-27
    相关资源
    最近更新 更多