根据文档,运行时会评估本地 .env 文件
eas update
。
当您运行 eas update 时,将在捆绑 JavaScript 时评估存在的任何 .env 文件。应用程序代码中的任何 EXPO_PUBLIC_ 变量都将替换为发布更新的计算机上存在的 .env 文件中的相应值,无论该计算机是您的本地计算机还是 CI/CD 服务器。 (https://docs.expo.dev/eas-update/environment-variables/)
有没有办法在运行时忽略本地.env文件
eas update
?
我不知道
eas update
的任何参数会忽略 .env 文件。 EAS 还指出:运行 eas update 时,构建配置文件中的 env 字段上设置的环境变量不可用
./scripts/updateDevelopmentBuild.js
:
const fs = require('fs')
const {
execSync,
spawn,
} = require('child_process')
const readline = require('readline-sync')
const easJson = require('../eas.json')
const ENV_FILE_PATH = '.env'
const ENV_FILE_STASH_PATH = '.env.stash'
async function main() {
console.log('🛠️ Creating update for development build...')
let currentBranch
try {
currentBranch = execSync('git branch --show-current').toString().trim()
}
catch (e) {
// do nothing
}
if (!currentBranch) {
console.error('🚫 Could not get current branch.')
process.exit(1)
}
console.info('')
const message = readline.question('Enter a short message for testers: ')
console.info('')
if (!message) {
console.error('🚫 Message is required.')
process.exit(1)
}
moveExistingEnvFile('stash')
writeTemporaryEnvFile()
pushEasUpdate(currentBranch, message)
.catch(error => {
console.error('🚫 EAS Update failed:', error)
})
.finally(() => {
deleteTemporaryEnvFile()
moveExistingEnvFile('restore')
})
}
function moveExistingEnvFile(action) {
if (action !== 'stash' && action !== 'restore') {
console.error('🐞 moveExistingEnvFile(action) only accepts "stash" or "restore" for action param.')
process.exit(1)
}
const cmd = action === 'stash' ? `mv ${ENV_FILE_PATH} ${ENV_FILE_STASH_PATH}` : `mv ${ENV_FILE_STASH_PATH} ${ENV_FILE_PATH}`
try {
execSync(cmd)
console.log(`✔️ Existing ${ENV_FILE_PATH} file ${action === 'stash' ? 'stashed' : 'restored'}.`)
}
catch (e) {
console.error(`❗️ Unable to ${action} ${ENV_FILE_PATH} file.`)
// continue with script...
}
}
function writeTemporaryEnvFile() {
const envVars = easJson?.build?.--YOUR-EAS-PROFILE-HERE--?.env
if (!envVars) {
console.error('🐞 Unable to find build profile in eas.json.')
process.exit(1)
}
const envString = Object.keys(envVars).map(key => `${key}=${envVars[key]}`).join('\n')
fs.writeFileSync(ENV_FILE_PATH, envString)
console.log(`✔️ Temporary ${ENV_FILE_PATH} file written.`)
}
function deleteTemporaryEnvFile() {
fs.unlinkSync(ENV_FILE_PATH)
console.log(`✔️ Temporary ${ENV_FILE_PATH} file deleted.`)
}
function pushEasUpdate(currentBranch, message) {
return new Promise((resolve, reject) => {
const baseEasCmd = `eas update --clear-cache --branch ${currentBranch}`
console.log(`\n⏯️ Running command: ${baseEasCmd} --message "${message}"\n`)
const easCmdParts = [
...baseEasCmd.split(' '),
'--message',
message,
]
const runningProcess = spawn(easCmdParts[0], [ ...easCmdParts.slice(1) ])
runningProcess.stdout.pipe(process.stdout)
runningProcess.stderr.on('data', data => {
reject(data.toString())
})
runningProcess.on('exit', code => {
console.log(`child process exited with code ${code.toString()}`)
resolve(runningProcess.stdout)
})
})
}
main()
然后你可以打电话
node ./scripts/updateDevelopmentBuild.js