Skip to content

Commit

Permalink
cache the chrome binary
Browse files Browse the repository at this point in the history
  • Loading branch information
nexxeln committed Feb 19, 2025
1 parent 924a63c commit 75aaaec
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 75 deletions.
77 changes: 46 additions & 31 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
path: stateless-build.tar

build-stateful:
runs-on: blacksmith-4vcpu-ubuntu-2204
runs-on: blacksmith-4vcpu-ubuntu-2204-arm
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
Expand All @@ -60,18 +60,42 @@ jobs:
name: stateful-build
path: stateful-build.tar

setup-chrome:
runs-on: blacksmith-4vcpu-ubuntu-2204-arm
outputs:
chrome_path: ${{ steps.chrome-setup.outputs.chrome_path }}
steps:
- uses: actions/checkout@v4
- name: Set up Chrome
id: chrome-setup
run: |
./scripts/check-chrome.sh
echo "chrome_path=${PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH}" >> $GITHUB_OUTPUT
- name: Cache Chrome
uses: useblacksmith/cache@v5
with:
path: /usr/local/chrome
key: chrome-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('scripts/check-chrome.sh') }}

stateless:
name: stateless (${{matrix.shard}})
needs: build-stateless
needs: [build-stateless, setup-chrome]
timeout-minutes: 20
runs-on: blacksmith-4vcpu-ubuntu-2204
runs-on: blacksmith-4vcpu-ubuntu-2204-arm
strategy:
matrix:
shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup

- name: Restore Chrome
uses: useblacksmith/cache@v5
with:
path: /usr/local/chrome
key: chrome-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('scripts/check-chrome.sh') }}

- run: pnpm rebuild -r

- name: Download build
Expand All @@ -84,23 +108,17 @@ jobs:
- name: Untar files
run: tar -xvf stateless-build.tar

- name: Install playwright
- name: Install Playwright deps
run: |
# Install Playwright browsers with system dependencies
if [ "$(uname -m)" = "aarch64" ] || [ "$(uname -m)" = "arm64" ]; then
# Install Chromium from system for ARM
sudo apt-get update
sudo apt-get install -y chromium-browser
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npx playwright install
PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium-browser npx playwright install-deps chromium
else
# Regular installation for AMD64
npx playwright install chromium
fi
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npx playwright install
PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=${{ needs.setup-chrome.outputs.chrome_path }} npx playwright install-deps chromium
- name: Run tests
run: |
PLAYWRIGHT_SHARD=${{matrix.shard}} PLAYWRIGHT_TOTAL=${{strategy.job-total}} pnpm e2e:ci --no-build
PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=${{ needs.setup-chrome.outputs.chrome_path }} \
PLAYWRIGHT_SHARD=${{matrix.shard}} \
PLAYWRIGHT_TOTAL=${{strategy.job-total}} \
pnpm e2e:ci --no-build
- uses: actions/upload-artifact@v4
if: always()
Expand All @@ -112,18 +130,23 @@ jobs:

stateful:
name: stateful (${{matrix.shard}})
needs: build-stateful
needs: [build-stateful, setup-chrome]
timeout-minutes: 10
runs-on: blacksmith-4vcpu-ubuntu-2204
runs-on: blacksmith-4vcpu-ubuntu-2204-arm
strategy:
matrix:
shard: [1, 2, 3]

steps:
- uses: actions/checkout@v4
- run: ./scripts/check-chrome.sh
- uses: ./.github/actions/setup

- name: Restore Chrome
uses: useblacksmith/cache@v5
with:
path: /usr/local/chrome
key: chrome-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('scripts/check-chrome.sh') }}

- run: pnpm rebuild -r

- name: Download build
Expand All @@ -135,22 +158,14 @@ jobs:
- name: Untar files
run: tar -xvf stateful-build.tar

- name: Install playwright
- name: Install Playwright deps
run: |
# Install Playwright browsers with system dependencies
if [ "$(uname -m)" = "aarch64" ] || [ "$(uname -m)" = "arm64" ]; then
# Install Chromium from system for ARM
sudo apt-get update
sudo apt-get install -y chromium-browser
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npx playwright install
PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium-browser npx playwright install-deps chromium
else
# Regular installation for AMD64
npx playwright install chromium
fi
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 npx playwright install
PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=${{ needs.setup-chrome.outputs.chrome_path }} npx playwright install-deps chromium
- name: Run tests
run: |
PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=${{ needs.setup-chrome.outputs.chrome_path }} \
parallel --lb --halt now,success=1,fail=1 ::: \
"pnpm wrangle" \
"pnpm wait-on http://127.0.0.1:8788 && npx playwright test --shard=${{matrix.shard}}/${{strategy.job-total}} --project=stateful"
Expand Down
110 changes: 66 additions & 44 deletions scripts/check-chrome.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash
# Copied and modified from: https://github.com/actions/virtual-environments/issues/5651#issuecomment-1142075171
# Used to ensure that tests are always using the latest version of Chrome.
# This script now handles both ARM64 and AMD64 architectures.
# Script to download and set up Chromium directly from official sources

