Skip to content

Commit

Permalink
[CI] Correctly retrieve a git branch on CI (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
testableapple authored Jul 31, 2024
1 parent 9eb77e6 commit 37b1eba
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 39 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/cron-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ concurrency:

env:
HOMEBREW_NO_INSTALL_CLEANUP: 1 # Disable cleanup for homebrew, we don't need it on CI
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
build-test-app-and-frameworks:
Expand Down Expand Up @@ -54,7 +55,6 @@ jobs:
runs-on: ${{ matrix.os }}
env:
GITHUB_EVENT: ${{ toJson(github.event) }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ALLURE_TOKEN: ${{ secrets.ALLURE_TOKEN }}
XCODE_VERSION: ${{ matrix.xcode }}
steps:
Expand Down Expand Up @@ -149,6 +149,10 @@ jobs:
env:
XCODE_VERSION: "15.0.1"
steps:
- name: Connect iOS Bot
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
- uses: actions/[email protected]
- uses: ./.github/actions/ruby-cache
- name: List Xcode versions
Expand All @@ -157,10 +161,6 @@ jobs:
- name: Build SwiftUI
run: bundle exec fastlane test_ui device:"iPhone 15" build_for_testing:true
timeout-minutes: 25
- name: Install Bot SSH Key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
- name: Build XCFrameworks
run: bundle exec fastlane build_xcframeworks
timeout-minutes: 25
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: macos-13
if: github.event.pull_request.merged == true # only merged pull requests must trigger this job
steps:
- name: Install Bot SSH Key
- name: Connect iOS Bot
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
Expand Down
8 changes: 1 addition & 7 deletions .github/workflows/sdk-size-metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
env:
GITHUB_TOKEN: '${{ secrets.CI_BOT_GITHUB_TOKEN }}'
steps:
- name: Install Bot SSH Key
- name: Connect iOS Bot
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
Expand All @@ -28,16 +28,10 @@ jobs:

- uses: ./.github/actions/bootstrap

- name: Get branch name
id: get_branch_name
run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT

- name: Run SDK Size Metrics
run: bundle exec fastlane show_frameworks_sizes
timeout-minutes: 30
env:
BRANCH_NAME: ${{ steps.get_branch_name.outputs.branch }}
GITHUB_PR_NUM: ${{ github.event.pull_request.number }}
GITHUB_EVENT_NAME: ${{ github.event_name }}
MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
APPSTORE_API_KEY: ${{ secrets.APPSTORE_API_KEY }}
24 changes: 6 additions & 18 deletions .github/workflows/smoke-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ concurrency:
env:
HOMEBREW_NO_INSTALL_CLEANUP: 1 # Disable cleanup for homebrew, we don't need it on CI
IOS_SIMULATOR_DEVICE: "iPhone 15 Pro (17.4)"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_NUM: ${{ github.event.pull_request.number }}

jobs:
build-test-app-and-frameworks:
Expand Down Expand Up @@ -48,8 +50,6 @@ jobs:
name: Automated Code Review
runs-on: macos-13
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_NUM: ${{ github.event.number }}
XCODE_VERSION: "15.0.1"
if: ${{ github.event_name != 'push' && github.event.inputs.snapshots != 'true' }}
steps:
Expand All @@ -72,6 +72,10 @@ jobs:
env:
XCODE_VERSION: "15.0.1"
steps:
- name: Connect iOS Bot
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
- uses: actions/[email protected]
- uses: ./.github/actions/ruby-cache
- name: List Xcode versions
Expand All @@ -80,10 +84,6 @@ jobs:
- name: Build SwiftUI
run: bundle exec fastlane test_ui device:"iPhone 15" build_for_testing:true
timeout-minutes: 25
- name: Install Bot SSH Key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
- name: Build XCFrameworks
run: bundle exec fastlane build_xcframeworks
timeout-minutes: 25
Expand All @@ -110,19 +110,12 @@ jobs:
timeout-minutes: 60
env:
GITHUB_TOKEN: ${{ secrets.CI_BOT_GITHUB_TOKEN }} # to open a PR
GITHUB_PR_NUM: ${{ github.event.number }}
- name: Get branch name
id: get_branch_name
run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
- name: Run Sonar analysis
if: ${{ github.event.inputs.snapshots != 'true' }}
run: bundle exec fastlane sonar_upload
env:
BRANCH_NAME: ${{ steps.get_branch_name.outputs.branch }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
- uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
Expand Down Expand Up @@ -164,8 +157,6 @@ jobs:
run: bundle exec fastlane allure_launch
env:
ALLURE_TOKEN: ${{ secrets.ALLURE_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_NUM: ${{ github.event.number }}
GITHUB_EVENT: ${{ toJson(github.event) }}
- id: get_launch_id
run: echo "launch_id=${{env.LAUNCH_ID}}" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -203,14 +194,11 @@ jobs:
env:
XCODE_VERSION: "15.2" # the most stable pair of Xcode
IOS_SIMULATOR_DEVICE: "iPhone 15 Pro (17.2)" # and iOS
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_NUM: ${{ github.event.number }}
MATRIX_SIZE: ${{ strategy.job-total }}
- name: Allure TestOps Upload
if: env.LAUNCH_ID != '' && (success() || failure())
run: bundle exec fastlane allure_upload launch_id:$LAUNCH_ID
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ALLURE_TOKEN: ${{ secrets.ALLURE_TOKEN }}
- name: Allure TestOps Launch Removal
if: env.LAUNCH_ID != '' && cancelled()
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/start-new-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Start new release
runs-on: macos-14
steps:
- name: Install Bot SSH Key
- name: Connect iOS Bot
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BOT_SSH_PRIVATE_KEY }}
Expand Down
5 changes: 2 additions & 3 deletions fastlane/Allurefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ allure_results_path = 'allure-results'

desc 'Upload test results to Allure TestOps'
lane :allure_upload do |options|
branch = github_run_details['head_branch']
allure_args = "-e #{allure_url} --project-id #{allure_project_id} --launch-id #{options[:launch_id]}"
sh("./xcresults export test_output/StreamChatSwiftUITestsApp.xcresult #{allure_results_path} || true")
sh("./allurectl launch reopen #{options[:launch_id]} || true") # to prevent allure from uploading results to a closed launch
sh("env BRANCH_NAME='#{branch}' ./allurectl upload #{allure_args} #{allure_results_path} || true")
sh("env BRANCH_NAME='#{current_branch}' ./allurectl upload #{allure_args} #{allure_results_path} || true")
UI.success("Check out test results in Allure TestOps: #{allure_url}/launch/#{options[:launch_id]} 🎉")
end

Expand Down Expand Up @@ -54,6 +53,6 @@ def github_run_details
return nil unless is_ci

github_path = "#{ENV.fetch('GITHUB_API_URL', nil)}/repos/#{ENV.fetch('GITHUB_REPOSITORY', nil)}/actions/runs/#{ENV.fetch('GITHUB_RUN_ID', nil)}"
output = sh(command: "curl -s -H 'authorization: Bearer #{ENV.fetch('GITHUB_TOKEN', nil)}' -X GET -G #{github_path}", log: false)
output = sh(command: "curl -s -H 'authorization: Bearer #{ENV.fetch('GITHUB_TOKEN', nil)}' -X GET -G #{github_path}")
JSON.parse(output)
end
14 changes: 11 additions & 3 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ lane :sources_matrix do
ui: ['Sources', 'StreamChatSwiftUITests', xcode_project],
sample_apps: ['Sources', 'DemoAppSwiftUI', xcode_project],
ruby: ['fastlane', 'Gemfile', 'Gemfile.lock'],
size: ['Sources', xcode_project]
size: ['Sources', xcode_project],
sonar: ['Sources']
}
end

Expand Down Expand Up @@ -625,8 +626,15 @@ private_lane :create_pr_comment do |options|
end
end

private_lane :current_branch do
ENV['BRANCH_NAME'].to_s.empty? ? git_branch : ENV.fetch('BRANCH_NAME')
lane :current_branch do
branch = if ENV['GITHUB_PR_NUM'].to_s.empty?
git_branch
else
sh("gh pr view #{ENV.fetch('GITHUB_PR_NUM')} --json headRefName -q .headRefName").strip
end

UI.important("Current branch: #{branch} 🕊️")
branch
end

private_lane :git_status do |options|
Expand Down
4 changes: 3 additions & 1 deletion fastlane/Sonarfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

desc 'Get code coverage report and run complexity analysis for Sonar'
lane :sonar_upload do
next unless is_check_required(sources: sources_matrix[:sonar], force_check: @force_check)

sonar_args = ''
version_number = get_version_number(
xcodeproj: 'StreamChatSwiftUI.xcodeproj',
Expand Down Expand Up @@ -35,7 +37,7 @@ private_lane :sonar_options do |options|
if ENV['GITHUB_EVENT_NAME'] == 'pull_request'
default_options.merge(pull_request_branch: ENV.fetch('GITHUB_HEAD_REF', nil),
pull_request_base: ENV.fetch('GITHUB_BASE_REF', nil),
pull_request_key: ENV.fetch('PR_NUMBER', nil))
pull_request_key: ENV.fetch('GITHUB_PR_NUM', nil))
else
default_options.merge(branch_name: current_branch, project_version: options[:version_number])
end
Expand Down

0 comments on commit 37b1eba

Please sign in to comment.