简体   繁体   English

如何在应用中心的 Xamarin.Forms 中使用设置环境变量

[英]How can I use set Environment Variables in Xamarin.Forms from App Center

I have a Xamarin Forms app which is being built using App Center.我有一个正在使用 App Center 构建的 Xamarin Forms 应用程序。 The app contains some code that looks like:该应用程序包含一些代码,如下所示:

var secret= "secretvaluegoeshere";

I then use the secret to communicate with an API.然后我使用该密钥与 API 进行通信。 Now I want to extract that secret from code so as not to having it in source control and inject it when building on App Center.现在我想从代码中提取该秘密,以免将其置于源代码控制中并在 App Center 上构建时注入它。 Environment Variables seem like they should solve this very problem but the examples in the docs don't mention how they can get into code (only nuget and gradle config). 环境变量似乎应该解决这个问题,但文档中的示例没有提到它们如何进入代码(仅 nuget 和 gradle 配置)。 Is there a way to do what I want with Environment Variables or should I be doing this another way?有没有办法用环境变量做我想做的事,还是我应该用另一种方式做?

您可以通过预构建脚本将其注入到项目中(直接在代码中替换一些占位符,创建一些资源文件等),然后在运行时从那里读取它。

So it turns out this is surprisingly easy by following these steps: 因此,通过以下步骤可以很容易地实现这一点:

Install the Mobile.BuildTools NuGet package in your project. 在项目中安装Mobile.BuildTools NuGet包。

Add a secrets.json file in the root of your project (this should be excluded from source control using .gitignore). 在项目的根目录中添加secrets.json文件(这应该使用.gitignore从源代码管理中排除)。

Add your secret to the secrets.json file, so in my case I'm going to add a SearchApiKey, obviously you can add as many secrets as you want: 将你的秘密添加到secrets.json文件,所以在我的情况下我将添加一个SearchApiKey,显然你可以添加任意数量的秘密:

{
  "SearchApiKey": "SUPERSECRETGOESHERE"
}

Build your project and this will generate a static class called Secrets with a property SearchApiKey, you can find it under the obj folder if you want to have a look at it. 构建你的项目,这将生成一个名为Secrets的静态类,其属性为SearchApiKey,如果你想查看它,你可以在obj文件夹下找到它。

You can now access this class and it's properties in your code so I just do: 您现在可以在代码中访问此类及其属性,所以我只是这样做:

var secret = Secrets.SearchApiKey;

Finally to pass the secret into your build on AppCenter you need to add an Environment Variable that matches the property name prepended with Secret_ so in my case it's name is Secret_SearchApiKey and set it's value. 最后要将秘密传递到AppCenter上的构建中,您需要添加一个与Secret_前面的属性名称匹配的环境变量,因此在我的情况下,它的名称是Secret_SearchApiKey并设置它的值。

You can check out the Mobile.BuildTools GitHub repository for more information. 您可以查看Mobile.BuildTools GitHub存储库以获取更多信息。

If you don't want to use additional nuget packages, you can follow example posted from the official AppCenter project on GitHub如果您不想使用额外的 nuget 包,您可以按照GitHub 官方 AppCenter 项目中发布的示例进行操作

What you have to do is create a new environment variable on AppCenter with the value you want to set.您需要做的是在 AppCenter 上使用您要设置的值创建一个新的环境变量。 Then save a file named appcenter-pre-build.sh at the same lever of your .csproj file and create your own code like in the example.然后在.csproj文件的同一级别保存一个名为appcenter-pre-build.sh的文件,并像示例中一样创建自己的代码。

Once you have added the file to your repo, in order to save the detected scripts to the build configuration, you have to hit the “Save” or “Save & Build” button in the build configuration将文件添加到存储库后,为了将检测到的脚本保存到构建配置中,您必须在构建配置中点击“保存”或“保存并构建”按钮

It worked for me like a charm a momment ago, hope it helps someone!刚才它对我来说就像一个魅力,希望它可以帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Xamarin.Forms (Android) 中读取 Microsoft App Center 环境变量? - How to read Microsoft App Center Environment Variables in Xamarin.Forms (Android)? 我如何在xamarin.forms应用程序中使用permissionplugin和zxing nuget pkg? - How i can use permissionplugin with zxing nuget pkg in xamarin.forms app? 如何在NUnit Library项目中使用Xamarin.Forms? - How can I use Xamarin.Forms in a NUnit Library project? 如何检测 Xamarin.Forms 应用程序何时在应用程序中心运行? - How to detect when Xamarin.Forms app is running in App Center? 如何从 Xamarin.Forms 内本机启动外部应用程序? - How can I natively launch an external app from within Xamarin.Forms? 如何从 Xamarin.Forms 中启动谷歌照片应用程序? - How can I launch google photos app from within Xamarin.Forms? 如何从 Xamarin.Forms 获得连续的地理定位? - How can I get a continous geolication from Xamarin.Forms? 从代码设置绑定在xamarin.forms应用中不起作用 - Set binding from code dont work in xamarin.forms app How can I restrict my Azure Web App API access to only my Xamarin.Forms app? - How can I restrict my Azure Web App API access to only my Xamarin.Forms app? 无法从Xamarin.Forms应用连接到SignalR服务器 - Can't connect to SignalR server from Xamarin.Forms app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM