Importing variables from JSON
How to import variables from a JSON file with dart-define-from-file
You can use Flutter’s dart-define-from-file
functionality to provide variables in JSON format for your app at build time. You can store your JSON configuration securely in Codemagic and make it available in your pipeline at build time.
Configure the settings.json file
Create the desired JSON configuration file.
{
"value_one": "abc",
"value_two": "xyz"
}
Base64 encode your settings.json file
Open your Terminal and Base64 encode your JSON file as follows. This will also copy it to your clipboard, making it easier for you to paste the value into Codemagic.
cat settings.json | base64 | pbcopy
Add your JSON as an environment variable in Codemagic
You can now add the Base64 encoded file as an environment variable in Codemagic as follows:
- Open your Codemagic app settings, and go to the Environment variables tab.
- Enter the desired Variable name.
- Enter the Variable value.
- Enter the variable group name, e.g. json. Click the button to create the group.
- If the Secure option is selected, the variable will be protected by encryption. Its value will not be visible in Codemagic UI or build logs, it will be transferred securely to the build machine and made available only while the build is running. The whole build machine will be destroyed after build ends.
- Click the Add button to add the variable.
Import the environment variable group in codemagic.yaml
Import your variable group in your codemagic.yaml as follows:
workflows:
workflow-name:
environment:
groups:
- json_config
Using your settings.json to build your app
You will now need to save the settings.json file to disk so it can be used in your workflow. Assuming you named your variable JSON_CONFIG
you can do this as follows:
workflows:
workflow-name:
...
scripts:
- name: Export JSON configuation
script: echo "$JSON_CONFIG" | base64 --decode > settings.json
...
- name: Flutter build for iOS
script: |
flutter build ipa --release \
--build-name=1.0.0 \
--build-number=$(($(app-store-connect get-latest-testflight-build-number "$APP_ID") + 1)) \
--dart-define-from-file=settings.json
--export-options-plist=/Users/builder/export_options.plist
You settings.json
file is saved to the same directory as your app and this is referenced in dart-define-from-file
.
Using Workflow Editor
You can add your Base64 encoded settings.json as an evironment variable in the Workflow Editor, and then script to decode the value to disk in the Post-clone script section.
echo "$JSON_CONFIG" | base64 --decode > settings.json
In the Build section use dart-define-from-file
in the Build arguments