CI/CD workflows

Use the following files as a starting point for your automatic deployments. We have included the minimum you need to make your deployments, and hopefully you find them easy to extend.

We use Poetry in our examples but you can adapt to your favorite dependency manager, or use requirements.txt files directly.

Have a look at the deployments guide to get a better understanding of what these files are doing.

Gitlab

# .gitlab-ci.yml
#
# The environment variables can be configured at:
# https://gitlab.com/{YOU}/{YOUR_PROJECT}/-/settings/ci_cd
#
# Microsoft has some very useful Docker images for dev usage
# that include `pipx` and other python tools.
image: mcr.microsoft.com/vscode/devcontainers/python:3.9

# Enable Docker in Docker, and configure it.
# https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#docker-in-docker-with-tls-enabled
services:
  - docker:20-dind
variables:
  DOCKER_HOST: tcp://docker:2376
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_TLS_VERIFY: 1
  DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"

python_deploy:
  stage: deploy
  # Run only when commiting to "main"
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
  before_script:
    # We need a requirements.txt file for the dependencies.
    # This step is not required if you have a requirements.txt file already.
    - pipx run poetry export -f requirements.txt -o requirements.txt --without-hashes

    # Install the python-deploy cli
    - pipx install pd_cli~=1.0

  script:
    # Build the docker image
    - python-deploy build python-deploy:release
    # Deploy the docker image
    - python-deploy deploy python-deploy:release

    # Do you have migrations or additional commmands?
    # You can --wait for the deploy to finish to run them:
    #
    # - python-deploy deploy python-deploy:release --wait
    # - python-deploy run -- python3 manage.py migrate

Github

# .github/workflows/python_deploy.yml
#
# The environment variables can be configured at:
# https://github.com/{YOU}/{YOUR_PROJECT}/settings/secrets/actions

# Run only when commiting to "main"
on:
  push:
    branches:
      - main

jobs:
  python_deploy:
    # Microsoft has some very useful Docker images for dev usage
    # that include `pipx` and other python tools.
    container: mcr.microsoft.com/vscode/devcontainers/python:3.9
    name: Build and Deploy with PythonDeploy
    runs-on: ubuntu-latest
    env:
      PD_API_KEY: ${{ secrets.PD_API_KEY }}
      PD_APP_ID: ${{ secrets.PD_APP_ID }}
    steps:
      - uses: actions/[email protected]
      # We need a requirements.txt file for the dependencies.
      # This step is not required if you have a requirements.txt file already.
      - run: pipx run poetry export -f requirements.txt -o requirements.txt --without-hashes

      # Install the python-deploy cli
      - run: pipx install "pd_cli~=1.0"

      # Build the docker image
      - run: python-deploy build python-deploy:release
      # Deploy the docker image
      - run: python-deploy deploy python-deploy:release

      # Do you have migrations or additional commmands?
      # You can --wait for the deploy to finish to run them:
      #
      # - run: python-deploy deploy python-deploy:release --wait
      # - run: python-deploy run -- python3 manage.py migrate

Something missing? → [email protected]