Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bundle size check #11083

Merged
merged 23 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
69165c3
feat(INFRA-2045): ci and script to push bundle size
makemesteaks Aug 29, 2024
1e0aa32
feat(INFRA-2045): ios bundlesize ship
makemesteaks Aug 30, 2024
e7904fe
feat(INFRA-2045): execute script
makemesteaks Sep 19, 2024
f29d7ed
feat(INFRA-2045): forgot checkout
makemesteaks Sep 19, 2024
b23b119
feat(INFRA-2045): fixed github token for bundle push
bsgrigorov Sep 27, 2024
54c2a4c
feat(INFRA-2045): updated repo name
bsgrigorov Sep 27, 2024
ff5a2f6
feat(INFRA-2045): fixed bundle filepath
bsgrigorov Sep 27, 2024
7aa694f
feat(INFRA-2045): fixed bundle js file name
bsgrigorov Sep 27, 2024
4f5f122
Merge branch 'main' into feat/INFRA-2045-bundle-size
bsgrigorov Sep 30, 2024
e396a84
Merge branch 'main' into feat/INFRA-2045-bundle-size
bsgrigorov Sep 30, 2024
3798895
Update scripts/push-bundle-size.sh
bsgrigorov Oct 1, 2024
e055412
Update .github/workflows/ci.yml
bsgrigorov Oct 1, 2024
13dbb39
Update .github/workflows/ci.yml
bsgrigorov Oct 1, 2024
bb1b6d0
Update scripts/push-bundle-size.sh
bsgrigorov Oct 1, 2024
825ed1a
Update .github/workflows/ci.yml
bsgrigorov Oct 1, 2024
313e753
Update scripts/push-bundle-size.sh
bsgrigorov Oct 1, 2024
5df9928
Update .github/workflows/ci.yml
bsgrigorov Oct 1, 2024
1fdbd04
Update scripts/push-bundle-size.sh
bsgrigorov Oct 1, 2024
8918f7a
Merge branch 'main' into feat/INFRA-2045-bundle-size
bsgrigorov Oct 1, 2024
b945ccd
Updated ci workflow download and upload step versions
bsgrigorov Oct 1, 2024
6a31ba8
Updated to run on main only
bsgrigorov Oct 1, 2024
c7420af
Merge branch 'main' into feat/INFRA-2045-bundle-size
bsgrigorov Oct 1, 2024
b94d2fd
Merge branch 'main' into feat/INFRA-2045-bundle-size
legobeat Oct 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ jobs:
- name: Check bundle size
run: ./scripts/js-bundle-stats.sh ios/main.jsbundle 40

- name: Upload iOS bundle
uses: actions/upload-artifact@v4
with:
name: ios-bundle
path: ios/main.jsbundle

ship-js-bundle-size-check:
runs-on: ubuntu-latest
needs: [js-bundle-size-check]
if: ${{ github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v4

- name: Download iOS bundle
uses: actions/download-artifact@v4
with:
name: ios-bundle
path: ios/main.jsbundle


- name: Push bundle size to mobile_bundlesize_stats repo
run: ./scripts/push-bundle-size.sh
env:
GITHUB_ACTOR: metamaskbot
GITHUB_TOKEN: ${{ secrets.MOBILE_BUNDLESIZE_TOKEN }}

sonar-cloud:
runs-on: ubuntu-20.04
needs: merge-unit-tests
Expand Down
64 changes: 64 additions & 0 deletions scripts/push-bundle-size.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

set -euo pipefail

# Function to log messages
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}

# Check if running in GitHub Actions
if [[ "${GITHUB_ACTIONS:-}" != 'true' ]]; then
log "Error: This script must be run in a GitHub Actions environment"
exit 1
fi

if [[ -z "${GITHUB_ACTOR:-}" ]]; then
log "Error: GITHUB_ACTOR environment variable must be set"
exit 1
fi

# Check for GITHUB_TOKEN
if [[ -z "${GITHUB_TOKEN:-}" ]]; then
log "Error: GITHUB_TOKEN environment variable must be set"
exit 1
fi

# Set up git configuration
git config --global user.email "metamaskbot@users.noreply.github.com"
git config --global user.name "MetaMask Bot"

# Create a temporary directory
temp_dir="$(mktemp -d)"
trap 'rm -rf "$temp_dir"' EXIT

# Clone the repository
repo_url="https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/metamask/mobile_bundlesize_stats.git"
if ! git clone "$repo_url" "$temp_dir"; then
log "Error: Failed to clone repository"
exit 1
fi

# Get commit ID and bundle size
commit_id="$(git rev-parse HEAD)"
bundle_size="$(stat -f%z 'ios/main.jsbundle' 2>/dev/null || stat -c%s 'ios/main.jsbundle')"
timestamp="$(date +%s%3N)"
output_file="$temp_dir/stats/bundle_size_data.js"

./scripts/update_bundle_size.py "$output_file" "$commit_id" "$bundle_size" "$timestamp"

cd "$temp_dir"
# Commit and push changes
git add "$output_file"
if git commit -m "Add bundle size for commit: ${commit_id}"; then
if git push origin HEAD:main; then
log "Success: Bundle size data updated and pushed to main branch"
else
log "Error: Failed to push changes"
exit 1
fi
else
log "Info: No changes to commit"
fi

log "Script completed successfully"
48 changes: 48 additions & 0 deletions scripts/update_bundle_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

import argparse
import json
import os

def update_bundle_size(file_path, commit_id, bundle_size, timestamp):
# Read the existing content of the file
if os.path.exists(file_path):
with open(file_path, 'r') as file:
content = file.read()
# Extract the data object from the content
data_start = content.find('{')
data_end = content.rfind('}')
if data_start != -1 and data_end != -1:
data_str = content[data_start:data_end+1]
data = json.loads(data_str)
else:
data = {}
else:
data = {}

# Update the data with new information
# If commit_id already exists, it will overwrite the existing data
data[commit_id] = {
"ios": int(bundle_size),
"timestamp": int(timestamp)
}

# Write the updated data back to the file
with open(file_path, 'w') as file:
file.write("const data = ")
json.dump(data, file, indent=4, separators=(',', ': '))
file.write(";\n")

def main():
parser = argparse.ArgumentParser(description="Update bundle size data")
parser.add_argument("file_path", help="Path to the data file")
parser.add_argument("commit_id", help="Commit ID")
parser.add_argument("bundle_size", type=int, help="Bundle size")
parser.add_argument("timestamp", type=int, help="Timestamp")

args = parser.parse_args()

update_bundle_size(args.file_path, args.commit_id, args.bundle_size, args.timestamp)

if __name__ == "__main__":
main()
Loading