有没有办法使用 github 操作插件 docker/build-push-action@v1 设置 docker build 的路径

我有一个 docker build 动作如下

      - name: build-push
        uses: docker/build-push-action@v1
        with:
          username: ${{ DOCKER_USERNAME }}
          password: ${{ DOCKER_PASSWORD }}
          repository: <repo>
          tags: tag
          push: true
          dockerfile: ./<path-to-dockerfile>/Dockerfile

dockerfile 有向 docker 镜像添加一些文件的说明,如下所示

ADD file1 .
ADD file2 .
ADD file3 .

github的结构是:

-.github
-folder1------------
                   |
                   folder2-------------
                                       |
               -----------------------docker--------
               |             |           |          |
            file1          file2        file3      Dockerfile

问题是 GitHub 操作在 folder1 级别中查找时无法找到 file1file2file3。产生的错误是

ADD failed: file not found in build context or excluded by .dockerignore: file1: file does not exist

我不想将 dockerfile 中的路径修改为 ADD ./folder1/folder2/file1 . 。那么如何使用 docker/build-push-action@v1 从 GitHub Action 部分添加路径或更改目录?

stack overflow Is there a way to set path for docker build with the github action plugin docker/build-push-action@v1
原文答案
author avatar

接受的答案

而不是使用 docker/build-push-action@v1 使用 docker/build-push-action@v2 因为 v1 是旧版本。修改 GitHub 动作如下

  - name: Login to Docker Hub
    uses: docker/login-action@v1
    with:
      username: ${{ secrets.DOCKER_USERNAME }
      password: ${{ secrets.DOCKER_PASSWORD }}
  - name: build-push
        uses: docker/build-push-action@v2
        with:
          context: ./folder1/folder2/docker/
          tags: tag
          push: true
          dockerfile: ./<path-to-dockerfile>/Dockerfile

context 与 github 操作一起使用将解决此问题。 上下文仅适用于 v2 版本。


答案:

作者头像
- name: Login to Docker Hub
    uses: docker/login-action@v3
    with:
      username: ${{ secrets.DOCKER_USERNAME }
      password: ${{ secrets.DOCKER_PASSWORD }}
  - name: build-push
        uses: docker/build-push-action@v3
        with:
          context: ./folder1/folder2/docker/
          tags: tag
          push: true
          file: ./<path-to-dockerfile>/Dockerfile
作者头像
      context: "{{defaultContext}}:subdirectory"

https://github.com/docker/build-push-action#git-context