Skip to main content

How to simplify your Divio application deployment with a Python script

The ci-cd.py script enables you to deploy a Divio application environment with customizable options. You can specify an environment slug, a branch, and other parameters to facilitate the deployment process. The script automates the creation of environments, branch handling, and triggering of deployments.

Requirements

Before running the script, ensure you have the following requirements in place:

  • Python 3.x: The script requires Python 3.x. If it's not installed, download it from the official Python website.
  • GitPython Library: The script uses the GitPython library to interact with Git repositories. Install it using the following command in your terminal:
pip install GitPython
  • requests library: The script also uses the requests library to make HTTP requests to the Divio API. Install the library by running the following command in your terminal:
pip install requests

Running the Script

  • Download the Script: Save the ci-cd.py Python script to a directory on your system.
  • Navigate to Script Directory: Open a terminal and navigate to the directory where you saved the ci-cd.py script.
  • Execute the Script: Run the script using the following command, providing the required command-line arguments:
python ci-cd.py APP_UUID API_TOKEN [--env_slug ENV_SLUG] [--branch BRANCH] [--repository_path
REPO_PATH] [--source_env_slug SOURCE_ENV_SLUG]
  • Replace placeholders:

    Mandatory arguments:

    • APP_UUID: Application UUID. Extract the UUID from the Divio application dashboard URL, after the /app/ part. For example, if the application URL is https://control.divio.com/o/{uuid}/app/{uuid}/, the application UUID would be "{uuid}"`.
    • API_TOKEN: Your Divio API token.

    Optional arguments:

    • --env_slug ENV_SLUG: Environment slug (default: "test").
    • --branch BRANCH: Branch name.
    • --repository_path REPO_PATH: Local repository path.
    • --source_env_slug SOURCE_ENV_SLUG: Source environment slug for copying (default: "live").

Upon execution, the script dynamically manages the deployment process according to the provided arguments. It displays real-time deployment status messages in the terminal, offering clear insights into the success or failure of the deployment process.

Script behaviour based on argument combinations

Deploying default environment, the "test":

  • If no additional arguments are provided, the script will deploy the default environment, which is the "test" environment.

Deploying environment with ENV_SLUG:

  • If the ENV_SLUG argument is provided:

    • If an environment with the given ENV_SLUG exists, the script will deploy the specified environment.
    • If an environment with the given ENV_SLUG does not exist, the script will create a new environment with the provided ENV_SLUG by copying from the default source environment, which is the "live" environment, and then deploy the new environment.

Deploying environment with SOURCE_ENV_SLUG:

  • If the SOURCE_ENV_SLUG argument is also provided along with the ENV_SLUG argument:

    • The script will create a new environment with the specified ENV_SLUG by copying from the given source environment (specified by SOURCE_ENV_SLUG) and deploy the new environment.
    • Note that an environment with the given SOURCE_ENV_SLUG must exist for this operation. Should such an environment not exist, the script will terminate and display an error message.

Deploying Environment with BRANCH:

  • If the BRANCH argument is also provided: * If no ENV_SLUG is provided or if the ENV_SLUG already exists, the script will report an error message: "Environment exists. Please do not provide a branch argument."

    • If a branch with the provided BRANCH name does not exist, the script will:

      • Use the provided REPO_PATH to create a branch and push it to the remote repository.
      • If REPO_PATH is not provided, the script will exit with an error message: "Error: repository_path must be provided when creating a new branch."
    • If a new environment is created by copying:

      • The new environment's branch will be updated with the provided BRANCH. The new environment will then be deployed.

By following these steps, you can effectively utilize the ci-cd script to streamline the process of deploying Divio application environments with customized settings.