Skip to content
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

Speed up start up time #12

Merged
merged 1 commit into from
Dec 5, 2024
Merged

Speed up start up time #12

merged 1 commit into from
Dec 5, 2024

Conversation

Inkvi
Copy link
Collaborator

@Inkvi Inkvi commented Nov 14, 2024

Transcribe TS to JS during build time

Summary by CodeRabbit

  • New Features
    • Introduced new scripts for building TypeScript files and starting the server from compiled output.
  • Improvements
    • Enhanced Docker build process for a more efficient production setup by running pre-compiled JavaScript instead of TypeScript.
    • Added a comprehensive .gitignore file to improve project cleanliness by excluding unnecessary files from version control.
  • Updates
    • Upgraded the Trivy vulnerability scanner action in CI/CD workflows for improved security scanning.

@Inkvi Inkvi requested review from dshiell and jlafiandra6 November 14, 2024 21:11
Copy link

coderabbitai bot commented Nov 14, 2024

Walkthrough

The changes involve updates to the Dockerfile, package.json, and GitHub workflow files for a Node.js application. The Dockerfile has been modified to eliminate the global installation of ts-node, incorporating a build step that compiles TypeScript to JavaScript. The application now executes the compiled JavaScript file. The package.json has been enhanced with new scripts for building and starting the application. Additionally, the version of the Trivy vulnerability scanner has been upgraded in two GitHub workflow files, and a comprehensive .gitignore file has been introduced for Node.js projects.

Changes

File Change Summary
Dockerfile Removed global ts-node installation; added step to run npm run build; modified start command to run compiled JavaScript from dist.
package.json Added scripts: "build": "tsc" and "start": "node dist/server.js" for building and starting the server; added dependency "cross-spawn": "^7.0.5".
.github/workflows/docker-publish.yml Updated Trivy action version from 0.21.0 to 0.28.0.
.github/workflows/vuln-scan.yml Updated Trivy action version from 0.21.0 to 0.28.0.
.gitignore Introduced a comprehensive .gitignore for Node.js projects to exclude unnecessary files and directories.

Poem

