如何在 GitHub 操作中为 Docker 构建和发布设置正确的工作目录?

Rar*_*rio 7 github docker dockerfile github-actions

我有这个 GitHub 操作作业来构建 Docker 映像并将其发布到 GitHub 注册表。

...
jobs:
  push_to_registry:
    name: Push Docker image to GitHub Packages
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v2
      - name: Push to GitHub Packages
        uses: docker/build-push-action@v1
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          dockerfile: Dockerfile
          registry: docker.pkg.github.com
          repository: myrepo/myimg
          tag_with_ref: true
Run Code Online (Sandbox Code Playgroud)

但是它在父目录中运行,而我Dockerfile的在里面app/

.
|- .github/workflow/ci.yaml
|- README
|- app/
   |- Dockerfile
   |- package.json
   |- package.lock.json
   |- node_modules/
   |- src/
   |- ...
Run Code Online (Sandbox Code Playgroud)

我尝试设置working-directory

.
|- .github/workflow/ci.yaml
|- README
|- app/
   |- Dockerfile
   |- package.json
   |- package.lock.json
   |- node_modules/
   |- src/
   |- ...
Run Code Online (Sandbox Code Playgroud)

但仍然遇到同样的错误,我看到论坛帖子说它不能很好地与uses.

如何使用 GitHub 操作在子目录中构建 Docker 映像?


编辑1

回复爱德华的回答。

是的,我也尝试过。它找到了正确的 Dockerfile,我必须重置里面的所有位置Dockerfile,例如COPY package*.json ./COPY ./app/package*.json ./. 问题是npm run build

Step 12/28 : RUN npm run build
 ---> Running in 6986869d4bdf

> @myrepo/myapp@0.0.1 build /app
> rm -rf dist && tsc --build

error TS18003: No inputs were found in config file '/app/tsconfig.json'. Specified 'include' paths were '["src/**/*"]' and 'exclude' paths were '["dist"]'.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @myrepo/myapp@0.0.1 build: `rm -rf dist && tsc --build`
npm ERR! Exit status 1
Run Code Online (Sandbox Code Playgroud)

这是我的tsconfig.json

        working-directory: ./app
Run Code Online (Sandbox Code Playgroud)

看来tsconfig.json也需要改一下了"include": ["app/src/**/*"]。但这会打乱我的开发工作流程,因为我正在npm run dev里面运行app/


编辑2

path解决它。https://github.com/docker/build-push-action#path

Edw*_*ero 0

尝试更改github操作调用参数dockerfile目录。假设您的 dockerfile 已正确设置以了解所有构建文件的位置。这应该允许此操作正确地拾取它。

  - name: Push to GitHub Packages
    uses: docker/build-push-action@v1
    with:
      username: ${{ github.actor }}
      password: ${{ secrets.GITHUB_TOKEN }}
      dockerfile: ./app/Dockerfile
      registry: docker.pkg.github.com
      repository: myrepo/myimg
      tag_with_ref: true
Run Code Online (Sandbox Code Playgroud)

根据图书馆

dockerfile Dockerfile 的路径。默认为 {path}/Dockerfile

请注意,设置时此路径不是相对于路径输入的,而是相对于当前工作目录的。

因此路径将是 github actions 中当前代码的根目录github.workpace,添加子路径./app/Dockerfile将为它提供正确的文件路径