download_with_retries() {
local URL="$1"
Expand All @@ -20,18 +18,16 @@ download_with_retries() {
interval=30
while [ $retries -gt 0 ]; do
((retries--))
# Temporary disable exit on error to retry on non-zero exit code
set +e
http_code=$(eval $COMMAND)
exit_code=$?
if [ $http_code -eq 200 ] && [ $exit_code -eq 0 ]; then
echo "Download completed"
return 0
else
echo "Error — Either HTTP response code for '$URL' is wrong - '$http_code' or exit code is not 0 - '$exit_code'. Waiting $interval seconds before the next attempt, $retries attempts left"
echo "Error — HTTP code: '$http_code', exit code: '$exit_code'. Retrying in $interval seconds, $retries attempts left"
sleep 30
fi
# Enable exit on error back
set -e
done

Expand All @@ -43,51 +39,77 @@ download_with_retries() {
ARCH=$(uname -m)
echo "Detected architecture: $ARCH"

# Check if Chrome is installed
if command -v google-chrome &> /dev/null; then
INSTALLED_CHROME_VERSION=$(google-chrome --product-version)
else
INSTALLED_CHROME_VERSION="none"
fi

LATEST_VERSION_URL="https://omahaproxy.appspot.com/linux"
echo "Fetching latest chrome version from: $LATEST_VERSION_URL"
LATEST_CHROME_VERSION=$(curl "$LATEST_VERSION_URL")

echo "Installed Chrome Version: $INSTALLED_CHROME_VERSION"
echo "Latest Chrome Version: $LATEST_CHROME_VERSION"
# Create chrome directory
CHROME_DIR="/usr/local/chrome"
sudo mkdir -p $CHROME_DIR

if [ "$INSTALLED_CHROME_VERSION" == "$LATEST_CHROME_VERSION" ]; then
echo "The latest major version of Chrome is already installed."
exit 0
fi

# Install dependencies
echo "Installing dependencies..."
sudo apt-get update
sudo apt-get install -y wget gnupg

# Add Chrome repository and install Chrome based on architecture
if [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
echo "Installing Chrome for ARM64..."
echo "Setting up Chromium for ARM64..."

# Use Chromium directly from Ubuntu's Snap store - they provide ARM builds
SNAP_CHROMIUM_URL="https://launchpad.net/ubuntu/+archive/primary/+files/chromium-browser_120.0.6099.224-0ubuntu0.18.04.1_arm64.deb"
CHROMIUM_DEB="/tmp/chromium-browser.deb"

# For ARM64, we need to use Chromium as Chrome doesn't provide official ARM64 builds
sudo apt-get install -y chromium-browser
# Download and extract
download_with_retries $SNAP_CHROMIUM_URL "/tmp" "chromium-browser.deb"
cd /tmp
ar x chromium-browser.deb
tar xf data.tar.xz

# Create symlink to make chromium accessible as google-chrome
sudo ln -sf /usr/bin/chromium-browser /usr/bin/google-chrome
# Move binary to our chrome directory
sudo mv usr/lib/chromium-browser/* $CHROME_DIR/

# Install additional dependencies that might be needed
sudo apt-get install -y libnss3 libgbm1 libasound2
# Create symlink
sudo ln -sf $CHROME_DIR/chrome /usr/local/bin/google-chrome

# Install minimal dependencies
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
libnss3 \
libgbm1 \
libasound2 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libcups2 \
libdrm2 \
libxkbcommon0 \
libxcomposite1 \
libxdamage1 \
libxfixes3 \
libxrandr2 \
libgbm1 \
libasound2
else
echo "Installing Chrome for AMD64..."
# Download and install Google Chrome
CHROME_DEB_URL="https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"
CHROME_DEB_NAME="google-chrome-stable_current_amd64.deb"
download_with_retries $CHROME_DEB_URL "/tmp" "${CHROME_DEB_NAME}"
sudo apt-get install -y "/tmp/${CHROME_DEB_NAME}" -f
echo "Setting up Chrome for AMD64..."
CHROME_URL="https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"
download_with_retries $CHROME_URL "/tmp" "chrome.deb"

cd /tmp
ar x chrome.deb
tar xf data.tar.xz

# Move binary to our chrome directory
sudo mv opt/google/chrome/* $CHROME_DIR/

# Create symlink
sudo ln -sf $CHROME_DIR/chrome /usr/local/bin/google-chrome

# Install minimal dependencies
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
libnss3 \
libgbm1 \
libasound2
fi

# Set up chrome sandbox
sudo chown root:root $CHROME_DIR/chrome_sandbox
sudo chmod 4755 $CHROME_DIR/chrome_sandbox
sudo cp $CHROME_DIR/chrome_sandbox $CHROME_DIR/chrome-sandbox

# Verify installation
CHROME_VERSION=$(google-chrome --version)
echo "Installed Chrome/Chromium version: $CHROME_VERSION"
echo "Installed Chrome/Chromium version: $CHROME_VERSION"

# Export chrome path for Playwright
echo "PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/local/bin/google-chrome" >> $GITHUB_ENV

0 comments on commit 75aaaec

Please sign in to comment.