简体   繁体   中英

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

I have this GitHub action job to build and publish Docker image to GitHub registry.

...
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

However it is running at parent directory and my Dockerfile is inside app/ .

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

I tried setting working-directory :

        working-directory: ./app

But still got the same error and I saw a forum post said it doesn't work well with uses .

How do I build Docker image inside sub-directory with GitHub action?


Edit 1

Reply to Edward's answer.

Yep, I tried it also. It found the correct Dockerfile, and I have to reset all location inside Dockerfile , such as COPY package*.json ./ to COPY ./app/package*.json ./ . Problem is 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

This is my 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/**/*"]
}

Seems like tsconfig.json also needs to be changed, "include": ["app/src/**/*"] . But it will all mess up my development workflow, because I'm running npm run dev inside app/ .


Edit 2

path solves it. https://github.com/docker/build-push-action#path

Try changing the the github action call parameter dockerfile directory. With the assumption that your dockerfile is setup properly to know where all the build files are at. That should allow this action to properly pick it up.

  - 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

Per the library

dockerfile Path to the Dockerfile. Defaults to {path}/Dockerfile

Note when set this path is not relative to the path input but is instead relative to the current working directory.

So path will be the root of your current code in github actions github.workpace and adding subpath ./app/Dockerfile will provide it the proper path to file

With action v2, one has to use context<\/code> :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM