Created
December 5, 2024 18:09
-
-
Save imWildCat/ca04ca2f98daadda59290bc9c788bdde to your computer and use it in GitHub Desktop.
migrate gitlab projects: variables and secure files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import gitlab | |
# GitLab API configuration | |
GITLAB_TOKEN = os.getenv("GITLAB_TOKEN") | |
GITLAB_URL = "https://xxx.gitlab.com" | |
gl = gitlab.Gitlab(url=GITLAB_URL, private_token=GITLAB_TOKEN) | |
# Project paths | |
SOURCE_PROJECT = "org_name/app-v2" | |
TARGET_PROJECT = "org_name/app-v3" | |
def get_project(project_path): | |
return gl.projects.get(project_path) | |
def migrate_variables(source_project, target_project): | |
variables = source_project.variables.list(all=True) | |
for var in variables: | |
target_project.variables.create( | |
{ | |
"key": var.key, | |
"value": var.value, | |
"protected": var.protected, | |
"masked": var.masked, | |
"variable_type": var.variable_type, | |
} | |
) | |
print(f"Migrated variable: {var.key}") | |
def migrate_secure_files(source_project, target_project): | |
secure_files = source_project.secure_files.list(all=True) | |
for file in secure_files: | |
# Download file with actual content | |
with open(file.name, "wb") as f: | |
file.download(streamed=True, action=f.write) | |
# Upload to target project | |
with open(file.name, "rb") as f: | |
target_project.secure_files.create({"name": file.name, "file": f}) | |
# Cleanup | |
os.remove(file.name) | |
print(f"Migrated secure file: {file.name}") | |
def main(): | |
try: | |
source_project = get_project(SOURCE_PROJECT) | |
target_project = get_project(TARGET_PROJECT) | |
# print("Migrating variables...") | |
# migrate_variables(source_project, target_project) | |
print("Migrating secure files...") | |
migrate_secure_files(source_project, target_project) | |
print("Migration completed successfully!") | |
except Exception as e: | |
print(f"Error during migration: {str(e)}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment