Skip to content

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

Conversation

yadavshubham01
Copy link
Contributor

@yadavshubham01 yadavshubham01 commented Oct 12, 2024

Closes #

✅ Checklist

  • I have followed every step in the contributing guide
  • The PR title follows the convention.
  • I ran and tested the code works

Changelog

#Added functionality to copy the .npmrc file into the Docker build context.
#Added support for the NPM_TOKEN environment variable to authenticate with private NPM registries.
#Updated Dockerfile to ensure private dependencies are installed using npm i during the build phase.

[Short description of what has changed]
This PR adds support for installing private NPM packages by using the .npmrc file and NPM_TOKEN. It includes modifications to the Dockerfile and ensures that private packages are handled during the build phase.

💯

Summary by CodeRabbit

  • New Features
    • Enhanced release process with improved npm authentication.
    • Introduced a new script for building Docker extensions.
  • Configuration Updates
    • Added npm authentication token configuration in .npmrc.
    • Updated Dockerfile to streamline the build process with new stages.
  • Chores
    • Minor formatting adjustments in configuration files.
    • Added a newline character in the .nvmrc file.

Copy link

changeset-bot bot commented Oct 12, 2024

⚠️ No Changeset found

Latest commit: 126b78f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Oct 12, 2024

Walkthrough

The pull request introduces several modifications to enhance the repository's build and release processes. Key changes include the addition of an NPM_TOKEN for npm authentication in the GitHub Actions workflow and the .npmrc file. A new Dockerfile structure introduces multiple stages for dependency management and application pruning. Additionally, a new script, build-extension.sh, is added to streamline the build process for a Docker extension by managing npm configurations and installations.

Changes

File Change Summary
.github/workflows/release.yml Added NPM_TOKEN secret for npm authentication; updated publish job to disable automatic publishing (if: false).
.npmrc Added authentication token configuration for npm registry; ensured public-hoist-pattern[]=*prisma* is present.
.nvmrc Added newline at the end; version remains v20.11.1.
docker/Dockerfile Introduced new stages (pruner, dev-deps, production-deps, builder, runner); added ARG NPM_TOKEN.
docker/scripts/build-extension.sh New script to copy .npmrc, set NPM_TOKEN, and execute pnpm install.

Possibly related PRs

🐇 In the code, I hop and play,
With tokens and scripts, I pave the way.
From Docker to npm, all in a line,
Building my dreams, oh how they shine!
A sprinkle of magic, a dash of cheer,
In the world of code, I have no fear! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (6)
.npmrc (1)

3-3: LGTM! Consider documenting the Prisma hoisting pattern.

The public-hoist-pattern for Prisma packages is correctly maintained. This is important for managing Prisma dependencies in a monorepo setup.

Consider adding a comment explaining the purpose of this hoisting pattern, especially if it's crucial for your project setup. For example:

+ # Hoist Prisma packages to the root to avoid multiple instances in monorepo
public-hoist-pattern[]=*prisma*
docker/scripts/build-extension.sh (1)

1-5: LGTM with a minor suggestion for improvement.

The initial setup looks good. The shebang is correct, and the script provides feedback about copying the .npmrc file. However, consider adding error handling to ensure the .npmrc file exists and the copy operation succeeds.

Here's a suggested improvement:

 #!/bin/bash

+set -e  # Exit immediately if a command exits with a non-zero status
+
 # Copy the .npmrc file into the build directory
 echo "Copying .npmrc to build context..."
-cp .npmrc /app/.npmrc
+cp .npmrc /app/.npmrc || { echo "Error: Failed to copy .npmrc file"; exit 1; }

This change will make the script exit if the copy operation fails, preventing further execution with an incomplete setup.

.github/workflows/release.yml (1)

Line range hint 1-105: Request clarification on the overall release strategy.

The changes in this file introduce npm authentication using NPM_TOKEN, which aligns with the goal of supporting private registry NPM packages. However, the disabling of automatic publishing seems to contradict the overall objective of enhancing the build and release processes.

Could you please provide a comprehensive explanation of the intended release strategy? This should cover:

  1. How the NPM_TOKEN will be used throughout the workflow.
  2. The reasoning behind disabling automatic publishing.
  3. The planned process for managing releases in the absence of automatic publishing.
  4. Any interim steps or manual processes that will be used.
  5. The timeline or conditions for re-enabling automatic publishing.

This information will help ensure that the changes align with the project's long-term goals and that all team members understand the new release process.

docker/Dockerfile (3)

31-32: Redundant Copy of .npmrc in dev-deps Stage

Copying .npmrc again in the dev-deps stage may be unnecessary since it's already included in the base stage. This redundancy can be eliminated to simplify the Dockerfile.

