自动将新设备添加到Apple Developer Portal(GitHub Action&Fastlane)
#github #cicd #ios #fastlane

为了在真实的iPhone设备上安装beta/分期构建,您必须首先注册它,这已不是什么秘密。您可以手动执行它,但是您需要特殊的管理特权。自动化是消除人为因素的不同策略。

该方法必须容易且实用。

  1. 您的存储库包含一个名为devices.txt的文件,该文件列出了允许的“ udids”。
  2. 在该列表中添加新的设备后,开发人员将提交代码审查的PR。
  3. CI/CD作业将在PR批准并合并到主要分支之后运行
  4. 就是这样。重建新的配置配置文件后,安装应用程序。

现在让我们回顾实施。我将以GitHub Actions和Fastlane为例。

首先,我们需要一个称为“ decection.txt”的文件

文件格式在下面显示(或者您可以查看Apple Sample):

<UDID>tabulation<Name>tabulation<Type: ios or mac>

示例:

Device ID   Device Name Device Platform
a02fc03df7e12c5fb1ec247e8a14e2c3e4b7d68d    My iPhoneX  ios
00006000-000C551C02D2801E   My MacBook  mac
00008120-000C3C262478C09E   My iPhone14 ios

â€warn:dev.to将^^^表转换为空格(当复制上面的示例时要小心)


现在让我们在您的Fastfile中创建一个新的车道2

我们的新车道将利用koude3koude4的动作。

platform :ios do
  desc 'Registers new devices to the Apple Dev Portal and update provisioning profiles'
  lane :register_new_device_id do
    ensure_env_vars(
      env_vars: [
        # a secret variable Git repo encryption password
        'MATCH_PASSWORD',
        # a secret variable used to clone repo of certs 
        # more: https://docs.fastlane.tools/actions/match/
        'MATCH_GIT_BASIC_AUTHORIZATION',
        # your App Store Connect API Key information
        # used by `register_devices` action to authenticate to App Store Connect
        # more: https://docs.fastlane.tools/app-store-connect-api/#using-fastlane-api-key-hash-option
        'APP_STORE_CONNECT_API_KEY_JSON',
      ]
    )

    apiFile = Tempfile.new('api_key')
    apiFile.write(ENV['APP_STORE_CONNECT_API_KEY_JSON'])
    apiFile.close

    # will register iOS/Mac devices with the Developer Portal so that you can include them in your provisioning profiles
    register_devices(
      api_key_path: apiFile.path,
      devices_file: "./devices.txt"
    )

    # Update provisioning profiles after `register_devices`
    match(
      type: "adhoc",
      readonly: false,
      force_for_new_devices: true
    )

    match(
      type: "appstore",
      readonly: false,
      force_for_new_devices: true
    )
  end
end

接下来,我们需要创建一个新的github操作工作流程

每当devices.txt文件在主中更改时,.github/workflows/register_device.yml工作流将执行作业。

name: Mobile Automation

on:
  push:
    branches:
      - master
    paths:
      - '**/devices.txt' 
    # When PR that changing a `devices.txt` file will be merged, this workflow will be triggered

    steps:
      - uses: actions/checkout@v3 # checks-out your repository

      - name: Install Ruby 2.x # Required for Fastlane
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '2.7.6' # set your version
          bundler-cache: true   # enable caching for bundler

      - name: Install Fastlane
        run: bundle install --jobs 4 --retry 3

      - name: Registers a new device to the Apple Dev Portal
        run: bundle exec fastlane ios register_new_device_id
        env:
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
          MATCH_GIT_BASIC_AUTHORIZATION: ${{ secrets.MATCH_GIT_BASIC_AUTHORIZATION }}
          APP_STORE_CONNECT_API_KEY_JSON: ${{ secrets.APP_STORE_CONNECT_API_KEY_JSON }}

就足够了。我这个示例我省略了“匹配”集成,但是在线上有几个指南来解释如何实施它。

如果您有任何疑问,请随时在评论部分中询问。

Muramur(C)