Skip to main content

03 fastlane自动化IOS工程(三)-自动发布

1 什么是自动发布?

前面的章节中的beta流程尽管能执行,但是,在同步证书的过程中要求开发都输入验证码后才能进行发布。这样一来就算不上是全自动化了。所以本章节就 的内容就是来解决这一问题,跳过开发苹果的2-factor authentication (2FA), 实现完全自动化。

2 如何才避免2-factor authentication (2FA)验证呢?

苹果开发平台上有App Store Connect API这种对外开放的api,可以通过 借着开发api来避免这种问题。所以为了调用这个API`, 我们需要在开发者平台上生成一个密钥,才能通过这个密钥来调用这个API**.

3 获取App Store Connect API密钥和相关信息

App Store Connect API, 进行生成. 然后把文件下载下来。

4 修改相关配置

4.1 创建fastlane/.env

.env文件是用于保存一些在fastlane启动时就默认加载的环境变量。由于密钥等配置在开发过程中不能被泄漏,所以采用.env文件形式来保存这些信息, 且会被git所忽略,来保证安全。

fastlane/.env
# app store connect api key. https://appstoreconnect.apple.com/access/api
APP_STORE_CONNECT_API_KEY_CONTENT="-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----"

# Identifies the issuer who created the authentication token. https://appstoreconnect.apple.com/access/api
APP_STORE_CONNECT_API_ISSUER_ID="..."

# The key of app store connect api. https://appstoreconnect.apple.com/access/api
APP_STORE_CONNECT_API_KEY_ID="..."

4.2 修改fastlane/Fastfile

fastlane/fastfile
diff --git a/fastlane/Fastfile b/fastlane/Fastfile
index ab44af6..a9e5aab 100644
--- a/fastlane/Fastfile
+++ b/fastlane/Fastfile
@@ -15,11 +15,17 @@

default_platform(:ios)

+# Load the App Store Connect API token to use in other fastlane tools and actions. https://docs.fastlane.tools/actions/app_store_connect_api_key/
+api_key = app_store_connect_api_key(
+ key_id: ENV["APP_STORE_CONNECT_API_KEY_ID"],
+ issuer_id: ENV["APP_STORE_CONNECT_API_ISSUER_ID"],
+ key_content: ENV["APP_STORE_CONNECT_API_KEY_CONTENT"]
+)

desc "Download certificates from the git"
lane :syncCertificates do
- match(type: "appstore")
- match(type: "development")
+ match(api_key: api_key, type: "appstore", readonly: true)
+ match(api_key: api_key, type: "development", readonly: true)
end

platform :ios do
..

5 删除本地证书并执行bata流程看看行不行

5.1 如何删除本地证书?

5.1 执行beta流程

$ fastlane syncCertificates # 同步下证书
$ fastlane beta # 执行流程
[] 🚀
[13:29:08]: fastlane detected a Gemfile in the current directory
[13:29:08]: However, it seems like you didn't use `bundle exec`
[13:29:08]: To launch fastlane faster, please use
[13:29:08]:
[13:29:08]: $ bundle exec fastlane beta
[13:29:08]:
...
[13:32:00]: fastlane.tools finished successfully 🎉

结果是成功的,也没有出现要2次验证的情况

6 本次改动的代码

本次修改的文件
diff --git a/.gitignore b/.gitignore
index 55a0685..10ed8c5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,5 @@
*.zip
*.ipa
+.idea
+fastlane/.env
+tmp
diff --git a/fastlane/Fastfile b/fastlane/Fastfile
index ab44af6..a9e5aab 100644
--- a/fastlane/Fastfile
+++ b/fastlane/Fastfile
@@ -15,11 +15,17 @@

default_platform(:ios)

+# Load the App Store Connect API token to use in other fastlane tools and actions. https://docs.fastlane.tools/actions/app_store_connect_api_key/
+api_key = app_store_connect_api_key(
+ key_id: ENV["APP_STORE_CONNECT_API_KEY_ID"],
+ issuer_id: ENV["APP_STORE_CONNECT_API_ISSUER_ID"],
+ key_content: ENV["APP_STORE_CONNECT_API_KEY_CONTENT"]
+)

desc "Download certificates from the git"
lane :syncCertificates do
- match(type: "appstore")
- match(type: "development")
+ match(api_key: api_key, type: "appstore", readonly: true)
+ match(api_key: api_key, type: "development", readonly: true)
end

platform :ios do
diff --git a/fastlane/Matchfile b/fastlane/Matchfile
index c454c5d..05986fc 100644
--- a/fastlane/Matchfile
+++ b/fastlane/Matchfile
@@ -4,8 +4,8 @@ storage_mode("git")

type("development") # The default type, can be: appstore, adhoc, enterprise or development

-# app_identifier(["tools.fastlane.app", "tools.fastlane.app2"])
-# username("user@fastlane.tools") # Your Apple Developer Portal username
+app_identifier(["com.wuchuheng"])
+username("root@wuchuheng.com") # Your Apple Developer Portal username

# For all available options run `fastlane match --help`
# Remove the # in the beginning of the line to enable the other options
$ git add -A   
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: .gitignore
new file: fastlane/.env.example
modified: fastlane/Fastfile
modified: fastlane/Matchfile
modified: fastlane/report.xml
modified: wuchuheng.xcodeproj/project.pbxproj

$ git commit -m 'feat: configure app-store-connect-api'
[master f6c0f8a] feat: configure app-store-connect-api
6 files changed, 40 insertions(+), 13 deletions(-)
create mode 100644 fastlane/.env.example

7 总结

本文的目标是解决之前在同步证书时遇到的2次验证所导致的不能全自动化实现流程。为此了解决这个问题,通过了app store connect api的对外 接口来解决这个问题。 那么解决这一问题后,将对后面的CI/CD的自动化起到了铺平道路的作用。

8 源代码

Source

8 参考资料