可以在 Podfile 中使用环境变量吗?

huangapple go评论63阅读模式
英文:

Is it possible to use environmental variables in Podfile

问题

我最近在Podfile中看到了这种代码行:

pod 'PodName', git => 'https://' + ENV['USER'] + ':' + ENV['PASS'] + 'url'

问题是,这两个变量ENV['USER']ENV['PASS']是在Podfile的顶部设置的,我认为它们根本不应该出现在Podfile中,出于安全考虑(如果我错了,请纠正我)。因此,我尝试将它们从Podfile中移出。

我尝试了两种方法:

  • 将它们移到模式中的环境变量中
  • 创建配置设置文件,并将它们放在那里

然而,这两种方法都不起作用,或者我不知道如何操作。
所以,问题是,是否可以在Podfile中使用一些外部变量?
如果可以,如何操作?

附注:
有趣的是,如果我从文件顶部删除这两个变量,然后将它们放入pod-git的URL中,我会收到以下错误消息:“无法将nil隐式转换为字符串”,好像这两个变量根本不存在。

英文:

I recently came across that kind of line of code in Podfile

pod 'PodName', git => 'https://' + ENV['USER'] + ':' + ENV['PASS'] + 'url'

The thing is those two, ENV['USER']& ENV['PASS'] are set at the top of Podfile which I don't think should be in Podfile at all as per security measures (please correct me if I'm wrong). So I've tried to move them out from Podfile.

Two approaches I've tried:

  • move them into Environment Variables in the schema
  • create Configuration Settings File and put them there

However, this not works, or I do not know how to do this.
So, the question is, is this possible to use some external variables in the Podfile?
If so, how?

Side note:
Interesting is that if I will remove the variables from the top of the file and put them into the pod-git URL I am getting the following error "no implicit conversion of nil into String" as if both variables are not there at all.

答案1

得分: 2

我不确定Podfile是否可以从环境中读取(@FreeNickname的回答似乎是指Fastfile),但我对私有pod的处理方式一直有点不同。

与其在URL中使用用户名/密码,我们使用一个只有特定帐户才能访问的私有存储库。CocoaPods使用已登录用户的凭据来获取存储库的内容。

可选地,您可以设置一个私有的spec存储库,定义您的私有pod的最新版本。

在涉及CI时,您必须确保CI运行程序也具有访问这些存储库的正确凭据。

英文:

I'm not sure if the Podfile can read from the environment (@FreeNickname's answer seems to refer to the Fastfile instead), but my approach to private pods has always been a bit different.

Instead of a username/password in the URL, we use a private repository that only certain accounts have access to. CocoaPods uses the credentials of the signed in user to fetch the contents of the repo.

Optionally, you can set up a private spec repository that defines what are the latest versions of your private pods.

When it comes to CI, you have to make sure that the CI runner also has the right credentials to access this repositories.

答案2

得分: 0

是的,这是可能的:https://docs.fastlane.tools/advanced/other/

您可以将.env.env.default文件放在与您的Fastfile相同的目录中。您可以在这些文件中配置环境变量,然后在您的FastfileAppfile等中使用它们:

.env

BUNDLE_IDENTIFIER = "com.company.app"
SCHEME = "My App Scheme"

Appfile

app_identifier(ENV['BUNDLE_IDENTIFIER'])

Fastfile

...
	lane :beta do
		build_app(scheme: ENV['SCHEME'],
			include_bitcode: false,
			export_xcargs: "-allowProvisioningUpdates"
    	)
    	upload_to_testflight
	end
...

您还可以拥有多个环境:.env.CustomEnvironment.env.AnotherEnvironment等。然后,您可以使用 --env 参数将要使用的环境传递给 Fastlane:

fastlane <lane_name> --env CustomEnvironment
英文:

UPD: I totally misread the question, my apologies. My answer is about Fastlane (so, Fastfile, Appfile, etc.). I'll leave it here just in case.


THE INFO BELOW IS ABOUT FASTLANE, NOT COCOAPODS!

Yes, it is possible: https://docs.fastlane.tools/advanced/other/

You can put .env or .env.default files in the same directory as your Fastfile. You can configure the environment variables in those files and then use them in your Fastfile, Appfile, etc.:

.env

BUNDLE_IDENTIFIER = &quot;com.company.app&quot;
SCHEME = &quot;My App Scheme&quot;

Appfile

app_identifier(ENV[&#39;BUNDLE_IDENTIFIER&#39;])

Fastfile

...
	lane :beta do
		build_app(scheme: ENV[&#39;SCHEME&#39;],
			include_bitcode: false,
			export_xcargs: &quot;-allowProvisioningUpdates&quot;
    	)
    	upload_to_testflight
	end
...

You can also have multiple environments: .env.CustomEnvironment, .env.AnotherEnvironment, etc. You can then pass the environment to use to Fastlane using the --env parameter:

fastlane &lt;lane_name&gt; --env CustomEnvironment

huangapple
  • 本文由 发表于 2023年7月20日 18:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定