Skip to content

Commit

Permalink
[CI] Automate the merge of the release branches (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
testableapple authored Aug 1, 2024
1 parent 7b863b4 commit bb6d827
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/release-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "Merge release"

on:
issue_comment:
types: [created]

workflow_dispatch:

jobs:
merge-comment:
name: Merge release to main
runs-on: macos-14
if: github.event_name == 'workflow_dispatch' || (github.event.issue.pull_request && github.event.issue.state == 'open' && github.event.comment.body == '/merge release')
steps:
- name: Connect iOS Bot
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}

- uses: actions/[email protected]
with:
fetch-depth: 0

- uses: ./.github/actions/ruby-cache

- name: Merge
run: bundle exec fastlane merge_release_to_main author:"$USER_LOGIN" --verbose
env:
GITHUB_TOKEN: ${{ secrets.ADMIN_API_TOKEN }} # A token with the "admin:org" scope to get the list of the team members on GitHub
GITHUB_PR_NUM: ${{ github.event.issue.number }}
USER_LOGIN: ${{ github.event.comment.user.login != null && github.event.comment.user.login || github.event.sender.login }}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ jobs:
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}

- uses: actions/[email protected]
with:
fetch-depth: 0

- name: Extract version from branch name (for release branches)
if: startsWith(github.event.pull_request.head.ref, 'release/')
run: |
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
VERSION=${BRANCH_NAME#release/}
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV
- uses: ./.github/actions/ruby-cache

- name: "Fastlane - Publish Release"
if: startsWith(github.event.pull_request.head.ref, 'release/')
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ jobs:
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}

- uses: actions/[email protected]
with:
fetch-depth: 0 # to fetch git tags

- uses: ./.github/actions/ruby-cache

- uses: ./.github/actions/xcode-cache

- name: Create Release PR
run: bundle exec fastlane release version:"${{ github.event.inputs.version }}" --verbose
env:
Expand Down
51 changes: 30 additions & 21 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ outstanding_status = '🚀' # Outstanding performance
before_all do |lane|
if is_ci
setup_ci
sh('git config --global user.name "Stream Bot"')
xcversion(version: xcode_version) unless [:publish_release, :allure_launch, :allure_upload, :pod_lint, :stop_e2e_helpers].include?(lane)
elsif lane == :test_e2e
stop_e2e_helpers
Expand Down Expand Up @@ -489,37 +490,46 @@ private_lane :build_example_app do |options|
)
end

lane :merge_release_to_main do
lane :merge_release_to_main do |options|
ensure_git_status_clean
sh('git checkout main')
sh('git pull')

# Grep all remote release branches and ensure there's only one
release_branches = sh(command: 'git branch -a', log: false).delete(' ').split("\n").grep(%r(origin/.*release/))
UI.user_error!("Expected 1 release branch, found #{release_branches.size}") if release_branches.size != 1
release_branch =
if is_ci
# This API operation needs the "admin:org" scope.
ios_team = sh('gh api orgs/GetStream/teams/ios-developers/members -q ".[].login"', log: false).split
UI.user_error!("#{options[:author]} is not a member of the iOS Team") unless ios_team.include?(options[:author])

current_branch
else
release_branches = sh(command: 'git branch -a', log: false).delete(' ').split("\n").grep(%r(origin/.*release/))
UI.user_error!("Expected 1 release branch, found #{release_branches.size}") if release_branches.size != 1

release_branches.first
end

UI.user_error!("`#{release_branch}`` branch does not match the release branch pattern: `release/*`") unless release_branch.start_with?('release/')

sh('git checkout origin/main')
sh('git pull origin main')

# Merge release branch to main. For more info, read: https://notion.so/iOS-Branching-Strategy-37c10127dc26493e937769d44b1d6d9a
sh("git merge #{release_branches.first} --ff-only")
UI.user_error!('Not pushing changes') unless prompt(text: 'Will push changes. All looking good?', boolean: true)
sh('git push')
UI.important('Please, wait for the `Publish new release` workflow to pass on GitHub Actions: ' \
"https://github.com/#{github_repo}/actions/workflows/publish-release.yml")
sh("git merge #{release_branch} --ff-only")
sh('git push origin main')

comment = "[Publication of the release](https://github.com/#{github_repo}/actions/workflows/release-publish.yml) has been launched 👍"
UI.important(comment)
create_pr_comment(pr_num: ENV.fetch('GITHUB_PR_NUM'), text: comment)
end

lane :merge_main_to_develop do
if is_ci
sh('git reset --hard')
else
ensure_git_status_clean
end

ensure_git_status_clean
sh('git checkout main')
sh('git pull origin main')
sh('git checkout develop')
sh('git checkout origin/develop')
sh('git pull origin develop')
sh('git log develop..main')
sh('git merge main')
sh('git push')
sh('git push origin develop')
end

desc 'Compresses the XCFrameworks into zip files'
Expand Down Expand Up @@ -594,7 +604,6 @@ private_lane :update_spm do |options|
File.open('./Package.swift', 'w') { |file| file << file_data }

# Update the repo
sh('git config --global user.name "Stream Bot"')
sh('git add -A')
sh("git commit -m 'Bump #{version}'")
sh('git push')
Expand Down Expand Up @@ -893,7 +902,7 @@ end
private_lane :create_pr_comment do |options|
if is_ci && !options[:pr_num].to_s.empty?
last_comment = sh("gh pr view #{options[:pr_num]} --json comments --jq '.comments | map(select(.author.login == \"Stream-iOS-Bot\")) | last'")
edit_last_comment = last_comment.include?(options[:edit_last_comment_with_text]) ? '--edit-last' : ''
edit_last_comment = options[:edit_last_comment_with_text] && last_comment.include?(options[:edit_last_comment_with_text]) ? '--edit-last' : ''
sh("gh pr comment #{options[:pr_num]} #{edit_last_comment} -b '#{options[:text]}'")
end
end
Expand Down

0 comments on commit bb6d827

Please sign in to comment.