[英]How to set a Dockerfile ARG in Github actions
我有一個 Dockerfile 用於我的 Node.js 服務之一,我嘗試通過使用 Github 操作將其推送到我的 Digitalocean 注冊表。
我的 Node.js 服務需要由我自己在 npm.js 注冊表上托管的私有 package。
在我的 Dockerfile 中,我有一個 ARG:
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_ACCESS_TOKEN 設置正確。
部署文件,那個失敗。 它告訴我這個:
#10 [5/7] RUN npm install
#10 sha256:28b0590a43c14b889983d16b5e375f0156f7fdacc29e32fc3c219bce54e61d69
#10 0.317 Error: Failed to replace env in config: ${NPM_TOKEN}
我在我的 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 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_TOKEN 現在設置了一個空字符串左右,因為在測試操作中它確實可以正常工作。
知道我還能嘗試什么嗎?
其實,我想通了。 必須將build-args
添加到 Build and 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}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.