-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbump-tag.py
executable file
·104 lines (85 loc) · 3.42 KB
/
bump-tag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
import subprocess
import tempfile
import yaml
from jinja2 import Template
context = {
"docker_tag_name": "v1.14.1_sunet-${GIT_COMMIT}",
"branch_name": "v1.14.1",
"repository_url": "https://github.com/Uninett/Argus-frontend.git"
}
def get_commit():
# Capture the most recent commit hash using `git log --show-signature -1`
result = subprocess.run(
"git log --show-signature -1",
shell=True,
check=True,
capture_output=True,
text=True,
)
# Extract the commit hash from the git log output
git_commit = None
for line in result.stdout.splitlines():
if line.startswith("commit"):
git_commit = line.split()[1] # Commit hash is the second word
break
return git_commit
def get_template(filename=".jenkins.yaml.jinja"):
template_content = None
# Read the Jinja2 template YAML file
with open(filename, "r") as template_file:
template_content = template_file.read()
return template_content
# Use Python's tempfile module to create a temporary directory in memory
with tempfile.TemporaryDirectory() as temp_dir:
# Clone the repository with --bare to save bandwidth
subprocess.run(
f"git clone --bare {context['repository_url']} {temp_dir} > /dev/null 2>&1",
shell=True,
check=True,
)
# Run the 'git for-each-ref' command to get the tags sorted by date
result = subprocess.run(
f"git -C {temp_dir} for-each-ref --sort=-creatordate --format '%(refname:short)' refs/tags",
shell=True,
check=True,
capture_output=True,
text=True,
)
# Split the output into a list of tags
tags = result.stdout.splitlines()
len_tags = len(tags)
# Print the tags
print(f"Tags from {context['repository_url']} listed by creation date:")
for index, tag in enumerate(tags):
print(f"{index+1}. {tag}")
tag_number = input("Choose the tag you want to build: ")
tag_number = int(tag_number)
assert (
tag_number >= 1 and tag_number <= len_tags + 1
), "The number should be one in the list above"
print(f"You have choosed: {tag_number}, {tags[tag_number-1]}")
git_commit = get_commit()[:8]
context['branch_name'] = tags[tag_number-1]
context['docker_tag_name'] = f"{tags[tag_number-1]}_sunet-" + "${GIT_COMMIT}"
# Todo. change .jenkins.yaml with the new version
template = Template(get_template(filename="templates/.jenkins.yaml.jinja"))
rendered_yaml = template.render(context)
yaml_data = yaml.safe_load(rendered_yaml)
# Print the rendered YAML content
print("Updated .jenkins.yaml:\n")
yaml_output = yaml.dump(yaml_data, default_flow_style=False)
tabbed_yaml_output = "\n".join(f"\t{line}" for line in yaml_output.splitlines())
print(tabbed_yaml_output)
# TODOD. push changes to git
file_to_remplace = ".jenkins.yaml"
with open(file_to_remplace, "w") as output_file:
output_file.write(rendered_yaml)
# Prompt the user for a commit message
commit_message = input("Enter the commit message: ")
print("\tCommitting changes...")
# Commit the changes
subprocess.run('git add .jenkins.yaml', shell=True, check=True)
subprocess.run(f'git commit -m "{commit_message}"', shell=True, check=True)
print("pushing changes to repository...")
subprocess.run("git push -u origin main", shell=True, check=True)