-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evaluate dynamicProjectConfig with EnvVars
- Loading branch information
1 parent
97f267b
commit e17b83a
Showing
16 changed files
with
187 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/eas-cli/src/build/evaluateConfigWithEnvVarsAsync.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Env } from '@expo/eas-build-job'; | ||
import { BuildProfile } from '@expo/eas-json'; | ||
|
||
import { ExpoGraphqlClient } from '../commandUtils/context/contextUtils/createGraphqlClient'; | ||
import { EnvironmentVariableEnvironment } from '../graphql/generated'; | ||
import { EnvironmentVariablesQuery } from '../graphql/queries/EnvironmentVariablesQuery'; | ||
import Log from '../log'; | ||
|
||
function isEnvironment(env: string): env is EnvironmentVariableEnvironment { | ||
if ( | ||
Object.values(EnvironmentVariableEnvironment).includes( | ||
env.toUpperCase() as EnvironmentVariableEnvironment | ||
) | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export async function evaluateConfigWithEnvVarsAsync<Config extends { projectId: string }, Opts>({ | ||
flags, | ||
buildProfile, | ||
graphqlClient, | ||
getProjectConfig, | ||
opts, | ||
}: { | ||
flags: { environment?: string }; | ||
buildProfile: BuildProfile; | ||
graphqlClient: ExpoGraphqlClient | null; | ||
opts: Opts; | ||
getProjectConfig(opts: Opts): Promise<Config>; | ||
}): Promise<Config & { env: Env }> { | ||
if (!graphqlClient) { | ||
Log.warn('An Expo user account is required to fetch environment variables.'); | ||
const config = await getProjectConfig(opts); | ||
return { env: buildProfile.env ?? {}, ...config }; | ||
} | ||
const { projectId } = await getProjectConfig({ env: buildProfile.env, ...opts }); | ||
const env = await resolveEnvVarsAsync({ flags, buildProfile, graphqlClient, projectId }); | ||
const config = await getProjectConfig({ ...opts, env }); | ||
|
||
return { env, ...config }; | ||
} | ||
|
||
async function resolveEnvVarsAsync({ | ||
flags, | ||
buildProfile, | ||
graphqlClient, | ||
projectId, | ||
}: { | ||
flags: { environment?: string }; | ||
buildProfile: BuildProfile; | ||
graphqlClient: ExpoGraphqlClient; | ||
projectId: string; | ||
}): Promise<Env> { | ||
const environment = | ||
flags.environment ?? buildProfile.environment ?? process.env.EAS_CURRENT_ENVIRONMENT; | ||
|
||
if (!environment || !isEnvironment(environment)) { | ||
return { ...buildProfile.env }; | ||
} | ||
|
||
try { | ||
const environmentVariables = await EnvironmentVariablesQuery.byAppIdWithSensitiveAsync( | ||
graphqlClient, | ||
{ | ||
appId: projectId, | ||
environment, | ||
} | ||
); | ||
const envVars = Object.fromEntries( | ||
environmentVariables | ||
.filter(({ name, value }) => name && value) | ||
.map(({ name, value }) => [name, value]) | ||
) as Record<string, string>; | ||
|
||
return { ...envVars, ...buildProfile.env }; | ||
} catch (e) { | ||
Log.error('Failed to pull env variables for environment ${environment} from EAS servers'); | ||
Log.error(e); | ||
Log.error( | ||
'This can possibly be a bug in EAS/EAS CLI. Report it here: https://github.com/expo/eas-cli/issues' | ||
); | ||
return { ...buildProfile.env }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.