Skip to content

Using react-native-dotenv with productFlavors for CI/CD #515

Not planned
@aokdev06

Description

@aokdev06

Problem:
When I run the ./gradlew assembleStagingRelease command, the build process uses the default .env file instead of the .env.staging file.

Question:
Is there a way to make the ./gradlew assembleStagingRelease command use the .env.staging file for building with the react-native-dotenv package?

Additional Context:
I am using a third-party CI/CD tool that does not support setting environment variables in the build command, so I need a solution that works directly with Gradle.

Current Setup:

  • babel.config.js:
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module:react-native-dotenv',
      {
        moduleName: '@env',
        path: '.env',
        blocklist: null,
        allowlist: null,
        safe: false,
        allowUndefined: true,
        verbose: false,
      },
    ],
  ],
}
  • android/app/build.gradle:
android {
    ...
    flavorDimensions "env"
    productFlavors {
        dev {
            dimension "env"
        }
        staging {
            dimension "env"
            versionNameSuffix "-staging"
        }
        preprod {
            dimension "env"
            versionNameSuffix "-preprod"
        }
        prod {
            dimension "env"
            versionNameSuffix "-prod"
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def versionName = variant.versionName
            def flavor = variant.flavorName
            def buildType = variant.buildType.name
            def newApkName = "app-v${versionName}-${flavor}-${buildType}.apk"
            def newAabName = "app-v${versionName}-${flavor}-${buildType}.aab"
            if (output.outputFile.name.endsWith('.apk')) {
                outputFileName = newApkName
            } else if (output.outputFile.name.endsWith('.aab')) {
                outputFileName = newAabName
            }
        }
    }
}

Activity

github-actions

github-actions commented on Jan 31, 2025

@github-actions

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/515

phubner

phubner commented on Jan 31, 2025

@phubner

I think per the documentation you can do APP_ENV=staging ./gradlew assembleStagingRelease. Note I haven't run this, but I believe this is the intent.

aokdev06

aokdev06 commented on Jan 31, 2025

@aokdev06
Author

I think per the documentation you can do APP_ENV=staging ./gradlew assembleStagingRelease. Note I haven't run this, but I believe this is the intent.

Hi @phubner, thanks for your response.

Normally, I can successfully build on my local device using the following command:
"android:staging-release": "APP_ENV=staging npx react-native build-android --mode=release --tasks assembleRelease"

However, the CI/CD tool I’m using supports productFlavors methods for Android. When I specify StagingRelease as the release method, it runs ./gradlew assembleStagingRelease. Because of this, I can’t prepend the command with APP_ENV.

Still, I appreciate your help.

phubner

phubner commented on Jan 31, 2025

@phubner

I see, sorry for the confusion @aokdev06. I am looking for a clean solution to this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @phubner@aokdev06

      Issue actions

        Using react-native-dotenv with productFlavors for CI/CD · Issue #515 · goatandsheep/react-native-dotenv