Hello @Geoffrey_S ,
Here is an example Python script to do what you want.
import requests
import os
import time
# Qovery API configuration
API_TOKEN = os.environ.get("QOVERY_API_TOKEN") # Use Environment variable
API_BASE_URL = "https://api.qovery.com"
APPLICATION_ID = [("app_id", "branch_name"), ("app_id", "branch_name")] # You can pass multiple application at once
ENVIRONMENT_ID ="environment_id"
headers = {
"Authorization": f"Token {API_TOKEN}",
"Content-Type": "application/json"
}
# Get the latest commit id for your application
def get_latest(app_info):
app_id = app_info[0]
branch_name = app_info[1]
url = f"{API_BASE_URL}/application/{app_id}/commit"
# Parameters for the request
params = {
"branch": branch_name,
"limit": 1
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
commits = response.json().get('results', [])
if commits:
commit = commits[0]
print(f"App_id: {app_id}, Latest Commit ID: {commit['git_commit_id']}")
return commit['git_commit_id']
else:
print("No commits found for the specified branch.")
return False
else:
print(f"Failed to fetch the latest commit: {response.status_code} - {response.text}")
return False
# Deploy your applications
def deploy_apps(deploy_target):
url = f"{API_BASE_URL}/environment/{ENVIRONMENT_ID}/service/deploy"
# Create the list of applications and commit ID
data= {
"applications": [
{
"application_id": app_id,
"git_commit_id": commit_id
}
for app_id, commit_id in deploy_target
]
}
# Do the deploy
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
print("Deploy OK")
else:
print(response.status_code)
print(response.text)
def deploy_latest():
deploy_target = []
for app_id in APPLICATION_ID:
latest_commit = get_latest(app_id)
if not latest_commit:
print("Error, failed to retrieve commits or no commit found for the current branch")
else:
deploy_target.append((app_id[0], latest_commit))
deploy_apps(deploy_target)
if __name__ == "__main__":
if not API_TOKEN:
print("Please set the QOVERY_API_TOKEN environment variable")
else:
deploy_latest()
The only import not part of the core libraries is requests
.
It is possible to install requests
using pip. The documentation is here.
Here is a quick description of the script:
First, you have your variables:
# Qovery API configuration
API_TOKEN = os.environ.get("QOVERY_API_TOKEN") # Use Environment variable
API_BASE_URL = "https://api.qovery.com"
APPLICATION_ID = [("app_id", "branch_name"), ("app_id", "branch_name")] # You can pass multiple application at once
ENVIRONMENT_ID ="environment_id"
You must pass QOVERY_API_TOKEN
as an Environment variable.
APPLICATION_ID
is an array composed of application IDs and corresponding Git branches.
ENVIRONMENT_ID
is the ID of your environment.
Then the script is in 2 parts:
get_latest(app_id)
is a function that retrieves the latest commit ID for an application
deploy_apps(deploy_target)
is a function that deploys all your applications in an environment to the passed commit ID.
If you create a Cron Job using this script, you should be able to update your applications to the latest commit when you want.
Please let me know if this was helpful for you and if you need any more assistance with this topic.
Regards,
Charles-Edouard