Skip to content

Latest commit

 

History

History
118 lines (102 loc) · 3.47 KB

language-python_5bdc36652c7d3a01757aba24.md

File metadata and controls

118 lines (102 loc) · 3.47 KB

This guide covers configuring Python projects on Semaphore. If you’re new to Semaphore please read our Guided tour first.

Supported Python versions

Semaphore provides major Python versions and tools preinstalled. You can find information about them in the Ubuntu image reference.

Python 2.7 is the default version. This can be switched to 3.7 with sem-version. Here's an example:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - sem-version python 3.7
      jobs:
        - name: Tests
          commands:
            - python --version

Dependency caching

You can use Semaphore's cache command to store and load pipenv's virtualenv. This requires setting the PIPENV_VENV_IN_PROJECT environment variable. In the following configuration example, we install dependencies and warm the cache in the first block, then use the cache in subsequent blocks.

version: v1.0
 name: Python Example
 agent:
   machine:
     type: e1-standard-2
     os_image: ubuntu1804
 
 blocks:
   - name: Install dependencies
     task:
       env_vars:
         - name: PIPENV_VENV_IN_PROJECT
           value: "true"
       prologue:
         commands:
           - sudo pip install pipenv
           - checkout
       jobs:
         - name: Install and cache dependencies
           commands:
             - cache restore pipenv-$(checksum Pipfile.lock)
             # --deploy also checks python version requirements
             - pipenv install --dev --deploy
             - cache store pipenv-$(checksum Pipfile.lock) .venv
   - name: Tests
     task:
       env_vars:
         - name: PIPENV_VENV_IN_PROJECT
           value: "true"
       prologue:
         commands:
           - sudo pip install pipenv
           - checkout
           - cache restore pipenv-$(checksum Pipfile.lock)
       jobs:
         - name: Everything
           commands:
             # assuming you have "test" in your Pipfile scripts
             - pipenv run test

If you need to clear cache for your project, launch a debug session and execute cache clear or cache delete <key>.

Environment Variables

Semaphore doesn't set project specific environment variables like TESTING used in Flask. You can set these at the task level.

blocks:
  - name: Tests
    task:
      env_vars:
        - name: TESTING
          value: "1"
      jobs:
        - name: Everything
          commands:
            - python test.py

C-Extensions & system dependencies

Projects may need system packages in order to install pips like postgres. Semaphore provides full sudo access so you may install all required packages. Here's an example of installing the postgres pip:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - sudo apt-get update && sudo apt-get install -y libpq-dev
          - pip install postgres
      jobs:
        - name: Everything
          commands:
            - python test.py