In a world of code where rabbits play,
We build and compile, come what may.
With TypeScript now neatly in line,
Our Docker's ready, oh so fine!
Hopping along, our server's a breeze,
Running on JavaScript, with grace and ease! 🐰✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0f8a03d and 3f1a203.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (5)
  • .github/workflows/docker-publish.yml (1 hunks)
  • .github/workflows/vuln-scan.yml (1 hunks)
  • .gitignore (1 hunks)
  • Dockerfile (1 hunks)
  • package.json (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • .github/workflows/docker-publish.yml
  • .github/workflows/vuln-scan.yml
  • .gitignore
  • Dockerfile
  • package.json

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

@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: 1

🧹 Outside diff range and nitpick comments (4)
Dockerfile (3)

13-17: Optimize build process and reduce image size.

While compiling TypeScript during build is more efficient than runtime compilation, we can further optimize the process:

 COPY tsconfig.json ./
 COPY src src
 
 # Build TypeScript files
-RUN npm run build
+RUN npm run build && \
+    # Remove source files and devDependencies after build
+    rm -rf src && \
+    npm prune --production

This change will:

  1. Remove source files after compilation
  2. Remove devDependencies after build
  3. Reduce final image size

21-22: Add HEALTHCHECK instruction for container monitoring.

While switching to compiled JavaScript will improve startup time, consider adding a health check to ensure the service is running correctly:

 # Run compiled JavaScript instead of TypeScript
+HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
+    CMD wget --no-verbose --tries=1 --spider http://localhost:8000/health || exit 1
 CMD ["node", "dist/server.js"]

21-22: Consider additional startup optimizations.

While compiling TypeScript improves startup time, consider these additional optimizations:

  1. Add Node.js optimization flags:
-CMD ["node", "dist/server.js"]
+CMD ["node", "--optimize_for_size", "--max_old_space_size=460", "dist/server.js"]
  1. Consider using PM2 in production for process management and automatic restarts:
-CMD ["node", "dist/server.js"]
+RUN npm install pm2 -g
+CMD ["pm2-runtime", "dist/server.js"]

These changes can further improve startup time and runtime performance while ensuring better process management in production.

package.json (1)

8-9: Consider enhancing the build scripts for better developer experience.

While the current setup works for production, consider adding these helpful scripts:

 "scripts": {
   "build": "tsc",
   "start": "node dist/server.js",
+  "dev": "ts-node src/server.ts",
+  "watch": "tsc --watch",
+  "clean": "rm -rf dist",
+  "build:clean": "npm run clean && npm run build",
   "test": "echo \"Error: no test specified\" && exit 1"
 },

This would provide:

  • dev: Quick development with ts-node
  • watch: Auto-recompilation during development
  • clean: Clean build artifacts
  • build:clean: Fresh build
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4640587 and b0d8b2c.

📒 Files selected for processing (2)
  • Dockerfile (1 hunks)
  • package.json (1 hunks)
🔇 Additional comments (2)
package.json (2)

8-9: LGTM! The changes align with the goal of improving startup time.

The addition of build and start scripts implements the TypeScript to JavaScript compilation strategy, which should improve startup performance by eliminating runtime TypeScript compilation.


8-9: Verify the build setup with existing dependencies.

Let's ensure the new build configuration works correctly with the project structure and dependencies.

✅ Verification successful

Build configuration is properly set up and aligned with the project structure

The build setup in package.json is correctly configured:

  • tsconfig.json is present with proper configuration
  • Source root is correctly set to src directory
  • Output directory is set to dist as referenced in the start script
  • server.ts exists at the expected location src/server.ts
  • TypeScript source files are properly organized under the src directory
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify TypeScript configuration and source files

# Check if tsconfig.json exists and its content
if [ -f tsconfig.json ]; then
    echo "Found tsconfig.json:"
    cat tsconfig.json
else
    echo "Warning: tsconfig.json not found"
fi

# Look for TypeScript source files
echo -e "\nTypeScript source files:"
fd -e ts -E "node_modules"

# Verify server.ts location
echo -e "\nLocating server.ts:"
fd "server.ts" -E "node_modules"

Length of output: 2207

Comment on lines +21 to +22
# Run compiled JavaScript instead of TypeScript
CMD ["node", "dist/server.js"]
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

The dist directory and compiled files are missing

The verification reveals that while the TypeScript configuration and source files are properly set up (src/server.ts exists and tsconfig.json specifies dist as output directory), the dist directory with compiled JavaScript files is missing. The Dockerfile attempts to run dist/server.js which doesn't exist yet.

  • The TypeScript build needs to be executed before running the application
  • Add a build step in the Dockerfile before the CMD instruction:
    RUN npm run build
    CMD ["node", "dist/server.js"]
🔗 Analysis chain

Verify the existence of compiled files before execution.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the server.js file is mentioned in tsconfig.json output paths
# and if the health endpoint is implemented

# Check tsconfig.json output configuration
echo "Checking TypeScript output configuration..."
cat tsconfig.json | jq -r '.compilerOptions.outDir'

# Look for health endpoint implementation
echo "Checking health endpoint implementation..."
rg -l "app\.(get|use)\(['\"]\/health['\"]" src/

Length of output: 473


Script:

#!/bin/bash
# Let's verify the TypeScript setup and compiled files

# Check if tsconfig.json exists and its content
echo "=== TypeScript Configuration ==="
fd -t f "tsconfig.json" -x cat {}

# Check if dist directory exists and list its contents
echo -e "\n=== Dist Directory Content ==="
fd . "dist" || echo "dist directory not found"

# Check for server.ts or similar entry point files
echo -e "\n=== Server Entry Points ==="
fd -t f "server.ts" "index.ts" "main.ts" "app.ts" "src"

# Check package.json for build scripts
echo -e "\n=== Build Configuration ==="
fd "package.json" -x cat {} | jq '.scripts'

Length of output: 2094

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (2)
.github/workflows/vuln-scan.yml (1)

Line range hint 1-27: Consider adding Docker layer caching

The workflow could benefit from Docker layer caching to speed up the build process, especially since this PR focuses on improving startup time.

Consider adding Docker layer caching:

 name: build
 on:
   workflow_dispatch:
   pull_request:
 jobs:
   signer:
     name: Build and Scan Signer
     runs-on: ubuntu-22.04
     steps:
       - name: Checkout code
         uses: actions/checkout@v3

+      - name: Set up Docker Buildx
+        uses: docker/setup-buildx-action@v2
+
+      - name: Cache Docker layers
+        uses: actions/cache@v3
+        with:
+          path: /tmp/.buildx-cache
+          key: ${{ runner.os }}-buildx-${{ github.sha }}
+          restore-keys: |
+            ${{ runner.os }}-buildx-

       - name: Build an image
         run: |
-          docker build -t signer  .
+          docker buildx build \
+            --cache-from type=local,src=/tmp/.buildx-cache \
+            --cache-to type=local,dest=/tmp/.buildx-cache-new \
+            --load \
+            -t signer .
+
+      - name: Move cache
+        run: |
+          rm -rf /tmp/.buildx-cache
+          mv /tmp/.buildx-cache-new /tmp/.buildx-cache
.github/workflows/docker-publish.yml (1)

Line range hint 61-67: Consider publishing scan results as Security Advisory

The current configuration is robust with appropriate severity levels and vulnerability types. To enhance security visibility, consider adding a step to publish scan results as GitHub Security Advisory.

Add this step after the Trivy scan:

      - name: Upload Trivy scan results
        uses: github/codeql-action/upload-sarif@v2
        if: always()
        with:
          sarif_file: 'trivy-results.sarif'

And modify the Trivy step to output SARIF:

       uses: aquasecurity/[email protected]
       with:
         image-ref: ${{ env.IMAGE_NAME }}:latest
-        format: 'table'
+        format: 'sarif'
+        output: 'trivy-results.sarif'
         exit-code: '1'
         ignore-unfixed: true
         vuln-type: 'os,library'
         severity: 'CRITICAL,HIGH'
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b0d8b2c and 0f8a03d.

📒 Files selected for processing (5)
  • .github/workflows/docker-publish.yml (1 hunks)
  • .github/workflows/vuln-scan.yml (1 hunks)
  • .gitignore (1 hunks)
  • Dockerfile (1 hunks)
  • package.json (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .gitignore
🚧 Files skipped from review as they are similar to previous changes (2)
  • Dockerfile
  • package.json
🔇 Additional comments (2)
.github/workflows/vuln-scan.yml (1)

18-18: Verify the Trivy action version upgrade

The version upgrade from 0.21.0 to 0.28.0 is a good improvement, but let's ensure we're using the most optimal version.

Let's verify the version and compatibility:

✅ Verification successful

Version 0.28.0 is currently the latest stable release

The verification shows that version 0.28.0 (released on October 15, 2024) is indeed the latest stable release of the Trivy action. While we couldn't automatically verify the breaking changes and parameter compatibility due to API limitations, the version being used is current and appropriate.

Since this is a security scanning tool, using the latest stable version is a good practice to ensure you have the most recent vulnerability definitions and fixes.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check latest version, breaking changes, and parameter compatibility for aquasecurity/trivy-action

# Get the latest release version
echo "Latest release version:"
gh api repos/aquasecurity/trivy-action/releases/latest --jq .tag_name

# Check for breaking changes in the changelog between 0.21.0 and current version
echo -e "\nBreaking changes between 0.21.0 and current version:"
gh api repos/aquasecurity/trivy-action/contents/CHANGELOG.md --jq .content | base64 -d | awk '/## \[0\.2[1-8]\./ {p=1}; p; /## \[0\.21\.0\]/ {exit}' | grep -i "breaking"

# Verify if the current parameters are still valid in the action.yml
echo -e "\nVerifying parameter compatibility:"
gh api repos/aquasecurity/trivy-action/contents/action.yml --jq .content | base64 -d | grep -A 1 -E "image-ref:|format:|exit-code:|ignore-unfixed:|vuln-type:|severity:"

Length of output: 832


Script:

#!/bin/bash
# Let's try a different approach using direct GitHub API calls and jq for parsing JSON

# Get the latest release version and compare with 0.28.0
echo "Latest release version check:"
gh api repos/aquasecurity/trivy-action/releases/latest --jq '.tag_name, .published_at'

# Get the action.yml content directly and parse with jq
echo -e "\nVerifying input parameters in action.yml:"
gh api repos/aquasecurity/trivy-action/contents/action.yml --jq '.content' | \
  tr -d '\n' | \
  jq -R 'split(".") | .[0] | @base64d' | \
  grep -A 1 -E "image-ref:|format:|exit-code:|ignore-unfixed:|vuln-type:|severity:"

# Get the changelog content and look for breaking changes
echo -e "\nChecking for breaking changes:"
gh api repos/aquasecurity/trivy-action/contents/CHANGELOG.md --jq '.content' | \
  tr -d '\n' | \
  jq -R 'split(".") | .[0] | @base64d' | \
  grep -B 1 -A 2 -i "breaking"

Length of output: 930

.github/workflows/docker-publish.yml (1)

61-61: Verify Trivy version upgrade compatibility

The upgrade from v0.21.0 to v0.28.0 is a significant version jump that improves security coverage.

Let's verify if there are any breaking changes in this upgrade:

✅ Verification successful

Let me generate another script to check for breaking changes in the Trivy GitHub Action specifically, since the previous script didn't provide enough information.


Let me try one more verification with a different approach to check the action's repository:


Let me try one final verification focusing on the main repository's action file:


Trivy Action upgrade is safe to proceed

The version upgrade from v0.21.0 to v0.28.0 is safe as:

  • Both workflow files use identical configuration parameters that are basic and stable
  • The parameters used (image-ref, format, exit-code, ignore-unfixed, vuln-type, severity) are core features that remain unchanged
  • The configuration focuses on essential vulnerability scanning with consistent settings across workflows
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check Trivy release notes for breaking changes between versions

# Fetch release notes for relevant versions
gh release view v0.28.0 --repo aquasecurity/trivy
for ver in $(gh release list --repo aquasecurity/trivy | grep -o "v0.2[1-8]\.0" | sort -V); do
  echo "=== $ver ==="
  gh release view $ver --repo aquasecurity/trivy | grep -i "breaking\|migration"
done

Length of output: 3674


Script:

#!/bin/bash
# Check the Trivy GitHub Action changelog for breaking changes
gh api repos/aquasecurity/trivy-action/commits --paginate | jq -r '.[] | select(.commit.message | test("(?i)breaking|migration")) | "=== Commit " + .sha[0:7] + " ===\n" + .commit.message + "\n"' || true

# Also check the current action.yml for any parameter changes
curl -s https://raw.githubusercontent.com/aquasecurity/trivy-action/0.28.0/action.yml
echo "=== Previous version parameters ==="
curl -s https://raw.githubusercontent.com/aquasecurity/trivy-action/0.21.0/action.yml

Length of output: 482


Script:

#!/bin/bash
# Clone the repo to check changes between versions
git clone https://github.com/aquasecurity/trivy-action.git /tmp/trivy-action
cd /tmp/trivy-action

# Compare the action.yml between versions
git checkout 0.21.0 2>/dev/null
echo "=== v0.21.0 inputs ==="
git show 0.21.0:action.yml | grep -A 50 "inputs:"

git checkout 0.28.0 2>/dev/null
echo -e "\n=== v0.28.0 inputs ==="
git show 0.28.0:action.yml | grep -A 50 "inputs:"

# Check commit messages between versions for breaking changes
echo -e "\n=== Commits between v0.21.0 and v0.28.0 ==="
git log --oneline 0.21.0..0.28.0 | grep -i "breaking\|migration"

Length of output: 733


Script:

#!/bin/bash
# Check the current workflow file configuration
echo "=== Current Trivy Action Configuration ==="
cat .github/workflows/docker-publish.yml | grep -A 10 "uses: aquasecurity/trivy-action"

# Check if there are other workflow files using Trivy
echo -e "\n=== Other Trivy Usage in Workflows ==="
find .github/workflows -type f -name "*.yml" -exec grep -l "aquasecurity/trivy-action" {} \;

# Show their configurations too
for file in $(find .github/workflows -type f -name "*.yml"); do
    if grep -q "aquasecurity/trivy-action" "$file"; then
        echo -e "\n=== $file configuration ==="
        cat "$file" | grep -A 10 "uses: aquasecurity/trivy-action"
    fi
done

Length of output: 1955

Remove global ts-node installation and add build step for TypeScript. Copy tsconfig.json and update CMD to run compiled JavaScript. Adjust package.json scripts to include build and start commands.
@Inkvi Inkvi merged commit a5ed3d3 into main Dec 5, 2024
1 check failed
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.

1 participant