diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 64bd5bea831..e1b92a4d58d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/scripts/push-bundle-size.sh b/scripts/push-bundle-size.sh new file mode 100755 index 00000000000..c09e0d39436 --- /dev/null +++ b/scripts/push-bundle-size.sh @@ -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" diff --git a/scripts/update_bundle_size.py b/scripts/update_bundle_size.py new file mode 100755 index 00000000000..44822b3ea61 --- /dev/null +++ b/scripts/update_bundle_size.py @@ -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()