Automating Environment Configuration in Our Project

Introduction

In software development, maintaining different configurations for various environments such as development, staging, and production is crucial. Switching between these environments manually can be time-consuming and prone to errors. To streamline this process, we use an environment configuration shell script that automates the setup based on the specified environment. In this blog, I’ll walk you through our environment configuration script, explaining how it works and how it ensures smooth transitions between environments.

The Environment Configuration Script

Our environment configuration script is designed to replace configuration files and update settings for both iOS and Android projects based on the environment variable passed to it. Here’s a step-by-step breakdown of what the script does:

  1. Copying Environment-Specific Configuration Files
    • The script starts by copying the appropriate configuration files (google-services.json for Android and GoogleService-Info.plist for iOS) from a directory that contains configurations for different environments:
      • cp lib/config/"$1"/google-services.json android/app/google-services.json
      • cp lib/config/"$1"/GoogleService-Info.plist ios/Runner/GoogleService-Info.plist
  2. Environment-Specific Settings
    • The script then checks if the specified environment is production or not. Depending on the environment, it performs a series of file replacements and updates.
    • Production Environment: For the production environment, it updates bundle identifiers, package IDs, and environment variables in the necessary files. After making these changes, it removes any auto-generated backup files and logs a message indicating the successful setup.

      if [ "$1" == "production" ];then
      sed -i -e 's/com.example.envdemo.staging;/com.example.envdemo.prod;/g' ios/Runner.xcodeproj/project.pbxproj
      sed -i -e 's/\<string\>com.example.envdemo.staging\<\/string\>/\<string\>com.example.envdemo.prod\<\/string\>/g' ios/Runner/Info.plist
      sed -i -e 's/"com.example.envdemo.staging"/"com.example.envdemo.prod"/g' android/app/build.gradle
      sed -i -e 's/Environment.STAGING/Environment.PROD/g' lib/main.dart rm ios/Runner.xcodeproj/project.pbxproj-e
      rm android/app/build.gradle-e
      rm ios/Runner/Info.plist-e
      rm lib/main.dart-e
      echo "Production setup changes are done."
    • Staging Environment:
      • For the staging environment, it reverts the changes made for production to their staging equivalents and updates the Android build configuration to use the debug signing config instead of the release signing config.

        else
        sed -i -e 's/com.example.envdemo.prod;/com.example.envdemo.staging;/g' ios/Runner.xcodeproj/project.pbxproj
        sed -i -e 's/\<string\>com.example.envdemo.prod\<\/string\>/\<string\>com.example.envdemo.staging\<\/string\>/g' ios/Runner/Info.plist
        sed -i -e 's/"com.example.envdemo.prod"/"com.example.envdemo.staging"/g' android/app/build.gradle
        sed -i -e 's/Environment.PROD/Environment.STAGING/g' lib/main.dart
        sed -i -e 's/signingConfig signingConfigs.release/signingConfig signingConfigs.debug/g' android/app/build.gradle
        rm ios/Runner.xcodeproj/project.pbxproj-e
        rm android/app/build.gradle-e
        rm ios/Runner/Info.plist-e rm lib/main.dart-e
        echo "Staging setup changes are done."
        fi

    • Using the Script
      • To change the configuration, you can simply run the script with the desired environment as an argument. For example, to switch to the production environment, you would use:
        sh env_config.sh production
      • Similarly, to switch to the staging environment, you would use:
        sh env_config.sh staging

You may also like

Leave a Reply