diff --git a/copier.yml b/copier.yml index cffdfe5..65fe573 100644 --- a/copier.yml +++ b/copier.yml @@ -54,6 +54,10 @@ org_name: # but allow to change it manually when: false +use_variable_from_description: + type: bool + help: Allow to pass advanced variable from PR description + next_templates: help: "Next templates to run. template path args" # syntax: @@ -89,3 +93,4 @@ _skip_if_exists: - clear-prod.secrets.docker-compose.yml - secrets.docker-compose.yml - .copier-answers-personal.yml + - .gitlab/merge_request_templates/with_variable.md diff --git a/src/.gitlab-ci.yml.jinja b/src/.gitlab-ci.yml.jinja index b6bea26..9d07197 100644 --- a/src/.gitlab-ci.yml.jinja +++ b/src/.gitlab-ci.yml.jinja @@ -56,6 +56,11 @@ stages: before_script: # TODO: manage mulitple preprod ? # ref-commit slug ? +{% if use_variable_from_description %} + - ./bin/extract_mr_variable_env.py + - cat mr_variable.sh + - source mr_variable.sh +{% endif %} - echo $AK_WORKING_DIR - echo $AK_BASE_CACHE_DIR - echo $AK_IS_MR diff --git a/src/bin/{% if use_variable_from_description %}extract_mr_variable_env.py{% endif %} b/src/bin/{% if use_variable_from_description %}extract_mr_variable_env.py{% endif %} new file mode 100755 index 0000000..f6c77e6 --- /dev/null +++ b/src/bin/{% if use_variable_from_description %}extract_mr_variable_env.py{% endif %} @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import json +import os + +# pylint: disable=print-used + + +description = os.getenv("CI_MERGE_REQUEST_DESCRIPTION") +with open("mr_variable.sh", "w") as f: + if description and "# Variable" in description: + data = description.split("#Variable")[-1].split("```")[1] + try: + data = json.loads(data) + except Exception as e: + raise Exception(f"Fail to read the json data are \n {data} \n error: {e}") + for key, value in data.items(): + if isinstance(value, (dict, list)): + # pass complex variable as a string in json + value = json.dumps(value) + f.writelines(f"export {key.upper()}='{value}'\n") + else: + print("Extract MR variable: No variable found or invalid description") diff --git a/src/{% if use_variable_from_description %}.gitlab{% endif %}/merge_request_templates/with_variable.md b/src/{% if use_variable_from_description %}.gitlab{% endif %}/merge_request_templates/with_variable.md new file mode 100644 index 0000000..74bf594 --- /dev/null +++ b/src/{% if use_variable_from_description %}.gitlab{% endif %}/merge_request_templates/with_variable.md @@ -0,0 +1,14 @@ +# Description + +La variable "foo" peut etre personalisé et sera passé dans les variables d'environement de cette PR + +Veuillez personnaliser les variables à personnaliser avant de commiter ce fichier + +# Variable + +``` +{ + "foo": "customisation", +} + +```