Skip to content

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

Closed as 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
            }
        }
    }
}

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

    Issue actions