How to Use Swift Variable Injector for CI/CD

Oct 11, 2024 | Programming

Swift Variable Injector is a handy tool that allows you to inject environment variable values into your Swift code before compilation and deployment. This helps manage different configurations for various environments, all while keeping sensitive keys secure. In this article, we will guide you on how to install and use this tool effectively.

What is Swift Variable Injector?

Imagine you’re a chef preparing three different dishes, each suited for a different occasion: a casual dinner, a formal banquet, and a quick lunch. Each dish requires specific ingredients – the casual dinner uses easy-to-source ingredients, the banquet requires gourmet selections, and the lunch must be quick and simple. In coding terms, these “ingredients” can be thought of as your environment variables, essential for setting up your app correctly based on whether it’s in Development, Testing, or Production.

Installation

To get started with the Swift Variable Injector, you can follow these simple steps:

Manual Installation

  • Clone the repository:
  • git clone https://github.com/LucianoPAlmeida/variable-injector.git
  • Run the install command:
  • make install

Once installed, you can find it in your bin folder ready for use.

Usage

Let’s dive into the usage of Swift Variable Injector:

First, define your environment variables in a struct or class using the $(VAR_NAME) syntax, like so:

struct CI {
    static var serviceAPIKey: String = $(SERVICE_PROD_KEY)
    static var otherAPIKey: String = $(OTHER_PROD_KEY)
}

If your environment variables are set correctly on your build machine, the injector will replace placeholders with actual values.

Your final structure would look something like this after substitution:

struct CI {
    static var serviceAPIKey: String = h344hjk2h4j3h24jk32h43j2k4h32jk4hkj324h
    static var otherAPIKey: String = dsa76d7adas7d6as87d6as78d6aklre423s7d6as8d7s6
}

Using as a Run Script in Build Phases

You can enhance your build process by adding the variable injector as a run script. Here’s how:

if which variable-injector > /dev/null; then
    variable-injector --file $SRCROOT/YourProject/YourGroupFolderPath/File.swift --verbose
else
    echo "Warning: Swift Variable Injector not installed, download from https://github.com/LucianoPAlmeida/variable-injector"
fi

Make sure to place this run script before the Compile Sources phase for it to work effectively.

Troubleshooting

If you encounter any issues, particularly with Xcode 11, try using version 0.3.3 of the tool. You can also ignore specific patterns to prevent replacement, or check logs using the --verbose flag. However, keep in mind that verbose mode will display environment variable values in the logs; use it cautiously, ideally for debugging purposes only.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox