# Secret Environment Variables not used in Xcode Archive & Export

**URL:** <https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994>\
**Category:** Question & Answer\
**Tags:** step, ios, xcode\
**Created:** [February 1, 2018, 4:14pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994 "2018-02-01T16:14:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnystrom](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/rnystrom/32/1178_2.png) [@rnystrom](https://discuss.bitrise.io/u/rnystrom)\
**Post date:** [February 1, 2018, 4:14pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/1 "2018-02-01T16:14:14Z")

</div>

I’m successfully building and running tests for our iOS app that uses environment variables to set things like API secrets/keys. Some of these tests access these secrets, so it seems like at test time, they exist.

However when added the “Deploy to iTunes” step, the app is flagged as crashing. In our crash logs, I can see that our [asserts](https://github.com/rnystrom/GitHawk/blob/master/Classes/Systems/Secrets.swift) are firing b/c environment variables are missing.

I’m not at all sure why the environment variables appear to be used during testing, but are not included during archive.

I’m very new to Bitrise, having come from Buddybuild and using this exact same setup there. Any guidance about where to start debugging would be very appreciated!

 ![envvar](https://us1.discourse-cdn.com/flex016/uploads/bitrise/original/2X/9/91664e213f1e02f25e1c304bd92f6bd46bbf67c8.png)

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [February 1, 2018, 4:23pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/2 "2018-02-01T16:23:13Z")

</div>

Hi @rnystrom,

The environment variables you set on [bitrise.io](http://bitrise.io) are available in the build / during the build as environment variables. That’s all, those are just env vars (including secrets).

It’s the same as if you set some env vars on your own Mac then run a build (e.g. with `xcodebuild` from your terminal). The env vars can be used during the build, but whether those will be **built into** the app depends on your build scripts.

We do not modify your project in any way, the Xcode steps just run Xcode actions (e.g. Xcode Test runs Xcode’s Test action). So whether the env vars will be included in the generated app depends on your Xcode configs.

I’m not 100% sure but I suspect you use(d) buddybuild’s SDK, or maybe they did modify your project, and that’s why those are included in the final `.ipa`. But in general environment variables are **environment** related, those exist in the environment they are defined in.

The usual solution to build any variable into the final .ipa / app is to have a script somewhere which writes the variables/secrets into a file, and then compile that file into the project. This can be e.g. a plist or json file, as long as it’s compiled/built into the .ipa/app.

I hope this helps, if you’d have any questions just let us know! 🙂

---

<div class="post-metadata">

**Author:** ![rnystrom](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/rnystrom/32/1178_2.png) [@rnystrom](https://discuss.bitrise.io/u/rnystrom)\
**Post date:** [February 1, 2018, 4:30pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/3 "2018-02-01T16:30:08Z")

</div>

Aha! Good to know. We weren’t using the BB SDK, but I’ll pull their build logs and see if they were doing something special at the `xcodebuild` step.

It sounds like that at the very least I can use our secret env vars and pass them along to the build step? Seems doable.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [February 1, 2018, 4:32pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/4 "2018-02-01T16:32:38Z")

</div>

> [@rnystrom](#):
>
> It sounds like that at the very least I can use our secret env vars and pass them along to the build step?

Sure! That definitely works! 🙂

> [@rnystrom](#):
>
> I’ll pull their build logs and see if they were doing something special at the xcodebuild step.

Great idea, keep us updated, I’m interested! 😉

Quick question: how do you define those variables locally on your Mac, e.g. if you’d want to or do create a test/ad-hoc .ipa?

---

<div class="post-metadata">

**Author:** ![rnystrom](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/rnystrom/32/1178_2.png) [@rnystrom](https://discuss.bitrise.io/u/rnystrom)\
**Post date:** [February 1, 2018, 5:06pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/5 "2018-02-01T17:06:09Z")

</div>

One of the screenshots above is from my local Xcode project file settings, which are ignored from the repo.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [February 1, 2018, 5:10pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/6 "2018-02-01T17:10:50Z")

</div>

Thanks! But those are not used for Archive, right? Only for running directly from Xcode, but not if you do an Archive, generate an `.ipa` and install that on a real device. Though I might be wrong… 🤔

---

<div class="post-metadata">

**Author:** ![rnystrom](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/rnystrom/32/1178_2.png) [@rnystrom](https://discuss.bitrise.io/u/rnystrom)\
**Post date:** [February 2, 2018, 2:48pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/7 "2018-02-02T14:48:47Z")

</div>

Oh nope, you’re right. Sorry! We setup [this Secrets.swift](https://github.com/rnystrom/GitHawk/blob/master/Classes/Systems/Secrets.swift) file and also have a [build phase script](https://gist.github.com/rnystrom/88d1a3284be5950190a4d2913d9a83fb) to replace the contents of the secrets on build.

I think this is where our issue is. We should be making env vars available to Bitrise in this build phase.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [February 2, 2018, 3:25pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/8 "2018-02-02T15:25:47Z")

</div>

Indeed, you can e.g. generate this `Secrets.swift` file with a script, or with a tool like [https://github.com/krzysztofzablocki/Sourcery](https://github.com/krzysztofzablocki/Sourcery)

---

<div class="post-metadata">

**Author:** ![rnystrom](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/rnystrom/32/1178_2.png) [@rnystrom](https://discuss.bitrise.io/u/rnystrom)\
**Post date:** [February 2, 2018, 10:57pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/9 "2018-02-02T22:57:28Z")

</div>

Is there a way to debug why build phase script `echo` output isn’t appearing in the Bitrise logs? I can see output like:

```auto
Running script 'Validate API Keys'

```

But then none of the `echo`, which there must be at least one, is showing up in logs. That’d help w/ debugging where things are missing.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [February 3, 2018, 1:06pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/10 "2018-02-03T13:06:00Z")

</div>

> [@rnystrom](#):
>
> Is there a way to debug why build phase script echo output isn’t appearing in the Bitrise logs?

The output of those scripts is most likely not included in the build’s log, as that’s formatted by `xcpretty` by default.

Do you use our Xcode Archive step? If you do and you also have the **Deploy to [Bitrise.io](http://Bitrise.io)** step in the workflow (by default it should be included) then the raw xcodebuild output log is also available, attached to the build as an artifact:

 ![Screen_Shot_2018-02-01_at_15_35_58](https://us1.discourse-cdn.com/flex016/uploads/bitrise/original/2X/5/5a147e9cbf168084d1f4ef04df4ac938e7ffe05b.png)

That should include the script outputs, it’s the whole, raw output of xcodebuild.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [February 3, 2018, 1:07pm UTC](https://discuss.bitrise.io/t/secret-environment-variables-not-used-in-xcode-archive-export/3994/11 "2018-02-03T13:07:17Z")

</div>

Running the build locally via our CLI can also help with debugging / experimenting on the config:

> [@How to experiment with Bitrise configs locally, on your Mac/Linux](http://discuss.bitrise.io/t/how-to-experiment-with-bitrise-configs-locally-on-your-mac-linux/1751):
>
> Do you want to run a custom Script in your builds, or do you want to experiment with things you just found on the [DevCenter](http://devcenter.bitrise.io/), but would love to make the process faster? If you do, this is the right place for you! It’s actually super simple: Install the Bitrise CLI, which is the exact same runner which runs the builds on the [bitrise.io](http://bitrise.io) VMs. [Bitrise CLI](https://www.bitrise.io/cli) Note: the CLI is open source, repo and code: [GitHub - bitrise-io/bitrise: Bitrise runner CLI - run your automations on your Mac or Linux mach…](https://github.com/bitrise-io/bitrise)
