簡體   English   中英

"如何在 GitHub 操作中為 Docker 構建和發布設置正確的工作目錄?"

[英]How to set correct working-directory for Docker building and publishing in GitHub action?

我有這個 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/**/*"] 但這一切都會打亂我的開發工作流程,因為我在app/內運行npm run dev


編輯 2

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

嘗試更改 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將為它提供正確的文件路徑

使用 action v2,必須使用context<\/code> :

      - uses: docker/build-push-action@v2
        with:
          context: app

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM