Search code examples
dockergithubdockerfilegithub-actions

Is there a way to set path for docker build with the github action plugin docker/build-push-action@v1


I have a docker build action as below

      - 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

The dockerfile has instructions to ADD few files to the docker image as below

ADD file1 .
ADD file2 .
ADD file3 .

the structure of github is:

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

The issue is that the GitHub action is unable to find file1, file2 and file3 as it is looking in the level of folder1. The error generated is

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

I do not want to modify the path in dockerfile as ADD ./folder1/folder2/file1 .. So how do I add path or change directory from the GitHub Action part using docker/build-push-action@v1 ?


Solution

  • Instead of using docker/build-push-action@v1 use docker/build-push-action@v2 as v1 is an older version. Modify the GitHub action as below

      - 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
    

    using context with the github action will solve this issue. Context is available only with v2 verison.