Skip to content

Instantly share code, notes, and snippets.

@Fooftilly
Created February 16, 2023 14:21
Show Gist options
  • Save Fooftilly/3e10ab9f1211ea6ba3f272c070314850 to your computer and use it in GitHub Desktop.
Save Fooftilly/3e10ab9f1211ea6ba3f272c070314850 to your computer and use it in GitHub Desktop.
Download latest release from Gitlab. You would need to modify <GITLAB_PROJECT_ID> and <GITLAB_PROJECT_RELEASE_FILE> for script to work.
#!/usr/bin/python
import requests
import json
# Replace <GITLAB_PROJECT_ID> with the ID of your GitLab project
url = "https://gitlab.com/api/v4/projects/<GITLAB_PROJECT_ID>/releases"
# Get the latest release
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Load the JSON response
data = json.loads(response.text)
# Get the first (latest) release
latest_release = data[0]
# Get the assets for the latest release
assets = latest_release["assets"]["links"]
# Find the asset with the name starting with "<GITLAB_PROJECT_RELEASE_FILE>"
target_asset = None
for asset in assets:
if asset["name"].startswith("<GITLAB_PROJECT_RELEASE_FILE"):
target_asset = asset
break
# Check if the target asset was found
if target_asset:
# Get the download URL for the target asset
download_url = target_asset["url"]
# Download the file
response = requests.get(download_url)
# Check if the download was successful
if response.status_code == 200:
# Write the file to disk
with open(target_asset["name"], "wb") as f:
f.write(response.content)
print(f"Downloaded {target_asset['name']}")
else:
print(f"Failed to download the file: {response.text}")
else:
print("Failed to find the target asset")
else:
print(f"Failed to get the latest release: {response.text}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment