Skip to content

Commit 5fe7ff2

Browse files
committed
Update docs and make compile accept more installation type
1 parent fdff192 commit 5fe7ff2

File tree

3 files changed

+78
-17
lines changed

3 files changed

+78
-17
lines changed

Buildpacks.PNG

14.3 KB
Loading

README.md

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,66 @@
1-
# heroku-playwright-chromium
2-
A buildpack to install the Playwright Chromium executable ONLY.
1+
# heroku-playwright-python-browsers
2+
A buildpack to install Playwright browser executables for usage with Python on Heroku.
33

4+
**NOTE**: this buildpack and the following instructions was developed for usage with Chromium in mind. It is likely to work with Firefox and Webkit as well BUT hasn't been tested by the author. You are on your own when using Firefox and Webkit since I don't use those tools. Let me know if this buildpack works though and I'll update the docs.
45

56
# Usage
67

7-
1. Add this buildpack to your app alongside the [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack).
8-
- The script CANNOT install the system requirements due to restrictions from Heroku. It's only able to install the Chromium exe.
9-
- Thus, we rely on [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack) to get the other packages.
10-
2. Run and deploy
8+
Follow the steps below.
119

12-
# Customization
13-
By default, this will install the necessary exe into a folder called `/browsers` under the build directory (the directory where the root script lives).
10+
## Tell Python to install `playwright`
1411

15-
To change the this folder's name, override the `INSTALL_PATH` environmental variable.
12+
This buildpack relies on the CLI command `playwright install`. This requires [`playwright`](https://pypi.org/project/playwright/) to be installed. Do this by specifying a `requirements.txt` or any other methods accepted by the Heroku Python buildpacks.
1613

17-
# Notes
18-
This was developed for usage with `playwright-python` in mind so support for other languages is unknown.
14+
## Buildpack ordering
15+
16+
Order matters for this buildpack to work. Here's the run order that you should should have:
17+
18+
![Buildpack Image](Buildpacks.PNG)
19+
20+
1. The official [Python buildpack](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-python).
21+
- Runs first so that Playwright is available as a CLI tool after the package installation is done
22+
1. This build pack. [Here's how to add this buildpack to your app](https://devcenter.heroku.com/articles/buildpacks#using-a-third-party-buildpack) by using its Git URL.
23+
2. The [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack)
24+
- **Why?** This buildpack CANNOT install system requirements (i.e. `playwright install --with-deps`) due to restrictions from Heroku. It's only able to install the browser executables. Thus, we rely on [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack) to get the needed system packages.
25+
26+
27+
## Customize the following config variables:
28+
1. `PLAYWRIGHT_BUILDPACK_BROWSERS` accepts a comma-separated list of the browser names `chromium,firefox,webkit`. By default, it's installing the executables for all these browsers.
29+
- E.g. To only install Chromium dependencies, set the variable to `chromium`. This will reduce the slug size in the end too.
30+
- **NOTE**: this is what the author uses, usage with the other 2 browsers are unknown. Feel free to test them out and let me know if they work and I'll update the docs.
31+
- This config variable is intended to be shared with [heroku-playwright-buildpack](https://github.com/playwright-community/heroku-playwright-buildpack).
32+
2. `BUILDPACK_BROWSERS_INSTALL_PATH` is the path where the browsers will be installed. Its default value is `browsers` and it's a relative path starting from the [`BUILD_DIR`](https://devcenter.heroku.com/articles/buildpack-api#bin-compile) (the starting directory of the compiled slug to be used by Heroku). It's necessary to have the installed browsers within the `BUILD_DIR` to have the file included by the time Heroku starts the dyno.
33+
- E.g. To install in the directory `utils/browsers`, set the variable to `utils/browsers`.
34+
- This config variable is exported by the buildpack for your use in the next step
35+
36+
## Modify your browser init script
37+
38+
Since the browsers are installed in a non-default location, we have to specify the executable path when we create the browser instances.
1939

40+
Here's how you should create your browser:
2041

42+
```
43+
with sync_playwright() as p:
44+
try:
45+
browser = p.chromium.launch(executable_path=env("CHROME_EXECUTABLE_PATH"), ...other arguments)
46+
# for chromium, you don't have to pass in flags like `no-sandbox` or `headless` because they are enabled by default.
47+
# Flags like `--disable-dev-shm-usage` and `--remote-debugging-port=9222` can be added if needed but I haven't needed that
48+
```
49+
50+
51+
# Questions
52+
53+
**1. Why doesn't this buildpack install the requirements as well?**
54+
55+
The command to install the system packages along with the browser is `playwright install --with-deps`. For some reason, this action is blocked by Heroku since it uses sudo underneath (which is not allowed as of 2024). Thus, only the browser installation can go through but that alone is not enough. We still need the system packages which is provided by `heroku-playwright-buildpack`.
56+
57+
**2. The script doesn't work for me. What do I do?**
58+
59+
Here are a few things to consider:
60+
- This buildpack is meant for Python Playwright and was developed with Chromium in mind. Any other browser/Playwright distro is not tested by me so I offer no guarantees there
61+
- Verify that the installation is successful. Look at the build logs, you should see progress bar for the executable installations
62+
- You can also `heroku run bash` into the dyno and search for the installation folder to verify that the exe are downloaded.
63+
64+
# Notes
65+
This was developed for usage with `playwright` for Python
2166

bin/compile

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@
44
set -e
55

66
BUILD_DIR=$1
7+
ENV_DIR=$3
78

89
# modify the installation location
9-
if [ -z "$INSTALL_PATH" ]; then
10-
INSTALL_PATH="/browsers"
10+
if [ -z "$BUILDPACK_BROWSERS_INSTALL_PATH" ]; then
11+
BUILDPACK_BROWSERS_INSTALL_PATH="/browsers"
1112
fi
1213

13-
export PLAYWRIGHT_BROWSERS_PATH=$BUILD_DIR/$INSTALL_PATH
14-
echo "-----> Install path is $INSTALL_PATH"
14+
if [ -z "$PLAYWRIGHT_BUILDPACK_BROWSERS" ]; then
15+
SUPPORTED_BROWSERS=${PLAYWRIGHT_BUILDPACK_BROWSERS:-chromium,firefox,webkit}
16+
echo "Installing Playwright dependencies (env: PLAYWRIGHT_BUILDPACK_BROWSERS) for $SUPPORTED_BROWSERS."
17+
fi
18+
19+
export PLAYWRIGHT_BROWSERS_PATH=$BUILD_DIR/$BUILDPACK_BROWSERS_INSTALL_PATH
20+
echo "-----> BROWSERS_INSTALL_PATH is $BUILDPACK_BROWSERS_INSTALL_PATH"
1521
echo "-----> Browsers will be installed in $PLAYWRIGHT_BROWSERS_PATH"
16-
playwright install chromium
17-
echo "-----> Installation done"
22+
echo "-----> Formatted values are ${SUPPORTED_BROWSERS/,/ }"
23+
playwright install ${SUPPORTED_BROWSERS/,/ }
24+
echo "-----> Installation done"
25+
26+
# export the file path as a ENV
27+
CHROMIUM_EXECUTABLE_PATH=${find ~+ -type f -name "chrome"}
28+
FIREFOX_EXECUTABLE_PATH=${find ~+ -type f -name "geckodriver"}
29+
WEBKIT_EXECUTABLE_PATH=${find ~+ -type f -name "webkit"}
30+
31+
if [ -v "$CHROMIUM_EXECUTABLE_PATH" ]; then
32+
echo $CHROMIUM_EXECUTABLE_PATH > $ENV_DIR/CHROMIUM_EXECUTABLE_PATH
33+
fi

0 commit comments

Comments
 (0)