1--- 2title: Environment variables and secrets in EAS Build 3sidebar_title: Environment variables and secrets 4description: Learn how to use environment variables and secrets in an EAS Build. 5--- 6 7import { Collapsible } from '~/ui/components/Collapsible'; 8import { Step } from '~/ui/components/Step'; 9import { Terminal } from '~/ui/components/Snippet'; 10 11The [Environment variables in Expo guide](/guides/environment-variables) describes how to use **.env** files to set environment variables that can be inlined in your JavaScript code. The Expo CLI will substitute properly prefixed variables in your code (for example,`process.env.EXPO_PUBLIC_VARNAME`) with the corresponding environment variable values in **.env** files present on your development machine. 12 13Because your EAS Build job runs on a remote server, these **.env** files might not be available. For instance, **.env** files are excluded from your uploaded project if they are listed in **.gitignore** or not committed to your local version control system. Additionally, you may want to use environment variables outside of your JavaScript code to customize your app binary at build time, such as setting a bundle identifier or a private key for an error reporting service. Therefore, EAS Build lets you set per-build-profile environment variables within **eas.json** as well as sensitive values that should not be committed to source control via EAS Secrets. 14 15## Setting plaintext environment variables in eas.json 16 17### For use in application code 18 19If you set variables in a **.env** file for local development as described in the [environment variables guide](/guides/environment-variables), you can set those same variables in a build profile in **eas.json**. For instance, you might set an API URL variable to a local backend server when developing locally, a test server for testing, and a production server for production builds. 20 21In this case, your **.env** file might look like this: 22 23```bash .env 24EXPO_PUBLIC_API_URL=http://api.local 25``` 26 27Add any applicable **.env** files to your **.gitignore** (and **.easignore**, if your project has one) file so they are not uploaded with your EAS Build job: 28 29```bash .gitignore 30# ignores all .env files 31.env* 32``` 33 34Then you can set the same environment variable for each build profile in **eas.json**: 35 36```json eas.json 37{ 38 "build": { 39 "production": { 40 "env": { 41 "EXPO_PUBLIC_API_URL": "https://api.production.com" 42 } 43 }, 44 "test": { 45 "env": { 46 "EXPO_PUBLIC_API_URL": "https://api.test.com" 47 } 48 } 49 } 50} 51``` 52 53Any reference to `process.env.EXPO_PUBLIC_API_URL` will be substituted for the applicable value depending on the environment. 54 55> `EXPO_PUBLIC_` variable replacement is available in SDK 49 and higher. See notes for [SDK 48 and lower](/guides/environment-variables#environment-variables-in-sdk-48-and-lower). 56 57### For use by your Expo config 58 59You can use environment variables in a [dynamic config](/workflow/configuration/#dynamic-configuration) (**app.config.js**) to change how your app is built. For instance, you might want to change your app icon or short name for a test build. 60 61Set the variable in your build profile: 62 63```json eas.json 64{ 65 "build": { 66 "test": { 67 "env": { 68 "APP_ICON": "./assets/icon-test.png", 69 "APP_NAME": "My App (Test)" 70 } 71 } 72 } 73} 74``` 75 76Then reference that variable in your **app.config.js**, providing fallbacks for local development: 77 78```js app.config.js 79module.exports = { 80 // use the variable if it's defined, otherwise use the fallback 81 icon: process.env.APP_ICON || './assets/icon.png', 82 name: process.env.APP_NAME || 'My App', 83}; 84``` 85 86> All environment variables in your **eas.json** build profile are available when evaluating **app.config.js**. It's a good practice to only use the `EXPO_PUBLIC_` prefix for variables used within your application code. 87 88### For use by other build steps 89 90Any environment variables set in your **eas.json** build profile are also available to other build steps. 91 92You can also set environment variables dynamically during the build process. The `set-env` executable is available in the `PATH` on EAS Build workers, and can be used to set environment variables that will be visible in the next build phases. 93 94For example, you can add the following in one of the [EAS Build hooks](/build-reference/npm-hooks/) and the environment variable `EXAMPLE_ENV` will be available until the end of the build job. 95 96<Terminal cmd={['set-env EXAMPLE_ENV "example value"']} /> 97 98## Built-in environment variables 99 100The following environment variables are exposed to each build job and can be used within any build step. They are not set when evaluating **app.config.js** locally: 101 102- `CI=1` - indicates this is a CI environment 103- `EAS_BUILD=true` - indicates this is an EAS Build environment 104- `EAS_BUILD_PLATFORM` - either `android` or `ios` 105- `EAS_BUILD_RUNNER` - either `eas-build` for EAS Build cloud builds or `local-build-plugin` for [local builds](local-builds) 106- `EAS_BUILD_ID` - the build ID, for example, `f51831f0-ea30-406a-8c5f-f8e1cc57d39c` 107- `EAS_BUILD_PROFILE` - the name of the build profile from **eas.json**, for example, `production` 108- `EAS_BUILD_GIT_COMMIT_HASH` - the hash of the Git commit, for example, `88f28ab5ea39108ade978de2d0d1adeedf0ece76` 109- `EAS_BUILD_NPM_CACHE_URL` - the URL of npm cache ([learn more](/build-reference/private-npm-packages)) 110- `EAS_BUILD_MAVEN_CACHE_URL` - the URL of Maven cache ([learn more](/build-reference/caching/#android-dependencies)) 111- `EAS_BUILD_COCOAPODS_CACHE_URL` - the URL of CocoaPods cache ([learn more](/build-reference/caching/#ios-dependencies)) 112- `EAS_BUILD_USERNAME` - the username of the user initiating the build (it's undefined for bot users) 113- `EAS_BUILD_WORKINGDIR` - the remote directory path with your project 114 115## Using secrets in environment variables 116 117To provide your build jobs with access to values that are too sensitive to include in your source code and Git repository, you can use "Secrets". 118 119A secret is made up of a name and a value. The name can only contain alphanumeric characters and underscores. The value is limited to 32 KiB. 120 121The value can be either a file or a string value. For a file, its contents are saved to a temporary file on EAS Build servers. The file path is available via the environment variable. For example, if you created a file secret named `SECRET_FILE`, EAS Build will create a file at `/Users/expo/workingdir/environment-secrets/__UNIQUE_RANDOM_UUID__`, and `SECRET_FILE` will be set to that path. 122 123The secret values are encrypted at rest and in transit and are only decrypted in a secure environment by EAS servers. 124 125You can create up to 100 account-wide secrets for each Expo account and 100 app-specific secrets for each app. Account-wide secrets will be exposed to every build environment across all of your apps. App-specific secrets only apply to the app they're defined for and will override any account-wide secrets with the same name. 126 127You can manage secrets through the Expo website and EAS CLI. 128 129> **warning** Always remember that **anything that is included in your client side code should be considered public and readable to any individual that can run the application**. 130> EAS Secrets are intended to be used to provide values to an EAS Build job so that they may be used during the build process. 131> Examples of correct usage include setting the `NPM_TOKEN` for installing private packages from npm, or a Sentry API key to create a release and upload your sourcemaps to their service. 132> EAS Secrets do not provide any additional security for values that you end up embedding in your application itself, such as an AWS access key or other private keys. 133 134### Secrets on the Expo website 135 136To create **account-wide secrets**, navigate to [the "Secrets" tab in your account or organization settings](https://expo.dev/accounts/[account]/settings/secrets). 137 138To create **app-specific secrets**, navigate to [the "Secrets" tab in your project dashboard](https://expo.dev/accounts/[account]/projects/[project]/secrets). If you haven't published your project yet and it isn't visible on the website, you can create it on the website from this link. 139 140### Adding secrets with EAS CLI 141 142To create a new secret, run `eas secret:create`: 143 144<Terminal 145 cmd={[ 146 '$ eas secret:create --scope project --name SECRET_NAME --value secretvalue --type string', 147 '✔ ️Created a new secret SECRET_NAME on project @fiberjw/goodweebs.', 148 ]} 149/> 150 151To view any existing secrets for this project, run `eas secret:list`: 152 153<Terminal 154 cmd={[ 155 '$ eas secret:list', 156 'Secrets for this account and project:', 157 '┌────────────────┬────────┬─────────┬──────────────────────────────────────┬─────────────────┐', 158 '│ Name │ Type │ Scope │ ID │ Updated at │', 159 '├────────────────┼────────┼─────────┼──────────────────────────────────────┼─────────────────┤', 160 '│ APP_UPLOAD_KEY │ string │ account │ 366bd434-b538-4192-887c-036c0eddedec │ Oct 05 11:51:46 │', 161 '├────────────────┼────────┼─────────┼──────────────────────────────────────┼─────────────────┤', 162 '│ NPM_TOKEN │ string │ project │ 03f4881f-88fd-4d94-9e35-a5c34d39c2f2 │ Oct 05 11:51:33 │', 163 '├────────────────┼────────┼─────────┼──────────────────────────────────────┼─────────────────┤', 164 '│ SECRET_FILE │ file │ project │ 72c7ac1e-78d0-4fa2-b105-229260cecc88 │ Oct 05 11:52:12 │', 165 '├────────────────┼────────┼─────────┼──────────────────────────────────────┼─────────────────┤', 166 '│ sentryApiKey │ string │ project │ 88dd0296-9119-4d50-a91b-1f646733f569 │ Oct 05 11:51:40 │', 167 '└────────────────┴────────┴─────────┴──────────────────────────────────────┴─────────────────┘', 168 ]} 169/> 170 171### Importing secrets from a dotenv file 172 173If you're using a **.env** file for storing your secrets locally, you can use the `eas secret:push` command to import all of them to EAS: 174 175<Terminal 176 cmd={[ 177 '$ eas secret:push --scope project --env-file ./eas/.env', 178 '✔ Creating secrets on account johndoe…', 179 '✔ Created the following secrets on account johndoe:', 180 '- ABC', 181 '- DEF', 182 '- GHI', 183 ]} 184/> 185 186Beware that EAS CLI will fail if some of the secrets defined in the dotenv file already exist on the server. To force override those secrets, pass the `--force` flag to the command. 187 188#### Doppler integration 189 190You can use the `eas secret:push` command to integrate EAS with your [Doppler](https://doppler.com/) project: 191 192<Terminal 193 cmd={[ 194 '$ doppler run --mount ./eas/.env -- eas secret:push --scope project --env-file ./eas/.env', 195 ]} 196/> 197 198### Accessing secrets in EAS Build 199 200After creating a secret, you can read it on subsequent EAS Build jobs with `process.env.VARIABLE_NAME` from Node.js or in shell scripts as `$VARIABLE_NAME`. 201 202## Common questions 203 204### Can EAS Build use .env files? 205 206Environment variables defined in a **.env** file are only considered by the Expo CLI. Therefore, if you upload a **.env** file to EAS Build, it can be used to inline `EXPO_PUBLIC_` variables into your application code. 207 208However, the recommended practice is to use **.env** files in your local environment, while defining environment variables for EAS Build in **eas.json**. Environment variables defined in your **eas.json** build profile will be used when evaluating your **app.config.js** when running `eas build` and will be available to all steps of the build process on the EAS Build server. 209 210This may result in some duplication of variables between **.env** files and **eas.json** build profiles, but makes it easier to see what variables will be applied across all environments. 211 212### How do I share environment variables between my local development environment, EAS Update, and EAS Build? 213 214Environment variables defined in **eas.json** are only available when running an EAS Build job. However, you may wish to change variables used within your application code based on the build profile while minimizing duplicating values you might keep in an **.env** file for local development or for when publishing to EAS Update. 215 216Our [Environment variables in EAS Update guide](/eas-update/environment-variables/#sharing-environment-variables-between-local-development-eas-update-and-eas-build) describes a few approaches for sharing environment variables between all of these contexts. 217 218### How are naming collisions between secrets, the `env ` field in **eas.json**, and **.env** files handled? 219 220Environment variables are applied in the following order: 221 2221. **eas.json** build profile `env` field 2232. Environment variables defined in EAS Secrets 2243. **.env** files committed to source control and are not in **.easignore** 225 226Variable sources applied last will overwrite the previously loaded source for variables with the same name. So, a secret created on the Expo website or with `eas secret:create` will take precedence over an environment variable of the same name that is set through the `env` field in **eas.json**. 227 228For example, if you create a secret with the name `MY_TOKEN` and value `secret` and also set `"env": { "MY_TOKEN": "public" }` in your **eas.json**, then `process.env.MY_TOKEN` on EAS Build will evaluate to `secret`. 229 230### How do environment variables work for my Expo Development Client builds? 231 232Environment variables set in your build profile that impact **app.config.js** will be used for configuring the development build. When you run `npx expo start` to load your app inside of your development build, only environment variables that are available on your development machine will be used. 233 234### Can I just set my environment variables on a CI provider? 235 236Environment variables must be defined in **eas.json** to be made available to EAS Build builders. If you are [triggering builds from CI](/build/building-on-ci) this same rule applies, and you should be careful to not confuse setting environment variables on GitHub Actions (or the provider of your choice) with setting environment variables and secrets in **eas.json**. 237 238### How to upload a secret file and use it in my app config? 239 240A common use case for uploading file secrets to EAS is when you want to supply your build with the **google-services.json** and **GoogleService-Info.plist** files. Usually, those files should not be checked into the repository. 241 242Here's an example of how to upload **google-services.json** to EAS and use it in your app config: 243 244<Step label="1"> 245 246Upload the file to EAS. 247 248<Terminal 249 cmd={[ 250 '$ eas secret:create --scope project --name GOOGLE_SERVICES_JSON --type file --value ./path/to/google-services.json', 251 '✔ ️Created a new secret GOOGLE_SERVICES_JSON on project @user/myproject.', 252 ]} 253/> 254 255</Step> 256 257<Step label="2"> 258 259Use **app.config.js** to read the path to **google-services.json**. 260 261```js app.config.js 262export default { 263 // ... 264 android: { 265 googleServicesFile: process.env.GOOGLE_SERVICES_JSON, 266 // ... 267 }, 268}; 269``` 270 271</Step> 272