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.
GitPythonLibrary: The script uses theGitPythonlibrary to interact with Git repositories. Install it using the following command in your terminal:
pip install GitPython
requestslibrary: The script also uses therequestslibrary 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.pyscript. - 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 ishttps://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_SLUGargument is provided:- If an environment with the given
ENV_SLUGexists, the script will deploy the specified environment. - If an environment with the given
ENV_SLUGdoes not exist, the script will create a new environment with the providedENV_SLUGby copying from the default source environment, which is the"live"environment, and then deploy the new environment.
- If an environment with the given
Deploying environment with SOURCE_ENV_SLUG:
-
If the
SOURCE_ENV_SLUGargument is also provided along with theENV_SLUGargument:- The script will create a new environment with the specified
ENV_SLUGby copying from the given source environment (specified bySOURCE_ENV_SLUG) and deploy the new environment. - Note that an environment with the given
SOURCE_ENV_SLUGmust exist for this operation. Should such an environment not exist, the script will terminate and display an error message.
- The script will create a new environment with the specified
Deploying Environment with BRANCH:
-
If the
BRANCHargument is also provided: * If noENV_SLUGis provided or if theENV_SLUGalready exists, the script will report an error message: "Environment exists. Please do not provide a branch argument."-
If a branch with the provided
BRANCHname does not exist, the script will:- Use the provided
REPO_PATHto create a branch and push it to the remote repository. - If
REPO_PATHis not provided, the script will exit with an error message: "Error: repository_path must be provided when creating a new branch."
- Use the provided
-
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.
- The new environment's branch will be updated with the provided
-
By following these steps, you can effectively utilize the ci-cd script to streamline the process of deploying Divio application environments with customized settings.