From afdad64a621062ce7fc456fb9cf76d1261c13cde Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Fri, 20 Dec 2024 16:49:44 +0900 Subject: [PATCH] :wrench: Set envName(production/preview) to cliVersion Integrates the current envName(production/preview) into cliVersion --- .github/workflows/vercel-deploy-erd-sample.yml | 1 - frontend/packages/cli/src/App.tsx | 1 + frontend/packages/cli/vite-plugins/set-env.ts | 16 ++++++++++++++++ .../erd-core/src/schemas/cliVersion/schemas.ts | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/vercel-deploy-erd-sample.yml b/.github/workflows/vercel-deploy-erd-sample.yml index 8c4be730..544a61eb 100644 --- a/.github/workflows/vercel-deploy-erd-sample.yml +++ b/.github/workflows/vercel-deploy-erd-sample.yml @@ -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 diff --git a/frontend/packages/cli/src/App.tsx b/frontend/packages/cli/src/App.tsx index ed696fd6..261cacd1 100644 --- a/frontend/packages/cli/src/App.tsx +++ b/frontend/packages/cli/src/App.tsx @@ -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, diff --git a/frontend/packages/cli/vite-plugins/set-env.ts b/frontend/packages/cli/vite-plugins/set-env.ts index a0ca80a9..b0cf0176 100644 --- a/frontend/packages/cli/vite-plugins/set-env.ts +++ b/frontend/packages/cli/vite-plugins/set-env.ts @@ -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. */ @@ -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() @@ -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' 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() }, } diff --git a/frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts b/frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts index b01dd50e..d1dd0027 100644 --- a/frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts +++ b/frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts @@ -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(), })