# Configure Your Python build & deploy pipeline Zerops provides a customizable build and runtime environment for your Python application. ## Add zerops.yaml to your repository Start by adding `zerops.yaml` file to the **root of your repository** and modify it to fit your application: ```yaml zerops: # define hostname of your service - setup: app # ==== how to build your application ==== build: # REQUIRED. Set the base technology for the build environment: base: python@latest # OPTIONAL. Set the operating system for the build environment. # os: ubuntu # OPTIONAL. Customize the build environment by installing additional packages # or tools to the base build environment. # prepareCommands: # - sudo apt-get something # - curl something else # REQUIRED. Select which files / folders to deploy after # the build has successfully finished deployFiles: - app.py # OPTIONAL. Copy files from build container to runtime container. addToRunPrepare: - requirements.txt # OPTIONAL. Which files / folders you want to cache for the next build. # Next builds will be faster when the cache is used. # cache: file.txt # ==== how to run your application ==== run: # OPTIONAL. Sets the base technology for the runtime environment: base: python@latest # OPTIONAL. Sets the internal port(s) your app listens on: ports: # port number - port: 8000 # OPTIONAL. Customize the runtime Python environment by installing additional # dependencies to the base Python runtime environment. # prepareCommands: # - python3 -m pip install --ignore-installed -r requirements.txt # OPTIONAL. Run one or more commands each time a new runtime container # is started or restarted. These commands are triggered before # your Python application is started. # initCommands: # - rm -rf ./cache # REQUIRED. Your Python application start command start: python3 app.py ``` The top-level element is always `zerops`. ### Setup The first element `setup` contains the **hostname** of your service. A runtime service with the same hostname must exist in Zerops. Zerops supports the definition of multiple runtime services in a single `zerops.yaml`. This is useful when you use a monorepo. Just add multiple setup elements in your `zerops.yaml`: ```yaml zerops: # definition for app service - setup: app # optional build: ... # optional deploy: ... # required run: ... # definition for api service - setup: api # optional build: ... # optional deploy: ... # required run: ... ``` Each service configuration contains at least the `run` section. Optional `build` and `deploy` sections can be added to further customize your process. ## Build pipeline configuration ### base _REQUIRED._ Sets the base technology for the build environment. Following options are available for Python builds: ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: # REQUIRED. Sets the base technology for the build environment: base: python@latest ... ```
The base build environment contains Alpine 3.20, the selected major version of Python, [Zerops command line tool](/references/cli), `pip` and `git`.
:::info You can change the base environment when you need to. Just simply modify the `zerops.yaml` in your repository. ::: If you need to install more technologies to the build environment, set multiple values as a yaml array. For example: ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: # REQUIRED. Sets the base technology for the build environment: base: - python@latest prepareCommands: - zsc add go@latest ... ``` See the full list of supported [build base environments](/zerops-yaml/base-list#runtime-services). To customize your build environment use the [prepareCommands](#preparecommands) attribute. :::note Modifying the base technology will invalidate your build cache. See our [Build Cache Documentation](/features/build-cache) for more details about cache invalidation. ::: ### os _OPTIONAL._ Sets the operating system for the build environment. Following options are available: - `alpine` - `ubuntu` Default value is `alpine`. We are currently using following os version: - Alpine 3.20 - Ubuntu 24.04 :::caution The os version is fixed and cannot be customized. ::: :::note Changing the OS setting will invalidate your build cache. See our [Build Cache Documentation](/features/build-cache) for details about cache behavior. ::: ### prepareCommands _OPTIONAL._ Customizes the build environment by installing additional dependencies or tools to the base build environment. The base build environment contains: - Alpine 3.20 - selected version of Python defined in the [base](#base) attribute - [Zerops command line tool](/references/cli) - `pip` and `git` To install additional packages or tools add one or more prepare commands: ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: # REQUIRED. Set the base technology for the build environment: base: python@latest # OPTIONAL. Customize the build environment by installing additional packages # or tools to the base build environment. prepareCommands: - sudo apt install python3-pip # already installed for Python services ... ``` When the first build is triggered, Zerops will 1. create a build container 2. download your application code from your repository 3. run the prepare commands in the defined order The application code is available in the `/var/www` folder in your build container before the prepare commands are triggered. This allows you to use any file from your application code in your prepare commands (e.g. a configuration file). :::note These commands are skipped when using cached environment. Modifying `prepareCommands` will invalidate your build cache. See our [Build Cache Documentation](/features/build-cache) for details about cache invalidation. ::: #### Command exit code If any command fails, it returns an exit code other than 0 and the build is canceled. Read the [build log](/python/how-to/logs#build-log) to troubleshoot the error. If the command ends successfully, it returns the exit code 0 and Zerops triggers the following command. When all prepare commands are finished, your custom build environment is ready for the build phase. #### Run prepare commands as a single shell instance Use following syntax to run all commands in the same environment context. For example, if one command changes the current directory, the next command continues in that directory. When one command creates an environment variable, the next command can access it. ```yaml prepareCommands: - | sudo apt update sudo apt install python3-pip # already installed for Python services ``` #### Run prepare commands as a separate shell instances When the following syntax is used, each command is triggered in a separate environment context. For example, each shell instance starts in the home directory again. When one command creates an environment variable, it won't be available for the next command. ```yaml prepareCommands: - sudo apt update - sudo apt install python3-pip # already installed for Python services ``` ### deployFiles _REQUIRED._ Selects which files or folders will be deployed after the build has successfully finished. To filter out specific files or folders, use [`.deployignore`](#deployignore) file. ```yaml # REQUIRED. Select which files / folders to deploy after # the build has successfully finished deployFiles: - app.py ``` Determines files or folders produced by your build, which should be deployed to your runtime service containers. The path starts from the **root directory** of your project (the location of `zerops.yaml`). You must enclose the name in quotes if the folder or the file name contains a space. The files/folders will be placed into `/var/www` folder in runtime, e.g. `./src/assets/fonts` would result in `/var/www/src/assets/fonts`. #### Examples Deploys a folder, and a file from the project root directory: ```yaml deployFiles: - app.py ``` Deploys the whole content of the build container: ```yaml deployFiles: . ``` Deploys a folder, and a file in a defined path: ```yaml deployFiles: - ./path/to/file.txt - ./path/to/dir/ ``` #### How to use a wildcard in the path Zerops supports the `~` character as a wildcard for one or more folders in the path. Deploys all `file.txt` files that are located in any path that begins with `/path/` and ends with `/to/` ```yaml deployFiles: ./path/~/to/file.txt ``` Deploys all folders that are located in any path that begins with `/path/to/` ```yaml deployFiles: ./path/to/~/ ``` Deploys all folders that are located in any path that begins with `/path/` and ends with `/to/` ```yaml deployFiles: ./path/~/to/ ``` :::note Example By default, `./src/assets/fonts` deploys to `/var/www/src/assets/fonts`, keeping the full path. Adding `~`, like `./src/assets/~fonts`, shortens it to `/var/www/fonts` ::: #### .deployignore Add a `.deployignore` file to the root of your project to specify which files and folders Zerops should ignore during deploy. The syntax follows the same pattern format as [`.gitignore`](https://git-scm.com/docs/gitignore#_pattern_format). To ignore a specific file or directory path, start the pattern with a forward slash (`/`). Without the leading slash, the pattern will match files with that name in any directory. :::tip For consistency, it's recommended to configure both your `.gitignore` and `.deployignore` files with the same patterns. ::: Examples: ```yaml title="zerops.yaml" zerops: - setup: app build: deployFiles: ./ ``` ```text title=".deployignore" /src/file.txt ``` The example above ignores `file.txt` only in the root src directory. ```text title=".deployignore" src/file.txt ``` This example above ignores `file.txt` in ANY directory named `src`, such as: - `/src/file.txt` - `/folder2/folder3/src/file.txt` - `/src/src/file.txt` :::note `.deployignore` file also works with [`zcli service deploy`](/references/zcli/commands#deploy) command. ::: ### cache _OPTIONAL._ Defines which files or folders will be cached for the next build. ```yaml # OPTIONAL. Which files / folders you want to cache for the next build. # Next builds will be faster when the cache is used. cache: file.txt ``` The cache attribute helps optimize build times by preserving specified files between builds. The cache attribute supports the [~ wildcard character](#how-to-use-a-wildcard-in-the-path). Learn more about the [build cache system](/features/build-cache) in Zerops. ### envVariables _OPTIONAL._ Defines the environment variables for the build environment. Enter one or more env variables in following format: ```yaml zerops: # define hostname of your service - setup: app # ==== how to build your application ==== build: base: python@latest … # OPTIONAL. Defines the env variables for the build environment: envVariables: PYTHON_ENV: production DB_NAME: db DB_HOST: db DB_USER: db DB_PASS: ``` Read more about [environment variables](/python/how-to/env-variables) in Zerops. ## Runtime configuration ### base _OPTIONAL._ Sets the base technology for the runtime environment. If you don't specify the `run.base` attribute, Zerops keeps the current Python version for your runtime. Following options are available for Python builds: ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: # REQUIRED. Sets the base technology for the build environment: base: python@latest ... # ==== how to run your application ==== run: # OPTIONAL. Sets the base technology for the runtime environment: base: python@latest ... ```The base runtime environment contains Alpine 3.20, the selected major version of Python, [Zerops command line tool](/references/cli), `pip` and `git`.
:::info You can change the base environment when you need to. Just simply modify the `zerops.yaml` in your repository. ::: If you need to install more technologies to the runtime environment, set multiple values as a yaml array. For example: ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: # REQUIRED. Sets the base technology for the build environment: base: python@latest ... # ==== how to run your application ==== run: # OPTIONAL. Sets the base technology for the runtime environment: base: - python@latest prepareCommands: - zsc add go@latest ... ``` See the full list of supported [run base environments](/zerops-yaml/base-list). To customize your build environment use the `prepareCommands` attribute. ### os _OPTIONAL._ Sets the operating system for the runtime environment. Following options are available: - `alpine` - `ubuntu` Default value is `alpine`. We are currently using following os version: - Alpine 3.20 - Ubuntu 24.04 :::caution The os version is fixed and cannot be customised. ::: ### ports _OPTIONAL._ Specifies one or more internal ports on which your application will listen. Projects in Zerops represent a group of one or more services. Services can be of different types (runtime services, databases, message brokers, object storage, etc.). All services of the same project share a **dedicated private network**. To connect to a service within the same project, just use the service hostname and its internal port. For example, to connect to a Python service with hostname = "app" and port = 8000 from another service of the same project, simply use `app:8000`. Read more about [how to access a Python service](/references/networking/internal-access#basic-service-communication). Each port has following attributes:| Parameter | Description |
|---|---|
| port | Defines the port number. You can set any port number between 10 and 65435. Ports outside this interval are reserved for internal Zerops systems. |
| protocol | Optional. Defines the protocol. Allowed values are TCP or UDP. Default value is TCP. |
| httpSupport | Optional. httpSupport = true is the default setting for TCP protocol. Set httpSupport = false if a web server isn't running on the port. Zerops uses this information for the configuration of [public access](/features/access). httpSupport = true is available only in combination with the TCP protocol. |
The base Python environment contains Alpine 3.20, the selected major version of Python, [Zerops command line tool](/references/cli), `pip` and `git`. To install additional packages or tools add one or more prepare commands:
```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: ... # ==== how to run your application ==== run: # OPTIONAL. Customise the runtime environment by installing additional packages # or tools to the base Python runtime environment. prepareCommands: - python3 -m pip install --ignore-installed -r requirements.txt ... ``` When the first deploy with a defined prepare attribute is triggered, Zerops will 1. create a prepare runtime container 2. optionally: [copy selected folders or files from your build container](#copy-folders-or-files-from-your-build-container) 3. run the `prepareCommands` commands in the defined order #### Command exit code If any command fails, it returns an exit code other than 0 and the deploy is canceled. Read the [prepare runtime log](/python/how-to/logs#prepare-runtime-log) to troubleshoot the error. If the command ends successfully, it returns the exit code 0 and Zerops triggers the following command. When all `prepareCommands` commands are finished, your custom runtime environment is ready for the deploy phase. #### Cache of your custom runtime environment Some packages or tools can take a long time to install. Therefore, Zerops caches your custom runtime environment after the installation of your custom packages or tools is completed. When the second or following deploy is triggered, Zerops will use the custom runtime cache from the previous deploy if following conditions are met: 1. Content of the [build.addToRunPrepare](#copy-folders-or-files-from-your-build-container) and `run.prepareCommands` attributes didn't change from the previous deploy 2. The custom runtime cache wasn't invalidated in the Zerops GUI. To invalidate the Zerops runtime cache go to your service detail in Zerops GUI, choose **Service dashboard & runtime containers** from the left menu and click on the **Open pipeline detail** button. Then click on the **Clear runtime prepare cache** button. When the prepare cache is used, Zerops doesn't create a prepare runtime container and executes the deployment of your application directly. #### Single or separated shell instances You can configure your prepare commands to be run in a single shell instance or multiple shell instances. The format is identical to [build:prepareCommands](#preparecommands). ### Copy folders or files from your build containerThe prepare runtime container contains Alpine 3.20 the selected major version of Python, [Zerops command line tool](/references/cli), `pip` and `git`.
The prepare runtime container does not contain your application code nor the built application. If you need to copy some folders or files from the build container to the runtime container (e.g. a configuration file) use the `addToRunPrepare` attribute in the [build section](#build-pipeline-configuration). ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: ... addToRunPrepare: - requirements.txt # ==== how to run your application ==== run: # OPTIONAL. Customise the runtime environment by installing additional packages # or tools to the base Python runtime environment. prepareCommands: - python3 -m pip install --ignore-installed -r requirements.txt ... ``` In the example above Zerops will copy the `runtime-config.yaml` file from your build container **after the build has finished** into the new **prepare runtime** container. The copied files and folders will be available in the `/home/zerops` folder in the new prepare runtime container before the prepare commands are triggered. ### initCommands _OPTIONAL._ Defines one or more commands to be run each time a new runtime container is started or a container is restarted. ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: ... # ==== how to run your application ==== run: # OPTIONAL. Run one or more commands each time a new runtime container # is started or restarted. These commands are triggered before # your Python application is started. initCommands: - rm -rf ./cache ``` These commands are triggered in the runtime container before your Python application is started via the [start command](#start). Use init commands to clean or initialise your application cache or similar operations. :::caution The init commands will delay the start of your application each time a new runtime container is started (including the horizontal [scaling](/python/how-to/scaling) or when a runtime container is restarted). Do not use the init commands for customising your runtime environment. Use the [run:prepareCommands](#preparecommands-1) attribute instead. ::: #### Command exit code If any of the `initCommands` fails, it returns an exit code other than 0, but deploy is **not** canceled. After all init commands are finished, regardless of the status code, the application is started. Read the [runtime log](/python/how-to/logs#runtime-log) to troubleshoot the error. #### Single or separated shell instances You can configure your `initCommands` to be run in a single shell instance or multiple shell instances. The format is identical to [build:prepareCommands](#preparecommands). ### envVariables _OPTIONAL._ Defines the environment variables for the runtime environment. Enter one or more env variables in following format: ```yaml zerops: # define hostname of your service - setup: app # ==== how to run your application ==== run: # OPTIONAL. Defines the env variables for the runtime environment: envVariables: PYTHON_ENV: production DB_NAME: db DB_HOST: db DB_USER: db DB_PASS: ``` Read more about [environment variables](/python/how-to/env-variables) in Zerops. ### start _REQUIRED._ Defines the start command for your Python application. ```yaml zerops: # hostname of your service - setup: app # ==== how to build your application ==== build: ... # ==== how to run your application ==== run: # REQUIRED. Your Python application start command start: app.py ``` ### health check _OPTIONAL._ Defines a health check. `healthCheck` requires either one `httpGet` object or one `exec` object. #### httpGet Configures the health check to request a local URL using a HTTP GET method. Following attributes are available:| Parameter | Description |
|---|---|
| port | Defines the port of the HTTP GET request.
The readiness check will trigger a GET request on {'http://127.0.0.1:{port}/{path}'} |
| path | Defines the URL path of the HTTP GET request.
The readiness check will trigger a GET request on {'http://127.0.0.1:{port}/{path}'} |
| host | Optional. The readiness check is triggered from inside of your runtime container so it always uses the localhost 127.0.0.1. If you need to add a host to the request header, specify it in the host attribute. |
| scheme | Optional. The readiness check is triggered from inside of your runtime container so no https is required.
If your application requires a https request, set scheme: https |
| Parameter | Description |
|---|---|
| command | Defines a local command to be run. The command has access to the same [environment variables](/python/how-to/create#set-secret-environment-variables) as your Python application. A single string is required. If you need to run multiple commands create a shell script or, use a multiline format as in the example below. |
| Parameter | Description |
|---|---|
| port | Defines the port of the HTTP GET request.
The readiness check will trigger a GET request on {'http://127.0.0.1:{port}/{path}'} |
| path | Defines the URL path of the HTTP GET request.
The readiness check will trigger a GET request on {'http://127.0.0.1:{port}/{path}'} |
| host | Optional. The readiness check is triggered from inside of your runtime container so it always uses the localhost 127.0.0.1. If you need to add a host to the request header, specify it in the host attribute. |
| scheme | Optional. The readiness check is triggered from inside of your runtime container so no https is required.
If your application requires a https request, set scheme: https |
| Parameter | Description |
|---|---|
| command | Defines a local command to be run. The command has access to the same [environment variables](/python/how-to/create#set-secret-environment-variables) as your Python application. A single string is required. If you need to run multiple commands create a shell script or, use a multiline format as in the example below. |