Skip to content

Commit c717748

Browse files
committed
Fix browser build sync and alive behavior
- Added logic to extend the alive check for the existence of either `alive-main` or `alive-dev` branches. - Resolved an issue where unexpected successes were occurring when failures were expected. - Implemented a check to determine the existence of the `alive-main` and `alive-dev` branches and create them if they do not exist. - Introduced a mechanism to identify the current branch being run (either `main` or `dev`). - Based on the current branch, the corresponding alive branch (`alive-main` or `alive-dev`) will be used to check for upstream changes. - Set a new variable `ABORT_SYNC` to `true` when the current branch is neither `dev` nor `main`. - The syncing attempt will proceed based on the `ABORT_SYNC` variable status. - Ensured proper branch synchronization to prevent build inconsistencies and failures - Addresses issue LoopKit/Loop#2192
1 parent a687588 commit c717748

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

.github/workflows/build_loop.yml

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ env:
1414
UPSTREAM_REPO: LoopKit/LoopWorkspace
1515
UPSTREAM_BRANCH: ${{ github.ref_name }} # branch on upstream repository to sync from (replace with specific branch name if needed)
1616
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)
17-
ALIVE_BRANCH: alive
17+
ALIVE_BRANCH_MAIN: alive-main
18+
ALIVE_BRANCH_DEV: alive-dev
1819

1920
jobs:
2021
validate:
@@ -50,38 +51,49 @@ jobs:
5051
echo "has_permission=false" >> $GITHUB_OUTPUT # Set WORKFLOW_PERMISSION to false.
5152
fi
5253
53-
- name: Check for alive branch
54+
- name: Check for alive branches
5455
if: steps.workflow-permission.outputs.has_permission == 'true'
5556
env:
5657
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
5758
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
59+
if [[ $(gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository_owner }}/LoopWorkspace/branches | jq --raw-output '[.[] | select(.name == "alive-main" or .name == "alive-dev")] | length > 0') == "true" ]]; then
60+
echo "Branches 'alive-main' or 'alive-dev' exist."
61+
echo "ALIVE_BRANCH_EXISTS=true" >> $GITHUB_ENV
6162
else
62-
echo "Branch 'alive' does not exist."
63-
echo "ALIVE_BRANCH_EXISTS=false" >> $GITHUB_ENV # Set ALIVE_BRANCH_EXISTS to false
63+
echo "Branches 'alive-main' and 'alive-dev' do not exist."
64+
echo "ALIVE_BRANCH_EXISTS=false" >> $GITHUB_ENV
6465
fi
66+
6567
66-
- name: Create alive branch
68+
- name: Create alive branches
6769
if: env.ALIVE_BRANCH_EXISTS == 'false'
6870
env:
6971
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
7072
run: |
73+
# Get ref for LoopKit/LoopWorkspace:main
74+
SHA_MAIN=$(curl -sS -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/${{ env.UPSTREAM_REPO }}/git/refs/heads/main | jq -r '.object.sha')
75+
7176
# 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-
);
77+
SHA_DEV=$(curl -sS -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/${{ env.UPSTREAM_REPO }}/git/refs/heads/dev | jq -r '.object.sha')
7678
77-
# Create alive branch based on LoopKit/LoopWorkspace:dev
79+
# Create alive-main branch based on LoopKit/LoopWorkspace:main
80+
gh api \
81+
--method POST \
82+
-H "Authorization: token $GITHUB_TOKEN" \
83+
-H "Accept: application/vnd.github.v3+json" \
84+
/repos/${{ github.repository_owner }}/LoopWorkspace/git/refs \
85+
-f ref='refs/heads/alive-main' \
86+
-f sha=$SHA_MAIN
87+
88+
# Create alive-dev branch based on LoopKit/LoopWorkspace:dev
7889
gh api \
7990
--method POST \
8091
-H "Authorization: token $GITHUB_TOKEN" \
8192
-H "Accept: application/vnd.github.v3+json" \
8293
/repos/${{ github.repository_owner }}/LoopWorkspace/git/refs \
83-
-f ref='refs/heads/alive' \
84-
-f sha=$SHA
94+
-f ref='refs/heads/alive-dev' \
95+
-f sha=$SHA_DEV
96+
8597
8698
# Checks for changes in upstream repository; if changes exist prompts sync for build
8799
# Performs keepalive to avoid stale fork
@@ -93,23 +105,41 @@ jobs:
93105
NEW_COMMITS: ${{ steps.sync.outputs.has_new_commits }}
94106

