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

Set envName(production or preview) to cliVersion #343

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/vercel-deploy-erd-sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ jobs:
- name: Update index.html content
run: pnpm --filter ${{ matrix.apps.name }} update_dist_content
working-directory: frontend
# if: ${{ main }}
- name: Build Project Artifacts
run: vercel build ${{ needs.setup-deployment.outputs.environment == 'production' && '--prod' || '' }}
- name: Deploy Project Artifacts to Vercel
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/cli/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ loadSchemaContent()
const cliVersionData = {
version: import.meta.env.VITE_CLI_VERSION_VERSION,
gitHash: import.meta.env.VITE_CLI_VERSION_GIT_HASH,
envName: import.meta.env.VITE_CLI_VERSION_ENV_NAME,
isReleasedGitHash:
import.meta.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH === '1',
date: import.meta.env.VITE_CLI_VERSION_DATE,
Expand Down
16 changes: 16 additions & 0 deletions frontend/packages/cli/vite-plugins/set-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { type Plugin, loadEnv } from 'vite'
* - VITE_CLI_VERSION_IS_RELEASED_GIT_HASH: A flag indicating whether the current GIT hash corresponds to a released tag.
* - VITE_CLI_VERSION_GIT_HASH: The current GIT commit hash.
* - VITE_CLI_VERSION_DATE: The commit date of the latest commit.
* - VITE_CLI_VERSION_ENV_NAME: Environment name (preview or production).
*
* These variables are essential for maintaining version consistency and tracking within the deployment environment.
*/
Expand All @@ -20,6 +21,15 @@ export function setEnvPlugin(): Plugin {
}
}

const fetchGitBranch = () => {
try {
return execSync('git rev-parse --abbrev-ref HEAD').toString().trim()
} catch (error) {
console.error('Failed to get git branch:', error)
return ''
}
}

const date = () => {
try {
const gitDate = execSync('git log -1 --format=%ci').toString().trim()
Expand Down Expand Up @@ -56,12 +66,18 @@ export function setEnvPlugin(): Plugin {

const packageJsonVersion = env.npm_package_version
const gitHash = fetchGitHash()
const gitBranch = fetchGitBranch()

// The main branch is considered production, all other branches are treated as previews.
// This alignment is done to match the deployment settings to Vercel specified in .github/workflows.
const envName = gitBranch === 'main' ? 'production' : 'preview'
Comment on lines +71 to +73
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if I should write about it here, since it's information only our team uses.
But once it's done, it looks good!

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, there seems to be some merit to that opinion! :) I believe managing the envName value within the CLI package is necessary for our team's dogfooding. Conversely, it might be a good idea to move this // comment to the .github/workflows side.


process.env.VITE_CLI_VERSION_VERSION = packageJsonVersion
process.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH = JSON.stringify(
isReleasedGitHash(gitHash, packageJsonVersion),
)
process.env.VITE_CLI_VERSION_GIT_HASH = gitHash
process.env.VITE_CLI_VERSION_ENV_NAME = envName
process.env.VITE_CLI_VERSION_DATE = date()
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as v from 'valibot'
export const cliVersionSchema = v.object({
version: v.string(),
gitHash: v.string(),
envName: v.string(),
isReleasedGitHash: v.boolean(),
date: v.string(),
})
Loading