Skip to content

Commit 03081df

Browse files
[CI] Automate the merge of the release branches (#570)
1 parent acfa498 commit 03081df

File tree

4 files changed

+67
-20
lines changed

4 files changed

+67
-20
lines changed

.github/workflows/release-merge.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "Merge release"
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
workflow_dispatch:
8+
9+
jobs:
10+
merge-comment:
11+
name: Merge release to main
12+
runs-on: macos-14
13+
if: github.event_name == 'workflow_dispatch' || (github.event.issue.pull_request && github.event.issue.state == 'open' && github.event.comment.body == '/merge release')
14+
steps:
15+
- name: Connect iOS Bot
16+
uses: webfactory/ssh-agent@v0.7.0
17+
with:
18+
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
19+
20+
- uses: actions/checkout@v4.1.1
21+
with:
22+
fetch-depth: 0
23+
24+
- uses: ./.github/actions/ruby-cache
25+
26+
- name: Merge
27+
run: bundle exec fastlane merge_release_to_main author:"$USER_LOGIN" --verbose
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.ADMIN_API_TOKEN }} # A token with the "admin:org" scope to get the list of the team members on GitHub
30+
GITHUB_PR_NUM: ${{ github.event.issue.number }}
31+
USER_LOGIN: ${{ github.event.comment.user.login != null && github.event.comment.user.login || github.event.sender.login }}

.github/workflows/publish-release.yml renamed to .github/workflows/release-publish.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ jobs:
1717
uses: webfactory/ssh-agent@v0.7.0
1818
with:
1919
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
20+
2021
- uses: actions/checkout@v4.1.1
2122
with:
2223
fetch-depth: 0
24+
2325
- name: Extract version from branch name (for release branches)
2426
if: startsWith(github.event.pull_request.head.ref, 'release/')
2527
run: |
2628
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
2729
VERSION=${BRANCH_NAME#release/}
2830
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV
31+
2932
- uses: ./.github/actions/ruby-cache
33+
3034
- name: "Fastlane - Publish Release"
3135
if: startsWith(github.event.pull_request.head.ref, 'release/')
3236
env:

.github/workflows/start-new-release.yml renamed to .github/workflows/release-start.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ jobs:
1717
uses: webfactory/ssh-agent@v0.7.0
1818
with:
1919
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
20+
2021
- uses: actions/checkout@v4.1.1
2122
with:
2223
fetch-depth: 0 # to fetch git tags
24+
2325
- uses: ./.github/actions/ruby-cache
26+
2427
- uses: ./.github/actions/xcode-cache
28+
2529
- name: Create Release PR
2630
run: bundle exec fastlane release version:"${{ github.event.inputs.version }}" --verbose
2731
env:

fastlane/Fastfile

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ outstanding_status = '🚀' # Outstanding performance
2828
before_all do |lane|
2929
if is_ci
3030
setup_ci
31+
sh('git config --global user.name "Stream Bot"')
3132
xcversion(version: xcode_version) unless [:publish_release, :allure_launch, :allure_upload, :copyright, :pod_lint].include?(lane)
3233
end
3334
end
@@ -123,31 +124,39 @@ lane :publish_release do |options|
123124
merge_main_to_develop
124125
end
125126

126-
lane :merge_release_to_main do
127+
lane :merge_release_to_main do |options|
127128
ensure_git_status_clean
128-
sh('git checkout main')
129-
sh('git pull')
130129

131-
# Grep all remote release branches and ensure there's only one
132-
release_branches = sh(command: 'git branch -a', log: false).delete(' ').split("\n").grep(%r(origin/.*release/))
133-
UI.user_error!("Expected 1 release branch, found #{release_branches.size}") if release_branches.size != 1
130+
release_branch =
131+
if is_ci
132+
# This API operation needs the "admin:org" scope.
133+
ios_team = sh('gh api orgs/GetStream/teams/ios-developers/members -q ".[].login"', log: false).split
134+
UI.user_error!("#{options[:author]} is not a member of the iOS Team") unless ios_team.include?(options[:author])
135+
136+
current_branch
137+
else
138+
release_branches = sh(command: 'git branch -a', log: false).delete(' ').split("\n").grep(%r(origin/.*release/))
139+
UI.user_error!("Expected 1 release branch, found #{release_branches.size}") if release_branches.size != 1
140+
141+
release_branches.first
142+
end
143+
144+
UI.user_error!("`#{release_branch}`` branch does not match the release branch pattern: `release/*`") unless release_branch.start_with?('release/')
145+
146+
sh('git checkout origin/main')
147+
sh('git pull origin main')
134148

135149
# Merge release branch to main. For more info, read: https://notion.so/iOS-Branching-Strategy-37c10127dc26493e937769d44b1d6d9a
136-
sh("git merge #{release_branches.first} --ff-only")
137-
UI.user_error!('Not pushing changes') unless prompt(text: 'Will push changes. All looking good?', boolean: true)
138-
sh('git push')
139-
UI.important('Please, wait for the `Publish new release` workflow to pass on GitHub Actions: ' \
140-
"https://github.com/#{github_repo}/actions/workflows/publish-release.yml")
150+
sh("git merge #{release_branch} --ff-only")
151+
sh('git push origin main')
152+
153+
comment = "[Publication of the release](https://github.com/#{github_repo}/actions/workflows/release-publish.yml) has been launched 👍"
154+
UI.important(comment)
155+
create_pr_comment(pr_num: ENV.fetch('GITHUB_PR_NUM'), text: comment)
141156
end
142157

143158
lane :merge_main_to_develop do
144-
if is_ci
145-
sh('git config --global user.name "Stream Bot"')
146-
sh('git reset --hard')
147-
else
148-
ensure_git_status_clean
149-
end
150-
159+
ensure_git_status_clean
151160
sh('git checkout main')
152161
sh('git pull origin main')
153162
sh('git checkout origin/develop')
@@ -408,7 +417,6 @@ private_lane :update_spm do |options|
408417
File.open('./Package.swift', 'w') { |file| file << file_data }
409418

410419
# Update the repo
411-
sh('git config --global user.name "Stream Bot"')
412420
sh('git add -A')
413421
sh("git commit -m 'Bump #{version}'")
414422
sh('git push')
@@ -622,7 +630,7 @@ end
622630
private_lane :create_pr_comment do |options|
623631
if is_ci && !options[:pr_num].to_s.empty?
624632
last_comment = sh("gh pr view #{options[:pr_num]} --json comments --jq '.comments | map(select(.author.login == \"Stream-iOS-Bot\")) | last'")
625-
edit_last_comment = last_comment.include?(options[:edit_last_comment_with_text]) ? '--edit-last' : ''
633+
edit_last_comment = options[:edit_last_comment_with_text] && last_comment.include?(options[:edit_last_comment_with_text]) ? '--edit-last' : ''
626634
sh("gh pr comment #{options[:pr_num]} #{edit_last_comment} -b '#{options[:text]}'")
627635
end
628636
end

0 commit comments

Comments
 (0)