GitHub Actions中构建Docker后无需拉取镜像运行pytest的方法
无需拉取镜像直接运行pytest的优化方案
你完全可以跳过从远程仓库拉取镜像的步骤,直接用构建后留在本地的镜像跑pytest,核心是让Docker构建完成后把镜像加载到本地Docker环境里。
具体修改方法
在你的Build步骤中添加load: true参数,使用docker/build-push-action@v4构建镜像时,会同时将镜像推送到远程仓库和加载到本地Docker daemon中,后续的Pytest步骤直接调用本地镜像即可:
- name: Build uses: docker/build-push-action@v4 with: context: . push: true load: true # 新增:将构建的镜像加载到本地Docker环境 tags: my_registry:${{ github.run_number }} cache-from: | type=registry,ref=my_registry:cache cache-to: | type=registry,ref=my_registry:cache,mode=max - name: Pytest run: docker run my_registry:${{ github.run_number }} pytest
额外优化思路
如果你的流程允许先测试再推送镜像,还可以调整顺序:先构建本地镜像(不推送),跑完pytest确认没问题后再推送到仓库,这样能省掉一次推送+拉取的时间,同时避免把有问题的镜像推送到仓库:
- name: Build local image uses: docker/build-push-action@v4 with: context: . push: false # 先不推送镜像 tags: my_registry:${{ github.run_number }} cache-from: | type=registry,ref=my_registry:cache cache-to: | type=registry,ref=my_registry:cache,mode=max - name: Pytest run: docker run my_registry:${{ github.run_number }} pytest - name: Push image to registry uses: docker/build-push-action@v4 with: context: . push: true tags: my_registry:${{ github.run_number }} cache-from: | type=registry,ref=my_registry:cache
内容的提问来源于stack exchange,提问作者katakuri
相关产品推荐
相关产品推荐

