Skip to content

Commit 319b611

Browse files
authored
Validate repository secrets (#36)
* Validate repository secrets Adds support for validation of repository secrets. * Validate $FASTLANE_KEY as unencrypted PKCS#8 * Number workflows to guide sequential exection
1 parent 7b68d4b commit 319b611

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed

.github/workflows/add_identifiers.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
name: Add Identifiers
1+
name: 2. Add Identifiers
2+
run-name: Add Identifiers
23
on:
34
workflow_dispatch:
45

56
jobs:
7+
secrets:
8+
uses: ./.github/workflows/validate_secrets.yml
9+
secrets: inherit
10+
611
identifiers:
12+
needs: secrets
713
runs-on: macos-12
814
steps:
915
# Uncomment to manually select latest Xcode if needed

.github/workflows/build_loop.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
name: Build Loop
1+
name: 4. Build Loop
2+
run-name: Build Loop
23
on:
34
workflow_dispatch:
45

56
jobs:
7+
secrets:
8+
uses: ./.github/workflows/validate_secrets.yml
9+
secrets: inherit
10+
611
build:
12+
needs: secrets
713
runs-on: macos-12
814
steps:
915
# Uncomment to manually select latest Xcode if needed

.github/workflows/create_certs.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
name: Create Certificates
1+
name: 3. Create Certificates
2+
run-name: Create Certificates
23
on:
34
workflow_dispatch:
45

56
jobs:
7+
secrets:
8+
uses: ./.github/workflows/validate_secrets.yml
9+
secrets: inherit
10+
611
certificates:
12+
needs: secrets
713
runs-on: macos-12
814
steps:
915
# Uncomment to manually select latest Xcode if needed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: 1. Validate Secrets
2+
run-name: Validate Secrets
3+
on: [workflow_call, workflow_dispatch]
4+
5+
jobs:
6+
validate:
7+
runs-on: macos-12
8+
steps:
9+
# Checks-out the repo
10+
- name: Checkout Repo
11+
uses: actions/checkout@v3
12+
13+
# Validates the repo secrets
14+
- name: Validate Secrets
15+
run: |
16+
# Validate Secrets
17+
echo Validating Repository Secrets...
18+
19+
# Validate TEAMID
20+
if [ -z "$TEAMID" ]; then
21+
failed=true
22+
echo "::error::TEAMID secret is unset or empty. Set it and try again."
23+
elif [ ${#TEAMID} -ne 10 ]; then
24+
failed=true
25+
echo "::error::TEAMID secret is set but has wrong length. Verify that it is set correctly and try again."
26+
fi
27+
28+
# Validate GH_PAT
29+
if [ -z "$GH_PAT" ]; then
30+
failed=true
31+
echo "::error::GH_PAT secret is unset or empty. Set it and try again."
32+
elif [ "$(gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository_owner }}/Match-Secrets | jq --raw-output '.permissions.push')" != "true" ]; then
33+
failed=true
34+
echo "::error::GH_PAT secret is set but invalid or lacking appropriate privileges on the ${{ github.repository_owner }}/Match-Secrets repository. Verify that it is set correctly and try again."
35+
fi
36+
37+
# Validate FASTLANE_ISSUER_ID, FASTLANE_KEY_ID, and FASTLANE_KEY
38+
if [ -z "$FASTLANE_ISSUER_ID" ] || [ -z "$FASTLANE_KEY_ID" ] || [ -z "$FASTLANE_KEY" ]; then
39+
failed=true
40+
[ -z "$FASTLANE_ISSUER_ID" ] && echo "::error::The FASTLANE_ISSUER_ID secret is unset or empty. Set it and try again."
41+
[ -z "$FASTLANE_KEY_ID" ] && echo "::error::The FASTLANE_KEY_ID secret is unset or empty. Set it and try again."
42+
[ -z "$FASTLANE_KEY" ] && echo "::error::The FASTLANE_KEY secret is unset or empty. Set it and try again."
43+
elif ! echo "$FASTLANE_KEY" | openssl pkcs8 -nocrypt >/dev/null; then
44+
failed=true
45+
echo "::error::The FASTLANE_KEY secret is set but invalid. Verify that it is set correctly and try again."
46+
elif ! fastlane validate_secrets; then
47+
failed=true
48+
echo "::error::Unable to create a valid authorization token for the App Store Connect API.\
49+
Verify that the FASTLANE_ISSUER_ID, FASTLANE_KEY_ID, and FASTLANE_KEY secrets are set correctly and try again."
50+
fi
51+
52+
# Validate MATCH_PASSWORD
53+
if [ -z "$MATCH_PASSWORD" ]; then
54+
failed=true
55+
echo "::error::The MATCH_PASSWORD secret is unset or empty. Set it and try again."
56+
fi
57+
58+
# Exit unsuccessfully if secret validation failed.
59+
if [ $failed ]; then
60+
exit 2
61+
fi
62+
shell: bash
63+
env:
64+
TEAMID: ${{ secrets.TEAMID }}
65+
GH_PAT: ${{ secrets.GH_PAT }}
66+
FASTLANE_ISSUER_ID: ${{ secrets.FASTLANE_ISSUER_ID }}
67+
FASTLANE_KEY_ID: ${{ secrets.FASTLANE_KEY_ID }}
68+
FASTLANE_KEY: ${{ secrets.FASTLANE_KEY }}
69+
MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
70+
GH_TOKEN: ${{ secrets.GH_PAT }}

fastlane/Fastfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ platform :ios do
231231
)
232232
end
233233

234+
desc "Validate Secrets"
235+
lane :validate_secrets do
236+
setup_ci if ENV['CI']
237+
ENV["MATCH_READONLY"] = true.to_s
238+
239+
app_store_connect_api_key(
240+
key_id: "#{FASTLANE_KEY_ID}",
241+
issuer_id: "#{FASTLANE_ISSUER_ID}",
242+
key_content: "#{FASTLANE_KEY}"
243+
)
244+
245+
def find_bundle_id(identifier)
246+
bundle_id = Spaceship::ConnectAPI::BundleId.find(identifier)
247+
end
248+
249+
find_bundle_id("com.#{TEAMID}.loopkit.Loop")
250+
end
251+
234252
desc "Nuke Certs"
235253
lane :nuke_certs do
236254
setup_ci if ENV['CI']

0 commit comments

Comments
 (0)