Skip to content

Commit

Permalink
#548 dynamic generation of codefresh prepare cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
filippomc committed Aug 11, 2022
1 parent 81c7185 commit c9fa294
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
16 changes: 12 additions & 4 deletions tools/cloudharness_utilities/codefresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ def create_codefresh_deployment_scripts(root_paths, envs=(), include=(), exclude
"""
Entry point to create deployment scripts for codefresh: codefresh.yaml and helm chart
"""

build_included = [app['harness']['name']
for app in values_manual_deploy['apps'].values() if 'harness' in app]
out_filename = f"codefresh-{'-'.join(envs)}.yaml"

if include:
if build_included:
logging.info(
'Including the following subpaths to the build: %s.', ', '
.join(include)
.join(build_included)
)

if exclude:
Expand Down Expand Up @@ -71,7 +72,7 @@ def create_codefresh_deployment_scripts(root_paths, envs=(), include=(), exclude
if not 'steps' in codefresh:
continue

def codefresh_build_step_from_base_path(base_path, build_step, fixed_context=None, include=include):
def codefresh_build_step_from_base_path(base_path, build_step, fixed_context=None, include=build_included):

for dockerfile_path in find_dockerfiles_paths(base_path):
app_relative_to_root = os.path.relpath(
Expand Down Expand Up @@ -142,8 +143,15 @@ def codefresh_build_step_from_base_path(base_path, build_step, fixed_context=Non
"CUSTOM_apps_%s_harness_secrets_%s=${{%s}}" % (app_name, secret_name, secret_name.upper()))

cmds = codefresh['steps']['prepare_deployment']['commands']

params = [p for inc in include for p in ["-i", inc]] +\
[p for ex in exclude for p in ["-i", ex]]

for i in range(len(cmds)):
cmds[i] = cmds[i].replace("$ENV", "-".join(envs))
cmds[i] = cmds[i].replace("$PARAMS", " ".join(params))
cmds[i] = cmds[i].replace("$PATHS", " ".join(os.path.relpath(
root_path, '.') for root_path in root_paths))

if save:
codefresh_abs_path = os.path.join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ steps:
working_directory: .
commands:
- bash cloud-harness/install.sh
- harness-deployment cloud-harness . -t ${{CF_BUILD_ID}} -d ${{DOMAIN}} -r ${{REGISTRY}} -rs ${{REGISTRY_SECRET}} -e $ENV
- harness-deployment $PATHS -t ${{CF_BUILD_ID}} -d ${{DOMAIN}} -r ${{REGISTRY}} -rs ${{REGISTRY_SECRET}} -n ${{NAMESPACE}} -e $ENV $PARAMS
prepare_deployment_view:
commands:
- 'helm template ./deployment/helm --debug -n ${{NAMESPACE}}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ steps:
working_directory: .
commands:
- bash cloud-harness/install.sh
- harness-deployment . cloud-harness -t ${{DEPLOYMENT_TAG}} -d ${{DOMAIN}} -r ${{REGISTRY}} -rs ${{REGISTRY_SECRET}} -e $ENV
- harness-deployment $PATHS -t ${{CF_BUILD_ID}} -d ${{DOMAIN}} -r ${{REGISTRY}} -rs ${{REGISTRY_SECRET}} -n ${{NAMESPACE}} -e $ENV $PARAMS
prepare_deployment_view:
commands:
- 'helm template ./deployment/helm --debug -n ${{NAMESPACE}}'
Expand Down
21 changes: 11 additions & 10 deletions tools/harness-deployment
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ if __name__ == "__main__":

root_paths = [os.path.join(os.getcwd(), path) for path in args.paths]

envs = (args.env.split("-") if "-" in args.env else [args.env]) if args.env else ()
envs = (args.env.split("-")
if "-" in args.env else [args.env]) if args.env else ()

if unknown:
print('There are unknown args. Make sure to call the script with the accepted args. Try --help')
Expand Down Expand Up @@ -90,17 +91,17 @@ if __name__ == "__main__":
namespace=args.namespace
)

root_paths = preprocess_build_overrides(root_paths=root_paths, helm_values=helm_values)

build_included = [app['harness']['name']
for app in helm_values['apps'].values() if 'harness' in app]
root_paths = preprocess_build_overrides(
root_paths=root_paths, helm_values=helm_values)

if envs:

create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=envs,
base_image_name=helm_values['name'],
values_manual_deploy=helm_values)
create_codefresh_deployment_scripts(
root_paths,
include=args.include,
exclude=args.exclude,
envs=envs,
base_image_name=helm_values['name'],
values_manual_deploy=helm_values)

create_skaffold_configuration(root_paths, helm_values)
create_vscode_debug_configuration([os.path.join(os.getcwd(), path) for path in args.paths],
Expand Down
3 changes: 3 additions & 0 deletions tools/tests/test_codefresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,7 @@ def test_create_codefresh_configuration_multienv():
for cmd in cf['steps']['prepare_deployment']['commands']:
if 'harness-deployment' in cmd:
assert '-e dev-test' in cmd
assert " . " in cmd
assert "test_deployment" in cmd
assert "-i samples" in cmd
shutil.rmtree(BUILD_MERGE_DIR)

0 comments on commit c9fa294

Please sign in to comment.