Build hooks

Run custom build scripts
Airplane allows you to run custom build scripts before and after dependencies are installed. This lets you to customize your task's environment and perform additional setup steps your tasks may require.
  • To run a script before dependency installation, create a file with the name airplane_preinstall.sh within your task root.
  • To run a script after dependency installation, create a file with the name airplane_postinstall.sh within your task root.
Airplane runs your custom build scripts as executables. If there are multiple airplane_preinstall.sh or airplane_postinstall.sh files found in directories between your task definition and task root, the file closest to your task definition will be used.

Example: Web scraping with selenium

Let's walk through an example of how we can use custom build scripts to set up selenium for web scraping in Python.
  1. We first need a requirements.txt file that specifies the Python selenium dependency.
Copied
1
selenium==4.4.3
  1. Then we add our custom build script called airplane_postinstall.sh that installs Firefox and geckodriver. Both are needed for selenium.
bash
Copied
1
#!/bin/bash
2
3
set -euo pipefail
4
5
GECKODRIVER_VER=v0.31.0
6
FIREFOX_VER=91.0
7
8
apt update \
9
&& apt upgrade -y \
10
&& apt install -y
11
12
# Install firefox
13
apt install -y \
14
firefox-esr \
15
libx11-xcb1 \
16
libdbus-glib-1-2 \
17
&& curl -sSLO https://download-installer.cdn.mozilla.net/pub/firefox/releases/${FIREFOX_VER}/linux-x86_64/en-US/firefox-${FIREFOX_VER}.tar.bz2 \
18
&& tar -jxf firefox-* \
19
&& mv firefox /opt/ \
20
&& chmod 755 /opt/firefox \
21
&& chmod 755 /opt/firefox/firefox
22
23
# Install geckodriver
24
curl -sSLO https://github.com/mozilla/geckodriver/releases/download/${GECKODRIVER_VER}/geckodriver-${GECKODRIVER_VER}-linux64.tar.gz \
25
&& tar zxf geckodriver-*.tar.gz \
26
&& mv geckodriver /usr/bin/
  1. Lastly, we add the Python script, selenium_demo.py.
python
Copied
1
from selenium import webdriver
2
from selenium.webdriver import FirefoxOptions
3
4
def main():
5
opts = FirefoxOptions()
6
opts.add_argument("--headless")
7
with webdriver.Firefox(options=opts) as driver:
8
driver.get("https://airplane.dev")
9
return {"page_source_len": len(driver.page_source)}
For the full source code, see the python_selenium_example template on GitHub. You can make a local copy of the template with the Airplane CLI:
Copied
1
airplane init --template=python_selenium_example