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

feat: use run time variables in dist in wf-dashboard #2476

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

pratapalakshmi
Copy link
Collaborator

@pratapalakshmi pratapalakshmi commented Jun 25, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a global env variable for configuration.
    • Added a new entrypoint.sh script for environment variable setup.
  • Enhancements

    • Streamlined application initialization with improved container configuration for better startup behavior.
    • Enhanced environment variable management to facilitate local development.
  • Configuration

    • Added config.js to manage global configuration settings, including VITE_API_URL and defaults for environment variables.

Copy link

changeset-bot bot commented Jun 25, 2024

⚠️ No Changeset found

Latest commit: 6aae2e8

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 Jun 25, 2024

Walkthrough

This update to the workflows-dashboard app includes modifications to the Dockerfile, adding a new working directory and an initialization script. The entrypoint.sh script is introduced to set environment variables and create a configuration file for the application. Additionally, a global configuration object is established in config.js, ensuring the application is adaptable to different environments. Changes to the package.json build script ensure a clean build process, while adjustments in TypeScript files facilitate the use of the new global configuration.

Changes

Files Change Summary
apps/workflows-dashboard/Dockerfile Added working directory, updated COPY command, set entry point to entrypoint.sh, and changed permissions.
apps/workflows-dashboard/entrypoint.sh Introduced a script to initialize environment variables and generate a configuration file.
apps/workflows-dashboard/global.d.ts Added global variable env for dynamic environment configurations.
apps/workflows-dashboard/index.html Added a script tag to include the new config.js for application configuration.
apps/workflows-dashboard/package.json Modified the build script to remove the dist directory before building.
apps/workflows-dashboard/public/config.js Created a global configuration object globalThis.env with essential environment variables.
apps/workflows-dashboard/src/lib/request/request.ts Updated Axios request base URL to use globalThis.env.VITE_API_URL.
apps/workflows-dashboard/src/main.tsx Added import for config.js and initialized global variable env with API URL.

Poem

In a world where code takes flight,
New scripts emerge, a wondrous sight.
Variables set, with care and grace,
Our app now dons a brighter face.
With configs to guide us on our way,
Let's celebrate this joyful day!
🐇✨🎉


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.

@pratapalakshmi pratapalakshmi changed the title feat: use run time variables in dist feat[wip]: use run time variables in dist Jun 25, 2024
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: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f5ecf8f and 2384ed6.

Files ignored due to path filters (1)
  • apps/workflows-dashboard/package-lock.json is excluded by !**/package-lock.json
Files selected for processing (6)
  • apps/workflows-dashboard/global.d.ts (1 hunks)
  • apps/workflows-dashboard/index.html (1 hunks)
  • apps/workflows-dashboard/package.json (1 hunks)
  • apps/workflows-dashboard/public/config.js (1 hunks)
  • apps/workflows-dashboard/src/lib/request/request.ts (1 hunks)
  • apps/workflows-dashboard/src/main.tsx (1 hunks)
Files skipped from review due to trivial changes (3)
  • apps/workflows-dashboard/index.html
  • apps/workflows-dashboard/package.json
  • apps/workflows-dashboard/public/config.js
Additional comments not posted (1)
apps/workflows-dashboard/src/main.tsx (1)

6-6: Review the necessity of URL import syntax.

The import statement uses ?url which might be intended to ensure the file is treated as a URL dependency. Confirm if this is necessary and correctly implemented as intended.

