Skip to content

Commit e32e9f3

Browse files
committed
Allow to specify variable for the PR by setting a json inside the description
1 parent 0585f63 commit e32e9f3

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

copier.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ org_name:
5454
# but allow to change it manually
5555
when: false
5656

57+
use_variable_from_description:
58+
type: bool
59+
help: Allow to pass advanced variable from PR description
60+
5761
next_templates:
5862
help: "Next templates to run. template path args"
5963
# syntax:
@@ -89,3 +93,4 @@ _skip_if_exists:
8993
- clear-prod.secrets.docker-compose.yml
9094
- secrets.docker-compose.yml
9195
- .copier-answers-personal.yml
96+
- .gitlab/merge_request_templates/with_variable.md

src/.gitlab-ci.yml.jinja

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ stages:
5656
before_script:
5757
# TODO: manage mulitple preprod ?
5858
# ref-commit slug ?
59+
{% if use_variable_from_description %}
60+
- ./bin/extract_mr_variable_env.py
61+
- cat mr_variable.sh
62+
- source mr_variable.sh
63+
{% endif %}
5964
- echo $AK_WORKING_DIR
6065
- echo $AK_BASE_CACHE_DIR
6166
- echo $AK_IS_MR
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
import json
3+
import os
4+
5+
# pylint: disable=print-used
6+
7+
8+
description = os.getenv("CI_MERGE_REQUEST_DESCRIPTION")
9+
with open("mr_variable.sh", "w") as f:
10+
if description and "# Variable" in description:
11+
data = description.split("#Variable")[-1].split("```")[1]
12+
try:
13+
data = json.loads(data)
14+
except Exception as e:
15+
raise Exception(f"Fail to read the json data are \n {data} \n error: {e}")
16+
for key, value in data.items():
17+
if isinstance(value, (dict, list)):
18+
# pass complex variable as a string in json
19+
value = json.dumps(value)
20+
f.writelines(f"export {key.upper()}='{value}'\n")
21+
else:
22+
print("Extract MR variable: No variable found or invalid description")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Description
2+
3+
La variable "foo" peut etre personalisé et sera passé dans les variables d'environement de cette PR
4+
5+
Veuillez personnaliser les variables à personnaliser avant de commiter ce fichier
6+
7+
# Variable
8+
9+
```
10+
{
11+
"foo": "customisation",
12+
}
13+
14+
```

0 commit comments

Comments
 (0)