Skip to content

Commit

Permalink
🔧 Set envName(production/preview) to cliVersion
Browse files Browse the repository at this point in the history
Integrates the current envName(production/preview) into cliVersion
  • Loading branch information
hoshinotsuyoshi committed Dec 20, 2024
1 parent ff3b794 commit afdad64
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
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'

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(),
})

0 comments on commit afdad64

Please sign in to comment.