-
-
Notifications
You must be signed in to change notification settings - Fork 724
Support for private registry NPM packages by using NPM_TOKEN and .npmrc (#1390) #1405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
//registry.npmjs.org/:_authToken=${NPM_TOKEN} | ||
link-workspace-packages=false | ||
public-hoist-pattern[]=*prisma* | ||
public-hoist-pattern[]=*prisma* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v20.11.1 | ||
v20.11.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,18 @@ RUN find . -name "node_modules" -type d -prune -exec rm -rf '{}' + | |
FROM node:20.11.1-bullseye-slim@sha256:5a5a92b3a8d392691c983719dbdc65d9f30085d6dcd65376e7a32e6fe9bf4cbe AS base | ||
RUN apt-get update && apt-get install -y openssl dumb-init | ||
WORKDIR /triggerdotdev | ||
|
||
# Copy .npmrc and use the NPM_TOKEN for private registry | ||
ARG NPM_TOKEN | ||
COPY .npmrc .npmrc | ||
RUN if [ -f .npmrc ] && [ -n "${NPM_TOKEN}" ]; then \ | ||
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc; \ | ||
fi | ||
|
||
# For better security, use Docker BuildKit secrets | ||
RUN --mount=type=secret,id=npm_token \ | ||
echo "//registry.npmjs.org/:_authToken=$(cat /run/secrets/npm_token)" >> .npmrc | ||
|
||
COPY --chown=node:node .gitignore .gitignore | ||
COPY --from=pruner --chown=node:node /triggerdotdev/out/json/ . | ||
COPY --from=pruner --chown=node:node /triggerdotdev/out/pnpm-lock.yaml ./pnpm-lock.yaml | ||
|
@@ -21,6 +33,8 @@ WORKDIR /triggerdotdev | |
# Corepack is used to install pnpm | ||
RUN corepack enable | ||
ENV NODE_ENV development | ||
# Copy .npmrc again for dev-deps stage | ||
COPY .npmrc .npmrc | ||
RUN pnpm install --ignore-scripts --no-frozen-lockfile | ||
Comment on lines
35
to
38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve dependency installation in dev-deps stage
Consider the following changes:
RUN pnpm install --frozen-lockfile This change ensures that the exact versions specified in the lockfile are installed, promoting build consistency across environments. |
||
|
||
## Production deps | ||
|
@@ -29,6 +43,8 @@ WORKDIR /triggerdotdev | |
# Corepack is used to install pnpm | ||
RUN corepack enable | ||
ENV NODE_ENV production | ||
# Copy .npmrc again for production-deps stage | ||
COPY .npmrc .npmrc | ||
RUN pnpm install --prod --no-frozen-lockfile | ||
COPY --from=pruner --chown=node:node /triggerdotdev/internal-packages/database/prisma/schema.prisma /triggerdotdev/internal-packages/database/prisma/schema.prisma | ||
# RUN pnpm add @prisma/client@5.1.1 -w | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,12 @@ | ||||||||||||||||||||||||||||||||||||
#!/bin/bash | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
# Copy the .npmrc file into the build directory | ||||||||||||||||||||||||||||||||||||
echo "Copying .npmrc to build context..." | ||||||||||||||||||||||||||||||||||||
cp .npmrc /app/.npmrc | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
# Set the NPM_TOKEN environment variable for the build | ||||||||||||||||||||||||||||||||||||
export NPM_TOKEN=$1 | ||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance security and robustness of NPM_TOKEN handling. While setting the NPM_TOKEN is necessary, the current implementation has some potential issues:
Consider implementing these improvements: # Set the NPM_TOKEN environment variable for the build
-export NPM_TOKEN=$1
+if [ -z "$1" ]; then
+ echo "Error: NPM_TOKEN not provided"
+ exit 1
+fi
+
+# Basic validation of the token (adjust regex as needed)
+if ! [[ $1 =~ ^[a-zA-Z0-9_-]+$ ]]; then
+ echo "Error: Invalid NPM_TOKEN format"
+ exit 1
+fi
+
+# Use read -s to avoid showing the token in process list
+read -r -s NPM_TOKEN <<< "$1"
+export NPM_TOKEN This change adds error checking, basic token validation, and uses 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
# Run the npm install command | ||||||||||||||||||||||||||||||||||||
echo "Running npm install with NPM_TOKEN..." | ||||||||||||||||||||||||||||||||||||
pnpm install | ||||||||||||||||||||||||||||||||||||
Comment on lines
+10
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Optimize and improve error handling for package installation. The use of pnpm for package installation is fine if it's the intended package manager. However, there are a few improvements we can make:
Here's a suggested improvement: # Run the npm install command
echo "Running npm install with NPM_TOKEN..."
-pnpm install
+pnpm install --production --frozen-lockfile || { echo "Error: pnpm install failed"; exit 1; } This change will:
Note: If dev dependencies are needed, remove the 📝 Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Streamline NPM_TOKEN handling
The current implementation has two methods for adding the NPM_TOKEN to .npmrc:
To simplify and improve security, consider using only the Docker BuildKit secrets method. This approach prevents the token from being stored in intermediate layers.
Replace the current NPM_TOKEN handling with:
This change ensures that the NPM_TOKEN is only handled securely using Docker BuildKit secrets.