95107
steps:
108+
- name: Check if running on main or dev branch
109+
id: check_branch
110+
if: |
111+
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
112+
(vars.SCHEDULED_BUILD != 'false' || vars.SCHEDULED_SYNC != 'false')
113+
run: |
114+
if [ "${GITHUB_REF##*/}" = "main" ]; then
115+
echo "Running on main branch"
116+
echo "::set-output name=alive_branch::${ALIVE_BRANCH_MAIN}"
117+
elif [ "${GITHUB_REF##*/}" = "dev" ]; then
118+
echo "Running on dev branch"
119+
echo "::set-output name=alive_branch::${ALIVE_BRANCH_DEV}"
120+
else
121+
echo "Not running on main or dev branch"
122+
echo "::set-output name=abort_sync::true"
123+
echo "ABORT_SYNC=false" >> $GITHUB_OUTPUT
124+
fi
125+
96126
- name: Checkout target repo
97127
if: |
98128
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
99129
(vars.SCHEDULED_BUILD != 'false' || vars.SCHEDULED_SYNC != 'false')
100130
uses: actions/checkout@v4
101131
with:
102132
token: ${{ secrets.GH_PAT }}
103-
ref: alive
133+
ref: ${{ steps.check_branch.outputs.alive_branch }}
104134

105135
- name: Sync upstream changes
106136
if: | # do not run the upstream sync action on the upstream repository
107137
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
108-
vars.SCHEDULED_SYNC != 'false' && github.repository_owner != 'LoopKit'
138+
vars.SCHEDULED_SYNC != 'false' && github.repository_owner != 'LoopKit' && steps.check_branch.outputs.on_main == 'true'
109139
id: sync
110140
uses: aormsby/[email protected]
111141
with:
112-
target_sync_branch: ${{ env.ALIVE_BRANCH }}
142+
target_sync_branch: ${{ steps.check_branch.outputs.alive_branch }}
113143
shallow_since: 6 months ago
114144
target_repo_token: ${{ secrets.GH_PAT }}
115145
upstream_sync_branch: ${{ env.UPSTREAM_BRANCH }}
@@ -181,7 +211,7 @@ jobs:
181211
- name: Sync upstream changes
182212
if: | # do not run the upstream sync action on the upstream repository
183213
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
184-
vars.SCHEDULED_SYNC != 'false' && github.repository_owner != 'LoopKit'
214+
vars.SCHEDULED_SYNC != 'false' && github.repository_owner != 'LoopKit' && needs.check_latest_from_upstream.outputs.ABORT_SYNC == 'false'
185215
id: sync
186216
uses: aormsby/[email protected]
187217
with:
@@ -195,19 +225,19 @@ jobs:
195225
- name: New commits found
196226
if: |
197227
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
198-
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'true'
228+
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'true' && needs.check_latest_from_upstream.outputs.ABORT_SYNC == 'false'
199229
run: echo "New commits were found to sync."
200230

201231
- name: No new commits
202232
if: |
203233
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true' &&
204-
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'false'
234+
vars.SCHEDULED_SYNC != 'false' && steps.sync.outputs.has_new_commits == 'false' && needs.check_latest_from_upstream.outputs.ABORT_SYNC == 'false'
205235
run: echo "There were no new commits."
206236

207237
- name: Show value of 'has_new_commits'
208238
if: |
209239
needs.check_alive_and_permissions.outputs.WORKFLOW_PERMISSION == 'true'
210-
&& vars.SCHEDULED_SYNC != 'false'
240+
&& vars.SCHEDULED_SYNC != 'false' && needs.check_latest_from_upstream.outputs.ABORT_SYNC == 'false'
211241
run: |
212242
echo ${{ steps.sync.outputs.has_new_commits }}
213243
echo "NEW_COMMITS=${{ steps.sync.outputs.has_new_commits }}" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)