我们目前在应用程序中使用 Font Awesome 库来存储字体和图标。它在
.npmrc
文件中注册,如下所示:
@fortawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:_authToken=${FONT_AWESOME_TOKEN}
我们的目标是出于安全目的从 GitHub 机密中检索
FONT_AWESOME_TOKEN
并将其注入 Dockerfile 中。
截至今天,该解决方案的 Docker 映像是使用 Dockerfile 创建的,如下所述:
# base node image
FROM node:16-bullseye-slim as base
# set for base and all layer that inherit from it
ENV NODE_ENV production
# Install all node_modules, including dev dependencies
FROM base as deps
EXPOSE 3010
ADD package.json package-lock.json .npmrc ./
RUN npm install --mount=type=secret,id=npmrc,target=/root/.npmrc
我们使用 GitHub Actions 来实现 CI/CD 目的,并且上面使用 Dockerfile 创建的 Docker 映像将传递到部署到 Azure 容器注册表的构建过程中。
- name: '🐋 Build and push :latest + :tag (if "releaseToDev-")'
run: |
az acr login --name testdevacr # DEV ACR
az acr build --secret-build-arg --secret-build-arg
FONT_AWESOME_TOKEN=${{ secrets.FONT_AWESOME_TOKEN }} -t test-web-app:latest -t test-web-
app:${{steps.tag-result.outputs.result}} -r testdevacr -f Dockerfile .
问题是 GitHub Actions 内的
npm build
失败并显示以下错误消息:
Step 8/22 : RUN npm install -g [email protected] --mount=type=secret,id=npmrc,target=/root/.npmrc
---> Running in d6d81255b755
npm WARN deprecated @npmcli/[email protected]: This functionality has been moved to @npmcli/fs
npm notice
npm notice New major version of npm available! 8.19.4 -> 10.2.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v10.2.0>
npm notice Run `npm install -g [email protected]` to update!
npm notice
npm ERR! code E401
npm ERR! Unable to authenticate, your authentication token seems to be invalid.
npm ERR! To correct this please trying logging in again with:
npm ERR! npm login
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2023-10-04T07_23_59_609Z-debug-0.log
The command '/bin/sh -c npm install --mount=type=secret,id=npmrc,target=/root/.npmrc' returned a non-zero code: 1
2023/10/04 07:24:37 Container failed during run: build. No retries remaining.
failed to run step ID: build: exit status 1
关于如何以可识别的方式传递 Font Awesome 令牌有什么建议吗?
我们最终可以使用 ARG 来解决这个问题
我们通过添加如下所示的
--secret-build-arg
变量来编辑 Acr.yml 文件命令。
az acr build --secret-build-arg FONT_AWESOME_TOKEN=${{ secrets.FONT_AWESOME_TOKEN }} -t test-web-app:latest -t test-web-
app:${{steps.tag-result.outputs.result}} -r testdevacr -f Dockerfile .
一旦我们在 Dockerfile 中定义了变量
ARG AZURE_ARTIFACTS_TOKEN
ARG FONT_AWESOME_TOKEN
它可以轻松阅读和识别,并且像魅力一样工作