Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jinja2 template for jenkins #54

Merged
merged 7 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/deploy-test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
name: Deploy to Aeolus Test

on:
workflow_dispatch:
inputs:
docker-tag:
description: 'Docker tag to deploy (e.g. 1.0.0 or latest, default: nightly)'
required: true
default: 'nightly'
branch-name:
description: 'Branch name to deploy (default: develop)'
required: true
default: 'develop'
pull_request:
types: [labeled]

Expand Down
5 changes: 4 additions & 1 deletion cli/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ trim_trailing_whitespace = false
indent_size = 2

[*.j2]
indent_size = 2
indent_size = 2

[Jenkinsfile]
indent_size = 2
6 changes: 3 additions & 3 deletions cli/generators/bamboo.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def generate_in_api(self, payload: str) -> Optional[str]:
if response.status_code == 200:
logger.info("🔨", "Bamboo YAML Spec file generated", self.output_settings.emoji)
body: dict[str, str] = response.json()
self.result.append(body["result"])
self.result = body["result"]
return body["key"]
logger.error("❌", "Bamboo YAML Spec file generation failed", self.output_settings.emoji)
raise ValueError("Bamboo YAML Spec file generation failed")
Expand Down Expand Up @@ -142,7 +142,7 @@ def generate_in_java(self, base64_str: str) -> None:
raise ValueError("Bamboo YAML Spec file generation failed")
logger.info("🔨", "Bamboo YAML Spec file generated", self.output_settings.emoji)
result_logs: str = process.stdout
self.result.append(result_logs)
self.result = result_logs

def generate_in_docker(self, base64_str: str) -> None:
"""
Expand Down Expand Up @@ -182,7 +182,7 @@ def generate_in_docker(self, base64_str: str) -> None:
logger.info("🔨", "Bamboo YAML Spec file generated", self.output_settings.emoji)
result_logs: str = container.logs(stdout=True, stderr=False).decode("utf-8")
container.remove()
self.result.append(result_logs)
self.result = result_logs

def check(self, content: str) -> bool:
raise NotImplementedError("check_syntax() not implemented")
Expand Down
15 changes: 3 additions & 12 deletions cli/generators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BaseGenerator:
input_settings: InputSettings
output_settings: OutputSettings
metadata: PassMetadata
result: typing.List[str]
result: str
final_result: typing.Optional[str]
environment: EnvironmentSchema
key: typing.Optional[str]
Expand All @@ -41,7 +41,7 @@ def __init__(
self.input_settings = input_settings
self.output_settings = output_settings
self.metadata = metadata
self.result = []
self.result = ""
if input_settings.target is None:
raise ValueError("No target specified")
env: typing.Optional[EnvironmentSchema] = utils.get_ci_environment(
Expand All @@ -67,14 +67,6 @@ def __init__(
self.needs_lifecycle_parameter = self.__needs_lifecycle_parameter()
self.needs_subshells = self.has_multiple_steps

def add_line(self, indentation: int, line: str) -> None:
"""
Add a line to the result.
:param indentation: indentation level
:param line: line to add
"""
self.result.append(" " * indentation + line)

def add_repository_urls_to_environment(self) -> None:
"""
Some systems don't offer a way to access repository urls in the CI file.
Expand Down Expand Up @@ -160,8 +152,7 @@ def generate(self) -> str:
"""
Generate the CI file.
"""
self.final_result = "\n".join(self.result)
return self.final_result
return self.result

def run(self, job_id: str) -> None:
"""
Expand Down
41 changes: 10 additions & 31 deletions cli/generators/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import typing
from typing import List, Optional

from jinja2 import Environment, FileSystemLoader, Template
from docker.client import DockerClient # type: ignore
from docker.models.containers import Container # type: ignore
from docker.types.daemon import CancellableStream # type: ignore
from jinja2 import Environment, FileSystemLoader

from classes.generated.definitions import ScriptAction, Repository, Target
from classes.generated.windfile import WindFile
Expand All @@ -29,6 +29,8 @@ class CliGenerator(BaseGenerator):

initial_directory_variable: str = "AEOLUS_INITIAL_DIRECTORY"

template: Optional[Template] = None

def __init__(
self, windfile: WindFile, input_settings: InputSettings, output_settings: OutputSettings, metadata: PassMetadata
):
Expand Down Expand Up @@ -74,30 +76,6 @@ def handle_step(self, name: str, step: ScriptAction, call: bool) -> None:
self.after_results[step.name] = [result for result in step.results if result.before]
return None

def add_environment(self, step: ScriptAction) -> None:
"""
Add environment variables to the step.
:param step: to add environment variables to
"""
if step.environment:
for env_var in step.environment.root.root:
env_value: typing.Any = step.environment.root.root[env_var]
if isinstance(env_value, List):
env_value = " ".join(env_value)
self.result.append(f'export {env_var}="' f'{env_value}"')

def add_parameters(self, step: ScriptAction) -> None:
"""
Add parameters to the step.
:param step: to add parameters to
"""
if step.parameters is not None:
for parameter in step.parameters.root.root:
value: typing.Any = step.parameters.root.root[parameter]
if isinstance(value, List):
value = " ".join(value)
self.add_line(indentation=2, line=f'{parameter}="' f'{value}"')

def check(self, content: str) -> bool:
"""
Check the generated bash file for syntax errors.
Expand Down Expand Up @@ -194,11 +172,12 @@ def generate_using_jinja2(self) -> str:
Generate the bash script to be used as a local CI system with jinja2.
"""
# Load the template from the file system
env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "..", "templates")))
template = env.get_template("cli.sh.j2")
if not self.template:
env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "..", "templates")))
self.template = env.get_template("cli.sh.j2")

# Prepare your data
data = {
data: dict[str, typing.Any] = {
"has_multiple_steps": self.has_multiple_steps,
"initial_directory_variable": self.initial_directory_variable,
"environment": self.windfile.environment.root.root if self.windfile.environment else {},
Expand All @@ -214,7 +193,7 @@ def generate_using_jinja2(self) -> str:
}

# Render the template with your data
rendered_script = template.render(data)
rendered_script = self.template.render(data)

return rendered_script

Expand All @@ -224,11 +203,11 @@ def generate(self) -> str:
we don't want to handle the credentials in the CI system.
:return: bash script
"""
self.result = []
self.result = ""
utils.replace_environment_variables_in_windfile(environment=self.environment, windfile=self.windfile)
self.add_repository_urls_to_environment()
for step in self.windfile.actions:
if isinstance(step.root, ScriptAction):
self.handle_step(name=step.root.name, step=step.root, call=not step.root.runAlways)
self.result.append(self.generate_using_jinja2())
self.result = self.generate_using_jinja2()
return super().generate()
Loading
Loading