build_LoopFollow.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. name: 4. Build LoopFollow
  2. run-name: Build LoopFollow (${{ github.ref_name }})
  3. on:
  4. workflow_dispatch:
  5. schedule:
  6. # Check for updates every Sunday
  7. # Later logic builds if there are updates or if it is the 2nd Sunday of the month
  8. - cron: "17 10 * * 0" # Sunday at UTC 10:17
  9. env:
  10. GH_PAT: ${{ secrets.GH_PAT }}
  11. UPSTREAM_REPO: loopandlearn/LoopFollow
  12. UPSTREAM_BRANCH: ${{ github.ref_name }} # branch on upstream repository to sync from (replace with specific branch name if needed)
  13. TARGET_BRANCH: ${{ github.ref_name }} # target branch on fork to be kept in sync
  14. jobs:
  15. # use a single runner for these sequential steps
  16. check_status:
  17. runs-on: ubuntu-latest
  18. name: Check status to decide whether to build
  19. permissions:
  20. contents: write
  21. outputs:
  22. NEW_COMMITS: ${{ steps.sync.outputs.has_new_commits }}
  23. IS_SECOND_IN_MONTH: ${{ steps.date-check.outputs.is_second_instance }}
  24. # Check GH_PAT, sync repository, check day in month
  25. steps:
  26. - name: Access
  27. id: workflow-permission
  28. run: |
  29. # Validate Access Token
  30. # Ensure that gh exit codes are handled when output is piped.
  31. set -o pipefail
  32. # Define patterns to validate the access token (GH_PAT) and distinguish between classic and fine-grained tokens.
  33. GH_PAT_CLASSIC_PATTERN='^ghp_[a-zA-Z0-9]{36}$'
  34. GH_PAT_FINE_GRAINED_PATTERN='^github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59}$'
  35. # Validate Access Token (GH_PAT)
  36. if [ -z "$GH_PAT" ]; then
  37. failed=true
  38. echo "::error::The GH_PAT secret is unset or empty. Set it and try again."
  39. else
  40. if [[ $GH_PAT =~ $GH_PAT_CLASSIC_PATTERN ]]; then
  41. provides_scopes=true
  42. echo "The GH_PAT secret is a structurally valid classic token."
  43. elif [[ $GH_PAT =~ $GH_PAT_FINE_GRAINED_PATTERN ]]; then
  44. echo "The GH_PAT secret is a structurally valid fine-grained token."
  45. else
  46. unknown_format=true
  47. echo "The GH_PAT secret does not have a known token format."
  48. fi
  49. # Attempt to capture the x-oauth-scopes scopes of the token.
  50. if ! scopes=$(curl -sS -f -I -H "Authorization: token $GH_PAT" https://api.github.com | { grep -i '^x-oauth-scopes:' || true; } | cut -d ' ' -f2- | tr -d '\r'); then
  51. failed=true
  52. if [ $unknown_format ]; then
  53. echo "::error::Unable to connect to GitHub using the GH_PAT secret. Verify that it is set correctly (including the 'ghp_' or 'github_pat_' prefix) and try again."
  54. else
  55. echo "::error::Unable to connect to GitHub using the GH_PAT secret. Verify that the token exists and has not expired at https://github.com/settings/tokens. If necessary, regenerate or create a new token (and update the secret), then try again."
  56. fi
  57. elif [[ $scopes =~ workflow ]]; then
  58. echo "The GH_PAT secret has repo and workflow permissions."
  59. echo "has_permission=true" >> $GITHUB_OUTPUT
  60. elif [[ $scopes =~ repo ]]; then
  61. echo "The GH_PAT secret has repo (but not workflow) permissions."
  62. elif [ $provides_scopes ]; then
  63. failed=true
  64. if [ -z "$scopes" ]; then
  65. echo "The GH_PAT secret is valid and can be used to connect to GitHub, but it does not provide any permission scopes."
  66. else
  67. echo "The GH_PAT secret is valid and can be used to connect to GitHub, but it only provides the following permission scopes: $scopes"
  68. fi
  69. echo "::error::The GH_PAT secret is lacking at least the 'repo' permission scope required to access the Match-Secrets repository. Update the token permissions at https://github.com/settings/tokens (to include the 'repo' and 'workflow' scopes) and try again."
  70. else
  71. echo "The GH_PAT secret is valid and can be used to connect to GitHub, but it does not provide inspectable scopes. Assuming that the 'repo' and 'workflow' permission scopes required to access the Match-Secrets repository and perform automations are present."
  72. echo "has_permission=true" >> $GITHUB_OUTPUT
  73. fi
  74. fi
  75. # Exit unsuccessfully if secret validation failed.
  76. if [ $failed ]; then
  77. exit 2
  78. fi
  79. - name: Checkout target repo
  80. if: |
  81. steps.workflow-permission.outputs.has_permission == 'true' &&
  82. (vars.SCHEDULED_BUILD != 'false' || vars.SCHEDULED_SYNC != 'false')
  83. uses: actions/checkout@v5
  84. with:
  85. token: ${{ secrets.GH_PAT }}
  86. # This syncs any target branch to upstream branch of the same name
  87. - name: Sync upstream changes
  88. if: | # do not run the upstream sync action on the upstream repository
  89. steps.workflow-permission.outputs.has_permission == 'true' &&
  90. vars.SCHEDULED_SYNC != 'false' && github.repository_owner != 'loopandlearn'
  91. id: sync
  92. uses: aormsby/Fork-Sync-With-Upstream-action@v3.4.2
  93. with:
  94. target_sync_branch: ${{ env.TARGET_BRANCH }}
  95. shallow_since: 6 months ago
  96. target_repo_token: ${{ secrets.GH_PAT }}
  97. upstream_sync_branch: ${{ env.UPSTREAM_BRANCH }}
  98. upstream_sync_repo: ${{ env.UPSTREAM_REPO }}
  99. # Display a sample message based on the sync output var 'has_new_commits'
  100. - name: New commits found
  101. if: |
  102. steps.workflow-permission.outputs.has_permission == 'true' &&
  103. vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'true'
  104. run: echo "New commits were found to sync."
  105. - name: No new commits
  106. if: |
  107. steps.workflow-permission.outputs.has_permission == 'true' &&
  108. vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'false'
  109. run: echo "There were no new commits."
  110. - name: Show value of 'has_new_commits'
  111. if: steps.workflow-permission.outputs.has_permission == 'true' && vars.SCHEDULED_SYNC != 'false'
  112. run: |
  113. echo ${{ steps.sync.outputs.has_new_commits }}
  114. echo "NEW_COMMITS=${{ steps.sync.outputs.has_new_commits }}" >> $GITHUB_OUTPUT
  115. - name: Show scheduled build configuration message
  116. if: steps.workflow-permission.outputs.has_permission != 'true'
  117. run: |
  118. echo "### :calendar: Scheduled Sync and Build Disabled :mobile_phone_off:" >> $GITHUB_STEP_SUMMARY
  119. echo "You have not yet configured the scheduled sync and build for LoopFollow's browser build." >> $GITHUB_STEP_SUMMARY
  120. echo "Synchronizing your fork of <code>LoopFollow</code> with the upstream repository <code>loopandlearn/LoopFollow</code> will be skipped." >> $GITHUB_STEP_SUMMARY
  121. echo "If you want to enable automatic builds and updates for your LoopFollow, please follow the instructions \
  122. under the following path <code>LoopFollow/fastlane/testflight.md</code>." >> $GITHUB_STEP_SUMMARY
  123. # Set a logic flag if this is the second instance of this day-of-week in this month
  124. - name: Check if this is the second time this day-of-week happens this month
  125. id: date-check
  126. run: |
  127. DAY_OF_MONTH=$(date +%-d)
  128. WEEK_OF_MONTH=$(( ($(date +%-d) - 1) / 7 + 1 ))
  129. if [[ $WEEK_OF_MONTH -eq 2 ]]; then
  130. echo "is_second_instance=true" >> "$GITHUB_OUTPUT"
  131. else
  132. echo "is_second_instance=false" >> "$GITHUB_OUTPUT"
  133. fi
  134. # Checks if Distribution certificate is present and valid, optionally nukes and
  135. # creates new certs if the repository variable ENABLE_NUKE_CERTS == 'true'
  136. # only run if a build is planned
  137. check_certs:
  138. needs: [check_status]
  139. name: Check certificates
  140. uses: ./.github/workflows/create_certs.yml
  141. secrets: inherit
  142. if: |
  143. github.event_name == 'workflow_dispatch' ||
  144. (vars.SCHEDULED_BUILD != 'false' && needs.check_status.outputs.IS_SECOND_IN_MONTH == 'true') ||
  145. (vars.SCHEDULED_SYNC != 'false' && needs.check_status.outputs.NEW_COMMITS == 'true' )
  146. # Builds LoopFollow
  147. build:
  148. name: Build
  149. needs: [check_certs, check_status]
  150. runs-on: macos-26
  151. permissions:
  152. contents: write
  153. if:
  154. | # builds with manual start; if scheduled: once a month or when new commits are found
  155. github.event_name == 'workflow_dispatch' ||
  156. (vars.SCHEDULED_BUILD != 'false' && needs.check_status.outputs.IS_SECOND_IN_MONTH == 'true') ||
  157. (vars.SCHEDULED_SYNC != 'false' && needs.check_status.outputs.NEW_COMMITS == 'true' )
  158. steps:
  159. - name: Select Xcode version
  160. run: "sudo xcode-select --switch /Applications/Xcode_26.2.app/Contents/Developer"
  161. - name: Checkout Repo for building
  162. uses: actions/checkout@v5
  163. with:
  164. token: ${{ secrets.GH_PAT }}
  165. submodules: recursive
  166. ref: ${{ env.TARGET_BRANCH }}
  167. # Patch Fastlane Match to not print tables
  168. - name: Patch Match Tables
  169. run: |
  170. TABLE_PRINTER_PATH=$(ruby -e 'puts Gem::Specification.find_by_name("fastlane").gem_dir')/match/lib/match/table_printer.rb
  171. if [ -f "$TABLE_PRINTER_PATH" ]; then
  172. sed -i "" "/puts(Terminal::Table.new(params))/d" "$TABLE_PRINTER_PATH"
  173. else
  174. echo "table_printer.rb not found"
  175. exit 1
  176. fi
  177. # Install project dependencies
  178. - name: Install Project Dependencies
  179. run: bundle install
  180. # Sync the GitHub runner clock with the Windows time server (workaround as suggested in https://github.com/actions/runner/issues/2996)
  181. - name: Sync clock
  182. run: sudo sntp -sS time.windows.com
  183. # Build signed LoopFollow IPA file
  184. - name: Fastlane Build & Archive
  185. run: bundle exec fastlane build_LoopFollow
  186. env:
  187. TEAMID: ${{ secrets.TEAMID }}
  188. GH_PAT: ${{ secrets.GH_PAT }}
  189. FASTLANE_KEY_ID: ${{ secrets.FASTLANE_KEY_ID }}
  190. FASTLANE_ISSUER_ID: ${{ secrets.FASTLANE_ISSUER_ID }}
  191. FASTLANE_KEY: ${{ secrets.FASTLANE_KEY }}
  192. MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
  193. # Upload to TestFlight
  194. - name: Fastlane upload to TestFlight
  195. run: bundle exec fastlane release
  196. env:
  197. TEAMID: ${{ secrets.TEAMID }}
  198. GH_PAT: ${{ secrets.GH_PAT }}
  199. FASTLANE_KEY_ID: ${{ secrets.FASTLANE_KEY_ID }}
  200. FASTLANE_ISSUER_ID: ${{ secrets.FASTLANE_ISSUER_ID }}
  201. FASTLANE_KEY: ${{ secrets.FASTLANE_KEY }}
  202. MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
  203. # Upload Build artifacts
  204. - name: Upload build log, IPA and Symbol artifacts
  205. if: always()
  206. uses: actions/upload-artifact@v6
  207. with:
  208. name: build-artifacts
  209. path: |
  210. artifacts
  211. buildlog