Skip to content

Instantly share code, notes, and snippets.

@KazaiMazai
Last active June 13, 2023 13:42
Show Gist options
  • Save KazaiMazai/4f2c8ea68e7826116a088cf702294354 to your computer and use it in GitHub Desktop.
Save KazaiMazai/4f2c8ea68e7826116a088cf702294354 to your computer and use it in GitHub Desktop.
Scripts for managing env vars in Xcode and CI. Here is an article with detailed description: https://kazaimazai.com/secret-env-vars-xcode-ci
SECRET_ONE = SECRET_ONE_EXAMPLE_VALUE
SECRET_TWO = SECRET_TWO_EXAMPLE_VALUE
#!/bin/bash
# This script should be run as a phase script before building the project
#
# The script:
# 1. Copies .env-example.xcconfig to .env.xcconfig if .env.xcconfig does not exist
# Skip if index build
if [ $ACTION = "indexbuild" ]; then exit 0; fi
echo "EnvironmentVars init"
CONFIGURATION_PATH="${SRCROOT}/ProjectName/Configuration/EnvironmentVars"
# Path to the env.xcconfig file
xcconfig_file="${CONFIGURATION_PATH}/env.xcconfig"
if [ -f "$xcconfig_file" ]; then
echo "The file '$xcconfig_file' found."
else
echo "The file '$xcconfig_file' does not exist."
example_xcconfig_file="${CONFIGURATION_PATH}/env-example.xcconfig"
cp "$example_xcconfig_file" "$xcconfig_file"
echo "Copied from '$example_xcconfig_file' to '$xcconfig_file'"
fi
#!/bin/bash
# This script should be run as a phase script before building the project
#
# The script:
# 1. Reads env vars from .env.xcconfig
# 2. Performs EnvironmentVars code generation using Sourcery and EnvironmentVars.stencil template
#
# Important to note: Sourcery path used here is valid for Sourcery CocoaPods installation. Update it if needed
# Skip if index build
if [ $ACTION = "indexbuild" ]; then exit 0; fi
echo "EnvironmentVars CodeGen"
CONFIGURATION_PATH="${SRCROOT}/ProjectName/Configuration/EnvironmentVars"
TEMPLATE_PATH="${CONFIGURATION_PATH}/EnvironmentVars.stencil"
# Path to the env.xcconfig file
xcconfig_file="${CONFIGURATION_PATH}/env.xcconfig"
if ! [ -f "$xcconfig_file" ]; then
echo "The file '$xcconfig_file' does not exist. Run 'env_vars_init.sh' first."
exit 1;
fi
echo "Performing EnvironmentVars CodeGen."
# Declare an empty string to hold the env variables in comma separated format
env_vars=""
# Read each line from the xcconfig file
while IFS= read -r line; do
# Ignore comments and empty lines
if [[ "$line" =~ ^\s*# ]] || [[ -z "$line" ]]; then
continue
fi
# Extract the variable name and value from the line
var_name="$(echo "$line" | cut -d'=' -f1 | sed 's/ //g')"
var_value="$(echo "$line" | cut -d'=' -f2- | sed 's/^ *//;s/ *$//')"
# Append the variable to the env_vars string
env_vars+="$var_name=$var_value,"
done < "$xcconfig_file"
# Remove the trailing comma
env_vars="${env_vars%,}"
$PODS_ROOT/Sourcery/bin/sourcery --templates "${TEMPLATE_PATH}" --sources "${CONFIGURATION_PATH}" --output "${CONFIGURATION_PATH}" --args "$env_vars"
#!/bin/bash
# This script could be run as a phase script if env vars needed
#
# The script:
# 1. Reads env vars from .env.xcconfig
# 2. Exports env vars
# Skip if index build
if [ $ACTION = "indexbuild" ]; then exit 0; fi
echo "EnvironmentVars Export"
CONFIGURATION_PATH="${SRCROOT}/ProjectName/Configuration/EnvironmentVars"
# Path to the env.xcconfig file
xcconfig_file="${CONFIGURATION_PATH}/env.xcconfig"
if ! [ -f "$xcconfig_file" ]; then
echo "The file '$xcconfig_file' does not exist. Run 'env_vars_init.sh' first."
exit 1;
fi
echo "Performing EnvironmentVars Export..."
# Read each line from the xcconfig file
while IFS= read -r line; do
# Ignore comments and empty lines
if [[ "$line" =~ ^\s*# ]] || [[ -z "$line" ]]; then
continue
fi
# Extract the variable name and value from the line
var_name="$(echo "$line" | cut -d'=' -f1 | sed 's/ //g')"
var_value="$(echo "$line" | cut -d'=' -f2- | sed 's/^ *//;s/ *$//')"
export "$var_name"="$var_value"
done < "$xcconfig_file"
echo "EnvironmentVars Exported."
//swiftlint:disable all
struct EnvironmentVars {
/// Generated using 'EnvironmentVars.stencil' template
static let secretNumberOne = "{{ argument.SECRET_ONE }}"
static let secretNumberTwo = "{{ argument.SECRET_TWO }}"
}
#!/bin/bash
# This script should be run on CI from project dir as a step before building the project
# First, Make sure that all env vars from .env-example.xcconfig are set on CI
#
# The script:
# 1. Reads all env vars keys defined in env-example.xcconfig
# 2. Writes values from env vars to env.xcconfig
config_path="./ProjectName/Configuration/EnvironmentVars"
# Path to the env.xcconfig file
xcconfig_file="${config_path}/env.xcconfig"
# Path to the env.xcconfig file
example_xcconfig_file="${config_path}/env-example.xcconfig"
# Open the source and destination files for reading and writing
exec 3<$example_xcconfig_file
exec 4>$xcconfig_file
echo "Writing env vars to '$xcconfig_file'..."
# Read the source file line by line and write to the destination file
while read -u 3 line; do
# Extract the variable name and value from the line
var_name="$(echo "$line" | cut -d'=' -f1 | sed 's/ //g')"
default_var_value="$(echo "$line" | cut -d'=' -f2- | sed 's/^ *//;s/ *$//')"
# Extract the variable value from env vars
var_value="${!var_name}"
if [[ -n "$var_value" ]]; then
echo "$var_name = $var_value" >&4
echo "Wrote '$var_name' from env vars."
else
echo "$var_name = $default_var_value" >&4
echo "Wrote default value for '$var_name'."
fi
done
# Close the files
exec 3<&-
exec 4>&-
echo "Wrote env vars to '$xcconfig_file' successfully."
@KazaiMazai
Copy link
Author

Detailed description and usage example can be found here: https://kazaimazai.com/secret-env-vars-xcode-ci

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