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:
    # If your Dockerfile installs dependencies from requirements.txt,
    # create one here.
    - 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

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/checkout@v2
      # If your Dockerfile installs dependencies from requirements.txt,
      # create one here.
      - 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40