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

时间:2020-09-08 02:20:16

标签: docker github 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

但是它正在父目录中运行,而我的Dockerfileapp/内部。

.
|- .github/workflow/ci.yaml
|- README
|- app/
   |- Dockerfile
   |- package.json
   |- package.lock.json
   |- node_modules/
   |- src/
   |- ...

我尝试设置working-directory

        working-directory: ./app

但是仍然出现相同的错误,我看到一个论坛帖子说它不适用于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

这是我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/*", "types/*"]
    }
  },
  "include": ["src/**/*"]
}

似乎还需要更改tsconfig.json之类的"include": ["app/src/**/*"]。但这一切都会搞乱我的开发工作流程,因为我正在npm run dev内运行app/


编辑2:

path解决了这个问题。 https://github.com/docker/build-push-action#path

1 个答案:

答案 0 :(得分: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

每个图书馆

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

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

因此,路径将成为github操作github.workpace中当前代码的根,添加子路径./app/Dockerfile将为其提供文件的正确路径