'Expo app cannot see env variable from Github action
We store our configurations on the vault and during the Github action workflow we pull this and intend to use this for the app during build time.
This is our workflow
- name: Get app configs from vault
env:
ENVIRONMENT: development
run: |
chmod +x scripts/ci/get-app-configs.sh
scripts/ci/get-app-configs.sh
echo "action_state=yellow" >> $GITHUB_ENV
- name: Set .env to process.env of github actions
uses: cardinalby/export-env-action@v1
with:
envFile: '.env'
expand: 'true'
- name: Generate APK
env:
EXPO_TOKEN: ${{ secrets.EXPO_TOKEN }}
run: |
echo "${{ env.action_state }}"
echo ${{ env.FIREBASE }}
eas build --platform android --local --profile development --non-interactive
The Get app configs from vault step creates an .env file with the following contents
FIREBASE={firebase_config_string}
And then inside firebase.ts we do this.
const firebaseConfig = JSON.parse(process.env.FIREBASE as string) as FirebaseOptions;
The app works fine when testing on an emulator but when testing the actual APK on a physical device, it always crashes. And in the logcat I keep seeing this error E AndroidRuntime: com.facebook.react.common.JavascriptException: SyntaxError: JSON Parse error: Unexpected identifier "undefined", stack:.
I've already narrowed it down to the firebase.ts process.env since the app doesn't crash when the config is hard coded.
I can see the env.FIREBASE as an env variable inside the Generate APK step so it's being passed to other steps fine. And the echo also prints out the value fine.
Is it not possible to access the env variables of Github action within the code during build time? Or are there other ways to achieve this?
Solution 1:[1]
Kept the github workflow as it is.
Fixed by using expo-constants.
Renaming the app.json to app.config.js to be able to add the following lines:
import 'dotenv/config';
export default {
expo: {
extra: {
ENV_VAR: process.env.ENV_VAR,
},
},
};
And access the said variable from the app by
import Constants from 'expo-constants';
const ENV_VAR = Constants.manifest?.extra?.ENV_VAR
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