If the copy is required due to layer scoping in multi-stage builds, consider adding a comment to explain the necessity for future maintainers.


41-42: Redundant Copy of .npmrc in production-deps Stage

Similar to the dev-deps stage, copying .npmrc again in the production-deps stage appears redundant if it's already present from the base stage.

Clarify if this duplication is required due to Docker's layer caching or stage isolation. Adding an explanatory comment can improve maintainability.


17-19: Simplify the Conditional .npmrc Modification

Since .npmrc is being copied explicitly, the check for its existence might be unnecessary. Simplifying the conditional can make the Dockerfile cleaner.

You can remove the [ -f .npmrc ] condition:

RUN if [ -n "${NPM_TOKEN}" ]; then \
      echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc; \
    fi

This assumes .npmrc exists due to the earlier COPY command.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1c80a74 and 47b11ff.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (5)
  • .github/workflows/release.yml (1 hunks)
  • .npmrc (1 hunks)
  • .nvmrc (1 hunks)
  • docker/Dockerfile (3 hunks)
  • docker/scripts/build-extension.sh (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .nvmrc
🧰 Additional context used
🔇 Additional comments (5)
.npmrc (1)

1-1: LGTM! Verify NPM_TOKEN in CI/CD environment.

The addition of the auth token configuration aligns with the PR objective of supporting private registry NPM packages. Using an environment variable for the token is a good security practice.

Please ensure that the NPM_TOKEN environment variable is properly set in your CI/CD environment. You can verify this by running the following command in your CI/CD pipeline:

docker/scripts/build-extension.sh (1)

1-12: Overall improvements and security consideration

The script achieves its main objectives of setting up the npm environment and installing dependencies. However, there are a few overall improvements and a security consideration to address:

  1. Add error handling throughout the script to catch and report failures at each step.
  2. Consider adding a cleanup step to remove sensitive information after installation.
  3. Use set -e at the beginning of the script to exit on any command failure.

Here's a suggested structure for the improved script:

#!/bin/bash

set -e

cleanup() {
    echo "Cleaning up..."
    rm -f /app/.npmrc
    unset NPM_TOKEN
}

trap cleanup EXIT

# ... (rest of the script with improvements from previous comments)

# After pnpm install
echo "Installation completed successfully."

This structure ensures cleanup of sensitive information even if the script exits prematurely.

To verify the impact of these changes on the build process, you may want to run:

This will help ensure that NPM_TOKEN is not hardcoded anywhere else in the codebase, which could pose a security risk.

✅ Verification successful

Verification Successful

The script does not contain any hardcoded NPM_TOKEN values and appropriately uses environment variables to manage sensitive information.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any hardcoded NPM_TOKEN in the codebase

# Test: Search for NPM_TOKEN assignments
rg 'NPM_TOKEN\s*=' --type sh

Length of output: 84


Script:

#!/bin/bash
# Description: Check for any hardcoded tokens or sensitive information in .npmrc files

# Test: Search for token-related entries in .npmrc
rg '(_token|auth|_auth)' --type markdown --file '.npmrc'

Length of output: 281


Script:

#!/bin/bash
# Description: Check for any hardcoded tokens or sensitive information in .npmrc files

# Test: Search for token-related entries in .npmrc
rg '(_authToken|authToken|NPM_TOKEN)' .npmrc

Length of output: 92

.github/workflows/release.yml (2)

Line range hint 103-105: Clarify the rationale for disabling automatic publishing.

The addition of if: false effectively disables the automatic publishing process. This seems to contradict the PR objective of enhancing the build and release processes.

Could you please provide more information on:

  1. The reasoning behind disabling automatic publishing at this time?
  2. The plans and timeline for re-enabling this feature?
  3. How releases will be managed in the interim?

To verify the impact of this change, we can run the following script to check for any manual publishing steps that might have been added elsewhere:

#!/bin/bash
# Description: Check for manual publishing steps in workflow files

echo "Searching for manual publishing steps in workflow files:"
rg --type yaml "npm publish" .github/workflows -C 3

echo "Checking for other jobs that might handle publishing:"
rg --type yaml "jobs:.*publish" .github/workflows -C 5

45-46: Approve the addition of NPM_TOKEN, but clarification needed.

The addition of the NPM_TOKEN environment variable is a good step towards supporting private registry NPM packages. However, it's not immediately clear how pnpm utilizes this environment variable during the dependency installation process.

Could you please clarify how pnpm makes use of the NPM_TOKEN environment variable? If it's not directly used by pnpm, consider moving this environment variable to a more relevant step where it's actively utilized.

To verify the usage of NPM_TOKEN in the project, we can run the following script:

✅ Verification successful

NPM_TOKEN Usage Verified

The NPM_TOKEN environment variable is correctly utilized in the .npmrc file for npm authentication. No further issues detected.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for NPM_TOKEN usage in project files

# Search for NPM_TOKEN usage in project files
echo "Searching for NPM_TOKEN usage:"
rg --type yaml --type javascript --type typescript "NPM_TOKEN" -C 3

# Check if .npmrc file exists and its contents
echo "Checking .npmrc file:"
if [ -f ".npmrc" ]; then
    cat .npmrc
else
    echo ".npmrc file not found"
fi

Length of output: 445


Script:

#!/bin/bash
# Description: Check for NPM_TOKEN usage in project files

# Search for NPM_TOKEN usage in project files
echo "Searching for NPM_TOKEN usage:"
rg --type yaml --type javascript --type typescript "NPM_TOKEN" -C 3

# Check if .npmrc file exists and its contents
echo "Checking .npmrc file:"
if [ -f ".npmrc" ]; then
    cat .npmrc
else
    echo ".npmrc file not found"
fi

Length of output: 367

docker/Dockerfile (1)

15-19: Add .npmrc to .dockerignore

Including .npmrc in the build context can inadvertently copy sensitive information into the image if the file contains other settings or credentials.

Ensure that .npmrc is listed in your .dockerignore file to prevent unintentional inclusion. Since you're copying it explicitly in the Dockerfile, the build context doesn't need to include it implicitly.

.npmrc Outdated
Comment on lines 0 to 3
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
link-workspace-packages=false
public-hoist-pattern[]=*prisma* No newline at end of file
public-hoist-pattern[]=*prisma*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding common npm configurations.

The current .npmrc file covers the essential configurations for your use case. However, you might want to consider adding some common npm configurations that could be beneficial for your project:

  1. Set a default save prefix:
save-exact=true
  1. Configure package-lock:
package-lock=true
  1. Set a default node version:
engine-strict=true
  1. Configure npm to use a specific registry for certain scopes:
@myorg:registry=https://registry.myorg.com/

These are just suggestions. Please add only the configurations that make sense for your project's needs.

Comment on lines +10 to +12
# Run the npm install command
echo "Running npm install with NPM_TOKEN..."
pnpm install
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  1. Consider adding the --production flag to avoid installing dev dependencies if this is for a production build.
  2. Add error handling to catch installation failures.
  3. Optionally, add a --frozen-lockfile flag to ensure consistent installations.

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:

  • Only install production dependencies
  • Ensure consistent installations across environments
  • Exit the script if the installation fails

Note: If dev dependencies are needed, remove the --production flag.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Run the npm install command
echo "Running npm install with NPM_TOKEN..."
pnpm install
# Run the npm install command
echo "Running npm install with NPM_TOKEN..."
pnpm install --production --frozen-lockfile || { echo "Error: pnpm install failed"; exit 1; }

Comment on lines +7 to +8
# Set the NPM_TOKEN environment variable for the build
export NPM_TOKEN=$1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Enhance security and robustness of NPM_TOKEN handling.

While setting the NPM_TOKEN is necessary, the current implementation has some potential issues:

  1. It assumes the token is always provided as the first argument.
  2. There's no validation of the token.
  3. The token is exposed in the environment variables.

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 read -s to reduce token visibility.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Set the NPM_TOKEN environment variable for the build
export NPM_TOKEN=$1
# Set the NPM_TOKEN environment variable for the build
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

Comment on lines 15 to 19
ARG NPM_TOKEN
COPY .npmrc .npmrc
RUN if [ -f .npmrc ]; then \
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc; \
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Security Concern: Avoid Exposing NPM_TOKEN in Build Layers

Including NPM_TOKEN directly in the Dockerfile can inadvertently expose it in the image layers, which poses a security risk if the image is ever shared publicly. Consider using Docker's build-time secrets to handle sensitive information securely.

You can modify the Dockerfile to use build-time secrets with Docker BuildKit:

# Use the secret in the RUN command without storing it in a layer
RUN --mount=type=secret,id=npm_token \
    echo "//registry.npmjs.org/:_authToken=$(cat /run/secrets/npm_token)" >> .npmrc

Then, build the image with the secret:

DOCKER_BUILDKIT=1 docker build --secret id=npm_token,src=npm_token.txt .

This approach ensures that NPM_TOKEN is not written to any layers in the final image.


⚠️ Potential issue

Conditional Check for NPM_TOKEN

If NPM_TOKEN is not provided, the script appends an empty token to .npmrc, which might cause authentication issues with npm. It's important to check if NPM_TOKEN is set before appending it.

Update the conditional to verify that NPM_TOKEN is not empty:

RUN if [ -f .npmrc ] && [ -n "${NPM_TOKEN}" ]; then \
      echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc; \
    fi

This ensures the authentication token is only added when it exists.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
docker/Dockerfile (2)

Line range hint 45-53: Optimize production dependency installation and Prisma setup

  1. Copying .npmrc again might be redundant if it's already in the base stage.
  2. The pnpm install command uses the --no-frozen-lockfile flag, which might lead to inconsistent builds.
  3. There's a commented out line for adding @prisma/client.

Consider the following changes:

  1. Remove the redundant COPY of .npmrc if it's not necessary.
  2. Modify the pnpm install command to ensure reproducible builds:
RUN pnpm install --prod --frozen-lockfile
  1. Remove the commented out line:
# Remove this line if it's no longer needed
# RUN pnpm add @prisma/client@5.1.1 -w

These changes will improve build consistency and clean up the Dockerfile.


Line range hint 74-93: LGTM: Runner stage is well-structured with a minor suggestion

The runner stage effectively combines the necessary components from previous stages to create the final image. Good practices observed include:

  • Using dumb-init for proper signal handling
  • Setting the user to node for improved security

For consistency with previous stages, consider using the same format for the FROM instruction:

FROM node:20.11.1-bullseye-slim@sha256:5a5a92b3a8d392691c983719dbdc65d9f30085d6dcd65376e7a32e6fe9bf4cbe AS runner

This change ensures that the exact same base image is used across all stages.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 47b11ff and 126b78f.

📒 Files selected for processing (1)
  • docker/Dockerfile (3 hunks)
🧰 Additional context used
🔇 Additional comments (3)
docker/Dockerfile (3)

Line range hint 1-6: LGTM: Pruner stage is well-structured

The pruner stage effectively reduces the build context size by using turbo prune and removing node_modules. This approach optimizes the subsequent stages of the build process.


Line range hint 56-71: LGTM: Builder stage is well-structured

The builder stage effectively combines the necessary files and dependencies from previous stages to build the webapp. The use of --filter=webapp... in the build command is a good practice for optimizing the build process by focusing only on the required packages.


Line range hint 1-93: Summary: Well-structured Dockerfile with room for optimization

Overall, the Dockerfile is well-organized with clear separation of concerns across multiple stages. Key strengths include the use of a pruner stage, proper handling of dependencies, and good security practices in the runner stage.

Main areas for improvement:

  1. Streamline NPM_TOKEN handling by using only Docker BuildKit secrets.
  2. Ensure consistent and reproducible builds by using --frozen-lockfile with pnpm install commands.
  3. Remove redundant COPY instructions for .npmrc in dev-deps and production-deps stages.
  4. Clean up commented out lines and ensure consistency in FROM instructions across stages.

Addressing these points will further enhance the security, consistency, and efficiency of the build process.

Comment on lines +13 to +23

# 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 link
Contributor

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:

  1. Using an ARG and conditional check (lines 15-19)
  2. Using Docker BuildKit secrets (lines 22-23)

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:

# Remove these lines
ARG NPM_TOKEN
COPY .npmrc .npmrc
RUN if [ -f .npmrc ] && [ -n "${NPM_TOKEN}" ]; then \
      echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc; \
    fi

# Keep only this part
COPY .npmrc .npmrc
RUN --mount=type=secret,id=npm_token \
    if [ -f /run/secrets/npm_token ]; then \
      echo "//registry.npmjs.org/:_authToken=$(cat /run/secrets/npm_token)" >> .npmrc; \
    fi

This change ensures that the NPM_TOKEN is only handled securely using Docker BuildKit secrets.

Comment on lines 35 to 38
ENV NODE_ENV development
# Copy .npmrc again for dev-deps stage
COPY .npmrc .npmrc
RUN pnpm install --ignore-scripts --no-frozen-lockfile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve dependency installation in dev-deps stage

  1. Copying .npmrc again might be redundant if it's already in the base stage.
  2. The pnpm install command uses flags that might lead to inconsistent builds:
    • --ignore-scripts skips running scripts specified in package.json
    • --no-frozen-lockfile allows installing newer versions of dependencies

Consider the following changes:

  1. Remove the redundant COPY of .npmrc if it's not necessary.
  2. Modify the pnpm install command to ensure reproducible builds:
RUN pnpm install --frozen-lockfile

This change ensures that the exact versions specified in the lockfile are installed, promoting build consistency across environments.

@ericallam
Copy link
Member

You've updated the server docker image, which is not the images that are built for trigger.dev workers. I'd suggest closing this PR and starting over. Maybe watch this to get a better idea how to contribute this feature: https://youtu.be/Ta8OI1q8Jvw?si=mYTsoZ4HYHisF6Td

@ericallam ericallam closed this Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants