From 48e388f16d6367adef06de5ceb5790e39f0a4cf0 Mon Sep 17 00:00:00 2001 From: MarkG Date: Wed, 13 Mar 2024 13:54:03 +0700 Subject: [PATCH] [#546] Support adding new devices directly from Github Action --- .../project_workflows/add_device_profile.yml | 67 +++++++++++++++++++ fastlane/Constants/Constant.swift | 20 ++++-- fastlane/Constants/Secret.swift | 4 ++ fastlane/Fastfile.swift | 18 +++++ .../xcschemes/FastlaneRunner.xcscheme | 2 +- 5 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 .github/project_workflows/add_device_profile.yml diff --git a/.github/project_workflows/add_device_profile.yml b/.github/project_workflows/add_device_profile.yml new file mode 100644 index 00000000..6bbce033 --- /dev/null +++ b/.github/project_workflows/add_device_profile.yml @@ -0,0 +1,67 @@ +name: Test Add device and regenerate profiles with fastlane match + +# SECRETS needed: +### SSH_PRIVATE_KEY for the match Repo +### MATCH_PASSWORD +### APPSTORE_CONNECT_API_KEY +### API_KEY_ID +### ISSUER_ID + +on: + pull_request: + workflow_dispatch: + inputs: + devices: + description: 'JSON of devices. `{"Luka iPhone 6": "1234567890123456789012345678901234567890","Felix iPad Air 2": "abcdefghijklmnopqrstvuwxyzabcdefghijklmn"}`' + required: true + type: string + platform: + description: 'The platform' + required: true + default: 'ios' + type: choice + options: + - ios + - mac + +jobs: + create_files: + name: Create certificates and profiles + runs-on: macOS-latest + steps: + - name: Checkout the repository + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Install SSH key + uses: webfactory/ssh-agent@v0.5.4 + with: + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} + + - name: Cache Pods + uses: actions/cache@v3 + id: cocoapodCache + with: + path: Pods + key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }} + restore-keys: | + ${{ runner.os }}-pods- + + - name: Bundle install + run: bundle install + + - name: Add device and regenerate profiles with match + run: bundle exec fastlane addDevicesGenerateProfiles + env: + MATCH_PASSWORD: ${{ secrets.MATCH_PASS }} + DEVICES: ${{ inputs.devices }} + APPSTORE_CONNECT_API_KEY: ${{ secrets.APPSTORE_CONNECT_API_KEY }} + API_KEY_ID: ${{ secrets.API_KEY_ID }} + ISSUER_ID: ${{ secrets.ISSUER_ID }} + PLATFORM: ${{ inputs.platform }} + + - name: Clean up keychain + if: ${{ always() }} + run: bundle exec fastlane remove_keychain + continue-on-error: true diff --git a/fastlane/Constants/Constant.swift b/fastlane/Constants/Constant.swift index 110dde40..83c64f58 100644 --- a/fastlane/Constants/Constant.swift +++ b/fastlane/Constants/Constant.swift @@ -28,12 +28,20 @@ enum Constant { static let appleProductionTeamId = "<#teamId#>" static let keychainName = "{PROJECT_NAME}_keychain" static let matchURL = "git@github.com:{organization}/{repo}.git" - static let apiKey: [String: Any] = [ - "key_id" : Secret.appStoreKeyIdKey, - "issuer_id": Secret.appStoreIssuerIdKey, - "key": Secret.appstoreConnectAPIKey, - "in_house": false - ] + static let apiKey: [String: Any] = { + var key = Secret.appstoreConnectAPIKey + if let data = Data(base64Encoded: Secret.appstoreConnectAPIKey), + let decodedKey = String(data: data, encoding: .utf8) { + key = decodedKey + } + + return [ + "key_id" : Secret.appStoreKeyIdKey, + "issuer_id": Secret.appStoreIssuerIdKey, + "key": key, + "in_house": false + ] + }() // MARK: - Path diff --git a/fastlane/Constants/Secret.swift b/fastlane/Constants/Secret.swift index 12f37911..6bc1ed25 100644 --- a/fastlane/Constants/Secret.swift +++ b/fastlane/Constants/Secret.swift @@ -18,4 +18,8 @@ enum Secret { static let appStoreIssuerIdKey = EnvironmentParser.string(key: "ISSUER_ID") static let bumpAppStoreBuildNumber = EnvironmentParser.bool(key: "BUMP_APP_STORE_BUILD_NUMBER") + + static let devices = EnvironmentParser.string(key: "DEVICES") + + static let platform = EnvironmentParser.string(key: "PLATFORM") } diff --git a/fastlane/Fastfile.swift b/fastlane/Fastfile.swift index 5052b7ad..d8e1f09d 100644 --- a/fastlane/Fastfile.swift +++ b/fastlane/Fastfile.swift @@ -176,6 +176,24 @@ class Fastfile: LaneFile { Match.syncCodeSigning(type: .adHoc, environment: .staging, isForce: true) } + func addDevicesGenerateProfilesLane() { + desc("Add device and regenerate profiles with match") + + guard let data = Secret.devices.data(using: .utf8), + let devices = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { + return + } + registerDevices( + devices: .userDefined(devices), + apiKey: .userDefined(Constant.apiKey), + teamId: .userDefined(Constant.appleStagingTeamId), + platform: Secret.platform + ) + + Match.syncCodeSigning(type: .development, environment: .staging, isForce: true) + Match.syncCodeSigning(type: .adHoc, environment: .staging, isForce: true) + } + // MARK: - Utilities func cleanUpOutputLane() { diff --git a/fastlane/swift/FastlaneSwiftRunner/FastlaneSwiftRunner.xcodeproj/xcshareddata/xcschemes/FastlaneRunner.xcscheme b/fastlane/swift/FastlaneSwiftRunner/FastlaneSwiftRunner.xcodeproj/xcshareddata/xcschemes/FastlaneRunner.xcscheme index 94572138..5c4ae769 100644 --- a/fastlane/swift/FastlaneSwiftRunner/FastlaneSwiftRunner.xcodeproj/xcshareddata/xcschemes/FastlaneRunner.xcscheme +++ b/fastlane/swift/FastlaneSwiftRunner/FastlaneSwiftRunner.xcodeproj/xcshareddata/xcschemes/FastlaneRunner.xcscheme @@ -61,7 +61,7 @@