Skip to main content

Build, push and deploy k8s services with single deploy.json file to provide common convention for multiple production services.

Project description

deploy

Build, push and deploy k8s services with single deploy.json file to provide common convention for multiple production services.

pip install python-deploy

Make your Docker setup deploy-enabled

First of all, create a file under deploy/deploy.json path pointing at your Dockerfile.

It's recommended to place your Dockerfile in a deploy/docker subdirectory.

{
    "fancy-service": {
        "docker": {
            "image": "certpl/fancy-service",
            "dockerfile": "deploy/docker/Dockerfile",
            "dir": "."
        }
    }
}

Then you can use deploy build to build your Docker image. Your image will be tagged using Git commit hash.

$ deploy build
[INFO] Building image certpl/fancy-service:a2f3d45a4ccfcee03a8a3941ec7177e394626cf2

If you want to provide alternative version tag, use --version. You can provide any tag you want, but few of them are special:

  • date uses current timestamp (e.g. v20200302170007)
  • commit is default
  • other strings are used "as is"

Use deploy push if you want to push (and build) image to Docker Registry or Docker Hub with all provided tags:

$ deploy push
[INFO] Building image certpl/fancy-service:a2f3d45a4ccfcee03a8a3941ec7177e394626cf2
[INFO] Pushing image certpl/fancy-service:a2f3d45a4ccfcee03a8a3941ec7177e394626cf2

Make your k8s deployment deploy-enabled

Enhance your deploy/deploy.json with configuration of k8s deployment.

Deploy supports two environments: k8s (production) and k8s-staging (staging).

A complete example of a deploy.json file is presented below:

{
    "fancy-service": {
        "docker": {
            "image": "certpl/fancy-service",
            "dockerfile": "deploy/docker/Dockerfile",
            "dir": "."
        },
        "k8s": {
            "namespace": "service-prod",
            "deployment": "deployment-fancy-service",
            "container": "container-fancy-service"
        },
        "k8s-staging": {
            "namespace": "service-st",
            "deployment": "deployment-fancy-service",
            "container": "container-fancy-service"
        }
    }
}

It's recommended to place your Kubernetes configuration files in the deploy/k8s and deploy/k8s-staging subdirectories.

This enables you to use deploy staging and deploy production commands.

  • deploy staging builds and pushes Docker image with version tag and latest tag. After all, updates the image name used by staging container to new version.

  • deploy production does the same thing, but images are additionally tagged as master. Then it updates the production container.

$ deploy production
[INFO] Building image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7
[INFO] Pushing image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7
[INFO] Tagging image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7 as certpl/fancy-service:master
[INFO] Tagging image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7 as certpl/fancy-service:latest
[INFO] Setting image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7 for k8s environment

If you don't want to rebuild your Docker images and need just to pull them from the Docker Registry, you can use deploy pull and deploy production --deploy-only switch

$ docker pull
[INFO] Pulling image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7
$ deploy production --deploy-only
[INFO] Tagging image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7 as certpl/fancy-service:master
[INFO] Tagging image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7 as certpl/fancy-service:latest
[INFO] Setting image certpl/fancy-service:7f6dd7010dba1ffdaeb32875e0f71c30c9810df7 for k8s environment

Support for multiple services

If your app is built using multiple containers, just specify more services as top-level keys of deploy.json.

{
    "fancy-service": {
        ...
    },
    "fancy-service-web": {
        ...
    }
}

Deploy will build, push and deploy them all (unless you explicitly select services using --service/-s option).

Make your Gitlab CI deploy-enabled

You can automate all these steps with CI/CD. Example .gitlab-ci.yml file is presented below:

image: certpl/deploy

services:
  - docker:dind

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script:
    - git submodule init
    - git submodule update --recursive
    # Build and push images to Docker Registry
    - deploy push

test:
  stage: test
  script:
    # Pull images and run unit tests
    - deploy pull
    - deploy run -- python -m unittest discover

deploy:
  stage: deploy
  only:
  - master
  script:
    # Set default token, pull images and deploy them
    - kubernetes_use_token https://kapi.example.com "$KUBE_TOKEN"
    - deploy pull
    - deploy production --deploy-only

If you don't have tests and you just want to build and deploy everything in one step: invoke deploy production

Full usage

Keep in mind that some optional arguments are only relevant for some commands

usage: deploy [-h] {build,push,pull,staging,production,image,run} ...

Deploy the application.

positional arguments:
  {build,push,pull,staging,production,image,run}
                        Deploy commands
    build               Only build images
    push                Build and push images
    pull                Pull images from registry
    staging             Build, push and deploy images to the staging
                        environment
    production          Build, push and deploy images to the PRODUCTION
                        environment
    image               Show image names
    run                 Run interactive command for service images

optional arguments:
  -h, --help            show this help message and exit

  --tag TAG, -t TAG     Alternative tags for image
  --no-cache            Pass --no-cache to docker build
  --service SERVICE, -s SERVICE
                        Specify services to perform action (default: all)
  --force, -f           Don't perform check-ups, force deploy (not
                        recommended)
  --verbose, -v         Print spawned subcommands and their outputs
  --version {commit,date,latest}
                        Alternative version tag

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

python_deploy-3.0.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file python_deploy-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_deploy-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.6.14

File hashes

Hashes for python_deploy-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7013e144842ef173e897150a7c878b89a345b09946977262b61c09f82e4645c7
MD5 a86e0efd08a74a5f1629a74095d0542e
BLAKE2b-256 e7b58f4e5aa6d83955cfe93d87fe8fb6db8de851ffec256024052b4a579edb81

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page