Skip to content

Commit 484d3b4

Browse files
authored
Scheduled build improvements (#71)
* Add conditional scheduled build and sync * Update testflight.md with instructions for scheduling setup * Fix typo * Remove GITHUB_TOKEN; use GH_PAT instead * Update testflight.md with instructions how to add workflow scope * Fixed conditions for scheduled build * Fix upstream repo owner * Refactor build to use workflow permissions and auto-create alive branch * Change GITHUB_TOKEN to GH_PAT * Change token to GITHUB_TOKEN where appropriate; Make env variable names more descriptive * Fix broken alive branch auto-creation * Update testflight.md with opt-out and new config info * Update cron for sync and schedule, update build condition * Fix typo… * Update testflight.md with suggestions and re-organized contents * Fix typo from PR74
1 parent 024b37c commit 484d3b4

File tree

2 files changed

+224
-33
lines changed

2 files changed

+224
-33
lines changed

.github/workflows/build_loop.yml

Lines changed: 125 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,105 @@ on:
77
#push:
88

99
schedule:
10-
- cron: '0 04 * * *' # Checks for updates at 04:00 UTC every day
11-
- cron: '0 04 1 * *' # Builds the app on the 1th every month
10+
- cron: '0 8 * * 3' # Checks for updates at 08:00 am UTC every Wednesday
11+
- cron: '0 8 1 * 6' # Builds the app on the 1st Saturday every month at 08:00 am UTC
1212

1313
env:
1414
UPSTREAM_REPO: LoopKit/LoopWorkspace
15-
UPSTREAM_BRANCH: ${{ github.ref_name }} # branch on upstream repository to sync from (relpace with specific branch name if needed)
16-
TARGET_BRANCH: ${{ github.ref_name }} # target branch on fork to be kept in sync, and target branch on upstream to be kept alive (relpace with specific branch name if needed)
15+
UPSTREAM_BRANCH: ${{ github.ref_name }} # branch on upstream repository to sync from (replace with specific branch name if needed)
16+
TARGET_BRANCH: ${{ github.ref_name }} # target branch on fork to be kept in sync, and target branch on upstream to be kept alive (replace with specific branch name if needed)
1717
ALIVE_BRANCH: alive
18-
SYNC_UPSTREAM: ${{ vars.SYNC_UPSTREAM }} # set an optional "SYNC_UPSTREAM" repository variable to 'false' to disable syncing of fork with the upstream repository
18+
WORKFLOW_PERMISSIONS: false
1919

2020
jobs:
21+
secrets:
22+
uses: ./.github/workflows/validate_secrets.yml
23+
secrets: inherit
24+
25+
# Checks if GH_PAT holds workflow permissions
26+
# Checks for existence of alive branch; if non-existent creates it
27+
check_alive_and_permissions:
28+
needs: secrets
29+
runs-on: ubuntu-latest
30+
name: Check alive branch and permissions
31+
permissions:
32+
contents: write
33+
outputs:
34+
WORKFLOW_PERMISSION: ${{ steps.workflow-permission.outputs.has_permission }}
35+
36+
steps:
37+
- name: Check for workflow permissions
38+
id: workflow-permission
39+
env:
40+
TOKEN_TO_CHECK: ${{ secrets.GH_PAT }}
41+
run: |
42+
PERMISSIONS=$(curl -sS -f -I -H "Authorization: token ${{ env.TOKEN_TO_CHECK }}" https://api.github.com | grep ^x-oauth-scopes: | cut -d' ' -f2-);
43+
44+
if [[ $PERMISSIONS =~ "workflow" || $PERMISSIONS == "" ]]; then
45+
echo "GH_PAT holds workflow permissions or is fine-grained PAT."
46+
echo "has_permission=true" >> $GITHUB_OUTPUT # Set WORKFLOW_PERMISSION to false.
47+
else
48+
echo "GH_PAT lacks workflow permissions."
49+
echo "Automated build features will be skipped!"
50+
echo "has_permission=false" >> $GITHUB_OUTPUT # Set WORKFLOW_PERMISSION to false.
51+
fi
52+
53+
- name: Check for alive branch
54+
if: steps.workflow-permission.outputs.has_permission == 'true'
55+
env:
56+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57+
run: |
58+
if [[ "$(gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository_owner }}/LoopWorkspace/branches | jq --raw-output 'any(.name=="alive")')" == "true" ]]; then
59+
echo "Branch 'alive' exists."
60+
echo "ALIVE_BRANCH_EXISTS=true" >> $GITHUB_ENV # Set ALIVE_BRANCH_EXISTS to true
61+
else
62+
echo "Branch 'alive' does not exist."
63+
echo "ALIVE_BRANCH_EXISTS=false" >> $GITHUB_ENV # Set ALIVE_BRANCH_EXISTS to false
64+
fi
65+
66+
- name: Create alive branch
67+
if: env.ALIVE_BRANCH_EXISTS != 'true'
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
run: |
71+
# Get ref for LoopKit/LoopWorkspace:dev
72+
SHA=$(curl -sS https://api.github.com/repos/${{ env.UPSTREAM_REPO }}/git/refs \
73+
| jq '.[] | select(.ref == "refs/heads/dev" ) | .object.sha' \
74+
| tr -d '"'
75+
);
76+
77+
# Create alive branch based on LoopKit/LoopWorkspace:dev
78+
gh api \
79+
--method POST \
80+
-H "Authorization: token $GITHUB_TOKEN" \
81+
-H "Accept: application/vnd.github.v3+json" \
82+
/repos/${{ github.repository_owner }}/LoopWorkspace/git/refs \
83+
-f ref='refs/heads/alive' \
84+
-f sha=$SHA
85+
86+
# Checks for changes in upstream repository; if changes exist prompts sync for build
87+
# Performs keepalive to avoid stale fork
2188
check_latest_from_upstream:
89+
needs: check_alive_and_permissions
2290
runs-on: ubuntu-latest
2391
name: Check upstream and keep alive
2492
outputs:
2593
NEW_COMMITS: ${{ steps.sync.outputs.has_new_commits }}
26-
94+
2795
steps:
2896
- name: Checkout target repo
97+
if: |
98+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
99+
(vars.SCHEDULED_BUILD != 'false' || vars.SCHEDULED_SYNC != 'false')
29100
uses: actions/checkout@v3
30101
with:
31102
token: ${{ secrets.GH_PAT }}
32103
ref: alive
33104

34105
- name: Sync upstream changes
35-
if: ${{ env.SYNC_UPSTREAM != 'false' && github.repository_owner != 'LoopKit' }} # do not run the upstream sync action on the upstream repository
106+
if: | # do not run the upstream sync action on the upstream repository
107+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
108+
vars.SCHEDULED_SYNC != 'false' && github.repository_owner != 'LoopKit'
36109
id: sync
37110
uses: aormsby/Fork-Sync-With-Upstream-action@v3.4
38111
with:
@@ -44,41 +117,72 @@ jobs:
44117

45118
# Display a sample message based on the sync output var 'has_new_commits'
46119
- name: New commits found
47-
if: steps.sync.outputs.has_new_commits == 'true'
120+
if: |
121+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
122+
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'true'
48123
run: echo "New commits were found to sync."
49124

50125
- name: No new commits
51-
if: steps.sync.outputs.has_new_commits == 'false'
126+
if: |
127+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
128+
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'false'
52129
run: echo "There were no new commits."
53130

54131
- name: Show value of 'has_new_commits'
132+
if: needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' && vars.SCHEDULED_SYNC != 'false'
55133
run: |
56134
echo ${{ steps.sync.outputs.has_new_commits }}
57135
echo "NEW_COMMITS=${{ steps.sync.outputs.has_new_commits }}" >> $GITHUB_OUTPUT
58-
136+
59137
# Keep repository "alive": add empty commits to ALIVE_BRANCH after "time_elapsed" days of inactivity to avoid inactivation of scheduled workflows
60138
- name: Keep alive
139+
if: |
140+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
141+
(vars.SCHEDULED_BUILD != 'false' || vars.SCHEDULED_SYNC != 'false')
61142
uses: gautamkrishnar/keepalive-workflow@v1 # using the workflow with default settings
62143
with:
63144
time_elapsed: 20 # Time elapsed from the previous commit to trigger a new automated commit (in days)
145+
146+
- name: Show scheduled build configuration message
147+
if: needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION != 'true'
148+
run: |
149+
echo "### :calendar: Scheduled Sync and Build Disabled :mobile_phone_off:" >> $GITHUB_STEP_SUMMARY
150+
echo "You have not yet configured the scheduled sync and build for Loop's browser build." >> $GITHUB_STEP_SUMMARY
151+
echo "Synchronizing your fork of <code>LoopWorkspace</code> with the upstream repository <code>LoopKit/LoopWorkspace</code> will be skipped." >> $GITHUB_STEP_SUMMARY
152+
echo "If you want to enable automatic builds and updates for your Loop, please follow the instructions \
153+
under the following path <code>LoopWorkspace/fastlane/testflight.md</code>." >> $GITHUB_STEP_SUMMARY
154+
64155

156+
# Builds Loop
65157
build:
66158
name: Build
67159
needs: check_latest_from_upstream
68160
runs-on: macos-13
69-
if: ${{ github.event_name == 'workflow_dispatch' || github.event.schedule == '0 04 1 * *' || needs.check_latest_from_upstream.outputs.NEW_COMMITS == 'true' }} # runs if started manually, or if scheduled on the first each month, or if new commits were found
161+
permissions:
162+
contents: write
163+
if: | # runs if started manually, or if sync schedule is set and enabled and scheduled on the first Saturday each month, or if sync schedule is set and enabled and new commits were found
164+
github.event_name == 'workflow_dispatch' ||
165+
(needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
166+
(vars.SCHEDULED_BUILD != 'false' && github.event.schedule == '0 8 1 * 6') ||
167+
(vars.SCHEDULED_SYNC != 'false' && needs.check_latest_from_upstream.outputs.NEW_COMMITS == 'true' )
168+
)
70169
steps:
71170
- name: Select Xcode version
72171
run: "sudo xcode-select --switch /Applications/Xcode_14.3.1.app/Contents/Developer"
73172

74173
- name: Checkout Repo for syncing
174+
if: |
175+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
176+
vars.SCHEDULED_SYNC != 'false'
75177
uses: actions/checkout@v3
76178
with:
77179
token: ${{ secrets.GH_PAT }}
78180
ref: ${{ env.TARGET_BRANCH }}
79181

80182
- name: Sync upstream changes
81-
if: ${{ env.SYNC_UPSTREAM != 'false' && github.repository_owner != 'LoopKit' }} # do not run the upstream sync action on the upstream repository
183+
if: | # do not run the upstream sync action on the upstream repository
184+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
185+
vars.SCHEDULED_SYNC != 'false' && github.repository_owner != 'LoopKit'
82186
id: sync
83187
uses: aormsby/Fork-Sync-With-Upstream-action@v3.4
84188
with:
@@ -90,14 +194,21 @@ jobs:
90194

91195
# Display a sample message based on the sync output var 'has_new_commits'
92196
- name: New commits found
93-
if: steps.sync.outputs.has_new_commits == 'true'
197+
if: |
198+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
199+
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'true'
94200
run: echo "New commits were found to sync."
95201

96202
- name: No new commits
97-
if: steps.sync.outputs.has_new_commits == 'false'
203+
if: |
204+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
205+
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'false'
98206
run: echo "There were no new commits."
99207

100208
- name: Show value of 'has_new_commits'
209+
if: |
210+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true'
211+
&& vars.SCHEDULED_SYNC != 'false'
101212
run: |
102213
echo ${{ steps.sync.outputs.has_new_commits }}
103214
echo "NEW_COMMITS=${{ steps.sync.outputs.has_new_commits }}" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)