Comment on lines +1 to +3
declare global {
export var env: {[key: string]: any};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using const for global declarations.

Using var for the global declaration of env can lead to hoisting issues and is generally less safe than const. Since env is unlikely to be reassigned directly (it's properties might change), using const would be more appropriate.

declare global {
  export const env: {[key: string]: any};
}

Comment on lines +4 to +5
//@ts-ignore
baseURL: globalThis.env.VITE_API_URL,
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid using @ts-ignore where possible.

The use of @ts-ignore can hide potential type issues. It's better to handle these explicitly, either by defining the types or handling potential undefined cases.

// Assuming `env` and `VITE_API_URL` are always defined
baseURL: globalThis.env.VITE_API_URL as string,

Comment on lines +14 to +20
//@ts-ignore
globalThis.env = globalThis.env || {
API_URL: import.meta.env.VITE_API_URL
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Refine TypeScript usage and handle potential null cases.

Using @ts-ignore to bypass TypeScript checks is not ideal. Also, consider handling the case where document.getElementById('root') might return null.

const rootElement = document.getElementById('root') as HTMLElement;
if (!rootElement) {
  console.error('Root element not found');
  throw new Error('Root element not found');
}
ReactDOM.createRoot(rootElement).render(
  <React.StrictMode>
    <RouterProvider router={router}></RouterProvider>
  </React.StrictMode>,
);

// Remove @ts-ignore and ensure type safety
globalThis.env = globalThis.env || {
  API_URL: import.meta.env.VITE_API_URL as string
}

@pratapalakshmi pratapalakshmi changed the title feat[wip]: use run time variables in dist feat: use run time variables in dist in wf-dashboard Aug 11, 2024
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: 0

Outside diff range, codebase verification and nitpick comments (1)
apps/workflows-dashboard/public/config.js (1)

1-3: Consider the implications of using global variables.

Defining environment variables in the global scope can simplify access but may lead to potential issues with maintainability and testing. Consider using a module or a configuration management library to encapsulate these variables, which can help avoid potential conflicts and improve testability.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2384ed6 and d737351.

Files ignored due to path filters (1)
  • apps/workflows-dashboard/package-lock.json is excluded by !**/package-lock.json
Files selected for processing (6)
  • apps/workflows-dashboard/global.d.ts (1 hunks)
  • apps/workflows-dashboard/index.html (1 hunks)
  • apps/workflows-dashboard/package.json (1 hunks)
  • apps/workflows-dashboard/public/config.js (1 hunks)
  • apps/workflows-dashboard/src/lib/request/request.ts (1 hunks)
  • apps/workflows-dashboard/src/main.tsx (2 hunks)
Files skipped from review due to trivial changes (1)
  • apps/workflows-dashboard/global.d.ts
Additional comments not posted (4)
apps/workflows-dashboard/src/lib/request/request.ts (1)

4-5: Avoid using @ts-ignore where possible.

The use of @ts-ignore can hide potential type issues. It's better to handle these explicitly, either by defining the types or handling potential undefined cases. Consider ensuring that globalThis.env and VITE_API_URL are properly typed and defined.

apps/workflows-dashboard/index.html (1)

9-9: Ensure correct script loading sequence.

Make sure that /config.js is loaded before any other scripts that depend on the environment variables defined within it. This ensures that all configurations are available when needed.

apps/workflows-dashboard/src/main.tsx (1)

7-7: Verify the existence and accessibility of config.js?url.

Ensure that the config.js?url file is correctly configured and accessible at runtime. This import is crucial for managing external configuration settings.

Verification successful

File config.js is correctly located and accessible.

The config.js file exists at apps/workflows-dashboard/public/config.js, confirming that the import statement should function as expected. Ensure that the file is correctly configured and accessible at runtime.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of `config.js?url`.

# Test: Check if `config.js` exists in the public directory. Expect: File should exist.
fd 'config.js' public

Length of output: 115


Script:

#!/bin/bash
# Description: Search for `config.js` in the entire repository.

# Test: Locate `config.js` file. Expect: File should exist somewhere in the repository.
fd 'config.js'

Length of output: 1308

apps/workflows-dashboard/package.json (1)

9-9: Good practice: Clean build environment.

The addition of rm -rf dist in the build script ensures a clean build environment by removing previous build artifacts. This is a good practice to prevent issues with stale files.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d737351 and dae7cc9.

Files selected for processing (3)
  • apps/workflows-dashboard/Dockerfile (1 hunks)
  • apps/workflows-dashboard/entrypoint.sh (1 hunks)
  • apps/workflows-dashboard/public/config.js (1 hunks)
Files skipped from review due to trivial changes (1)
  • apps/workflows-dashboard/public/config.js
Additional context used
Shellcheck
apps/workflows-dashboard/entrypoint.sh

[warning] 3-3: In POSIX sh, [[ ]] is undefined.

(SC3010)


[warning] 8-8: In POSIX sh, [[ ]] is undefined.

(SC3010)


[warning] 13-13: In POSIX sh, [[ ]] is undefined.

(SC3010)


[warning] 19-19: In POSIX sh, [[ ]] is undefined.

(SC3010)

Additional comments not posted (6)
apps/workflows-dashboard/Dockerfile (4)

22-22: Set working directory correctly.

The WORKDIR /app command is correctly used to set the working directory for subsequent commands. This is a standard practice in Dockerfiles.


26-26: Ensure the entrypoint script is copied correctly.

The COPY command is used to copy the entrypoint.sh script from the development stage to the production image. This is crucial for the script to be available in the production environment.


30-30: Correct file permissions for the entrypoint script.

The RUN chmod a+x /app/entrypoint.sh; command correctly sets the executable permissions for the entrypoint.sh script, ensuring it can be executed at runtime.


34-34: Set the entrypoint for the Docker container.

The ENTRYPOINT command is correctly updated to use the entrypoint.sh script. This change is crucial for initializing the container with the necessary environment setup.

apps/workflows-dashboard/entrypoint.sh (2)

25-32: Review the configuration file generation logic.

The script dynamically generates a configuration file config.js with runtime environment variables. This approach allows for flexible configuration based on the environment.


34-35: Ensure proper command handling in the script.

The exec "$@" command is used to handle the CMD command passed to the Docker container. This ensures that any command specified in the Docker CMD is executed after the script runs.

apps/workflows-dashboard/entrypoint.sh Show resolved Hide resolved
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: 0

Outside diff range and nitpick comments (2)
apps/workflows-dashboard/public/config.js (1)

1-6: LGTM! The global configuration object is a good addition.

The introduction of the globalThis.env object provides a centralized way to manage environment-specific configuration for the application. This approach facilitates the development process by making the necessary environment variables readily accessible.

A few suggestions for further improvement:

  1. Consider adding comments to describe the purpose of each property in the env object. This will make the code more self-explanatory and easier to maintain.

  2. If there are any sensitive configuration values (e.g., API keys, secrets), ensure that they are not hardcoded in this file. Instead, consider using environment variables or a separate configuration management system to securely store and retrieve such values.

  3. If the env object is expected to grow with more properties, consider organizing the properties into logical groups or sections for better readability.

Overall, this is a good step towards making the application more adaptable to different environments.

apps/workflows-dashboard/src/main.tsx (1)

7-7: Clarify the purpose of using a URL query parameter in the import statement.

The import statement is using a URL query parameter ?url, which is not a common practice for importing JavaScript modules. Can you please clarify the purpose of using this query parameter?

Also, consider using a TypeScript file (.ts) instead of a JavaScript file (.js) for type safety and consistency with the rest of the codebase.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between dae7cc9 and 6aae2e8.

Files ignored due to path filters (1)
  • apps/workflows-dashboard/package-lock.json is excluded by !**/package-lock.json
Files selected for processing (8)
  • apps/workflows-dashboard/Dockerfile (1 hunks)
  • apps/workflows-dashboard/entrypoint.sh (1 hunks)
  • apps/workflows-dashboard/global.d.ts (1 hunks)
  • apps/workflows-dashboard/index.html (1 hunks)
  • apps/workflows-dashboard/package.json (1 hunks)
  • apps/workflows-dashboard/public/config.js (1 hunks)
  • apps/workflows-dashboard/src/lib/request/request.ts (1 hunks)
  • apps/workflows-dashboard/src/main.tsx (2 hunks)
Additional context used
Shellcheck
apps/workflows-dashboard/entrypoint.sh

[warning] 3-3: In POSIX sh, [[ ]] is undefined.

(SC3010)


[warning] 8-8: In POSIX sh, [[ ]] is undefined.

(SC3010)


[warning] 13-13: In POSIX sh, [[ ]] is undefined.

(SC3010)


[warning] 19-19: In POSIX sh, [[ ]] is undefined.

(SC3010)

Additional comments not posted (10)
apps/workflows-dashboard/global.d.ts (1)

1-3: ** Consider using const for global declarations.**

Using var for the global declaration of env can lead to hoisting issues and is generally less safe than const. Since env is unlikely to be reassigned directly (its properties might change), using const would be more appropriate.

declare global {
  export const env: {[key: string]: any};
}
apps/workflows-dashboard/src/lib/request/request.ts (1)

4-5: Handle types explicitly instead of using @ts-ignore.

The use of @ts-ignore can hide potential type issues. It's better to handle these explicitly, either by defining the types or handling potential undefined cases.

Consider this alternative:

// Assuming `env` and `VITE_API_URL` are always defined
baseURL: globalThis.env.VITE_API_URL as string,

If globalThis.env.VITE_API_URL can be undefined, handle it like this:

baseURL: globalThis.env.VITE_API_URL ?? 'default_url',
apps/workflows-dashboard/index.html (1)

9-9: LGTM!

The inclusion of the config.js file in the <head> section is a good practice for defining global configuration settings. This aligns with the PR objective of using runtime variables in the workflow dashboard.

Note: The type="text/javascript" attribute is optional as it is the default script type.

apps/workflows-dashboard/src/main.tsx (1)

17-20: ** Refine TypeScript usage and handle potential null cases.**

The suggestions from the previous review comment are still valid and applicable:

  • Using @ts-ignore to bypass TypeScript checks is not ideal.
  • Consider handling the case where document.getElementById('root') might return null.

Here's a code snippet incorporating the suggestions:

const rootElement = document.getElementById('root') as HTMLElement;
if (!rootElement) {
  console.error('Root element not found');
  throw new Error('Root element not found');
}
ReactDOM.createRoot(rootElement).render(
  <React.StrictMode>
    <RouterProvider router={router}></RouterProvider>
  </React.StrictMode>,
);

// Remove @ts-ignore and ensure type safety
globalThis.env = globalThis.env || {
  API_URL: import.meta.env.VITE_API_URL as string
}
apps/workflows-dashboard/Dockerfile (1)

22-35: LGTM!

The changes to the Dockerfile enhance the production setup by:

  • Setting a working directory for better filesystem organization.
  • Copying the entrypoint.sh script from the development stage to the production stage.
  • Making the script executable.
  • Specifying the script as the container's entrypoint to perform any necessary setup or configuration before the main application starts.

The existing CMD instruction for running Nginx is retained, ensuring that Nginx runs in the foreground after the entrypoint.sh script is executed.

apps/workflows-dashboard/entrypoint.sh (4)

3-6: ** Replace [[ ]] with [ ] for POSIX compatibility.**

The past review comment is still valid. The script uses [[ ]] for condition checks, which is not defined in POSIX sh. This can lead to script failures in strict POSIX environments.

Replace [[ ]] with [ ] to ensure compatibility:

- if [[ -z "$VITE_DOMAIN" ]]
+ if [ -z "$VITE_DOMAIN" ]
Tools
Shellcheck

[warning] 3-3: In POSIX sh, [[ ]] is undefined.

(SC3010)


8-11: ** Replace [[ ]] with [ ] for POSIX compatibility.**

The past review comment is still valid. The script uses [[ ]] for condition checks, which is not defined in POSIX sh. This can lead to script failures in strict POSIX environments.

Replace [[ ]] with [ ] to ensure compatibility:

- if [[ -z "$MODE" ]]
+ if [ -z "$MODE" ]
Tools
Shellcheck

[warning] 8-8: In POSIX sh, [[ ]] is undefined.

(SC3010)


13-16: ** Replace [[ ]] with [ ] for POSIX compatibility.**

The past review comment is still valid. The script uses [[ ]] for condition checks, which is not defined in POSIX sh. This can lead to script failures in strict POSIX environments.

Replace [[ ]] with [ ] to ensure compatibility:

- if [[ -z "$VITE_IMAGE_LOGO_URL" ]]
+ if [ -z "$VITE_IMAGE_LOGO_URL" ]
Tools
Shellcheck

[warning] 13-13: In POSIX sh, [[ ]] is undefined.

(SC3010)


19-22: ** Replace [[ ]] with [ ] for POSIX compatibility.**

The past review comment is still valid. The script uses [[ ]] for condition checks, which is not defined in POSIX sh. This can lead to script failures in strict POSIX environments.

Replace [[ ]] with [ ] to ensure compatibility:

- if [[ -z "$VITE_ENVIRONMENT_NAME" ]]
+ if [ -z "$VITE_ENVIRONMENT_NAME" ]
Tools
Shellcheck

[warning] 19-19: In POSIX sh, [[ ]] is undefined.

(SC3010)

apps/workflows-dashboard/package.json (1)

9-9: LGTM!

The change to the build script is a good practice. Removing the dist directory before each build ensures a clean build process and prevents potential issues related to stale files from previous builds.

Comment on lines +25 to +31
cat << EOF > /usr/share/nginx/html/config.js
globalThis.env = {
VITE_API_URL: "http://$VITE_DOMAIN/api/v1/",
VITE_ENVIRONMENT_NAME: "$VITE_ENVIRONMENT_NAME",
MODE: "$MODE",
VITE_IMAGE_LOGO_URL: "$VITE_IMAGE_LOGO_URL",
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you need the file config.js if you are adding the same file content inline in here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i just say that you can delete config.js file

Comment on lines +25 to +31
cat << EOF > /usr/share/nginx/html/config.js
globalThis.env = {
VITE_API_URL: "http://$VITE_DOMAIN/api/v1/",
VITE_ENVIRONMENT_NAME: "$VITE_ENVIRONMENT_NAME",
MODE: "$MODE",
VITE_IMAGE_LOGO_URL: "$VITE_IMAGE_LOGO_URL",
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

i just say that you can delete config.js file

@@ -4,6 +4,7 @@ import * as ReactDOM from 'react-dom/client';
import { RouterProvider } from 'react-router-dom';
import './index.css';
import { initializeMonitoring } from '@/initialize-monitoring/initialize-monitoring';
import '../public/config.js?url';
Copy link
Collaborator

Choose a reason for hiding this comment

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

if you add it in here it would take in place just if you build the app again

@@ -6,6 +6,7 @@
<link rel="icon" type="image/png" sizes="16x16" href="/public/favicon-16x16.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Workflow Dashboard</title>
<script type="text/javascript" src="/config.js"></script>
Copy link
Collaborator

Choose a reason for hiding this comment

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

the path should be relative im not sure its actually working

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