Node.js 如何在Github操作中设置Dockerfile参数
我有一个Node.js服务的Dockerfile,我试图通过使用Github操作将其推送到我的Digitalocean注册表 My Node.js服务需要一个由我自己在npm.js注册表上托管的私有包 在我的Dockerfile中,我有一个参数:Node.js 如何在Github操作中设置Dockerfile参数,node.js,docker,npm,Node.js,Docker,Npm,我有一个Node.js服务的Dockerfile,我试图通过使用Github操作将其推送到我的Digitalocean注册表 My Node.js服务需要一个由我自己在npm.js注册表上托管的私有包 在我的Dockerfile中,我有一个参数: FROM node:14-slim ARG NODE_ENV=production EXPOSE 5000 WORKDIR /usr/src/app ARG NPM_TOKEN COPY .npmrc .npmrc COPY package
FROM node:14-slim
ARG NODE_ENV=production
EXPOSE 5000
WORKDIR /usr/src/app
ARG NPM_TOKEN
COPY .npmrc .npmrc
COPY package*.json ./
RUN npm install
RUN rm -f .npmrc
COPY src src
CMD ["npm", "start"]
以及以下.npmrc文件:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
在我的Github操作工作流中,我有两个操作。一个用于运行测试:
name: tests-user-service
on:
pull_request:
paths:
- 'user-service/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install dependencies & run tests
run: cd user-service && npm install && npm run test:ci
env:
NPM_TOKEN: ${{secrets.NPM_ACCESS_TOKEN}}
一个用于构建docker文件并将其推送到注册表:
name: deploy-user-service
on:
push:
branches:
- main
paths:
- 'user-service/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check Out Repo
uses: actions/checkout@v2
- name: Install DigitalOcean Controller
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Set up Docker Builder
uses: docker/setup-buildx-action@v1
- name: Authenticate with DigitalOcean Container Registry
run: doctl registry login --expiry-seconds 600
- name: Build and Push to DigitalOcean Container Registry
uses: docker/build-push-action@v2
with:
context: ./user-service
push: true
tags: registry.digitalocean.com/xxx/xxx:latest
env:
NPM_TOKEN: ${{secrets.NPM_ACCESS_TOKEN}}
- name: Logout from DigitalOcean Container Registry
run: doctl registry logout
现在,测试文件工作了。因此我知道NPM_访问_令牌设置正确
部署文件失败。它告诉我:
#10 [5/7] RUN npm install
#10 sha256:28b0590a43c14b889983d16b5e375f0156f7fdacc29e32fc3c219bce54e61d69
#10 0.317 Error: Failed to replace env in config: ${NPM_TOKEN}
#10 2.784 npm ERR! code E404
#10 2.790 npm ERR! 404 Not Found - GET https://registry.npmjs.org/@xxx%2fxxx - Not found
#10 2.790 npm ERR! 404
#10 2.790 npm ERR! 404 '@xxx/xxx@^0.0.11' is not in the npm registry.
#10 2.790 npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
我在Dockerfile中尝试了以下操作:
FROM node:14-slim
ARG NODE_ENV=production
EXPOSE 5000
WORKDIR /usr/src/app
ARG NPM_TOKEN
ENV NPM_TOKEN ${NPM_TOKEN}
COPY .npmrc .npmrc
COPY package*.json ./
RUN npm install
RUN rm -f .npmrc
COPY src src
CMD ["npm", "start"]
然后它出错了,告诉我:
#10 [5/7] RUN npm install
#10 sha256:28b0590a43c14b889983d16b5e375f0156f7fdacc29e32fc3c219bce54e61d69
#10 0.317 Error: Failed to replace env in config: ${NPM_TOKEN}
#10 2.784 npm ERR! code E404
#10 2.790 npm ERR! 404 Not Found - GET https://registry.npmjs.org/@xxx%2fxxx - Not found
#10 2.790 npm ERR! 404
#10 2.790 npm ERR! 404 '@xxx/xxx@^0.0.11' is not in the npm registry.
#10 2.790 npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
那么它就找不到模块了。我假设发生这种情况是因为NPM_令牌现在设置为空字符串,就像在tests操作中一样,它工作正常
你知道我还能尝试什么吗?事实上,我已经想好了。必须将
build args
添加到build和Push部分,并从中删除env
因此,不是:
- name: Build and Push to DigitalOcean Container Registry
uses: docker/build-push-action@v2
with:
context: ./user-service
push: true
tags: registry.digitalocean.com/xxx/xxx:latest
env:
NPM_TOKEN: ${{secrets.NPM_ACCESS_TOKEN}}
应使用此选项:
- name: Build and Push to DigitalOcean Container Registry
uses: docker/build-push-action@v2
with:
context: ./user-service
push: true
tags: registry.digitalocean.com/xxx/xxx:latest
build-args: NPM_TOKEN=${{secrets.NPM_ACCESS_TOKEN}}