Skip to content

Commit 64131c4

Browse files
Add delete preview cmd
1 parent 9e0646c commit 64131c4

5 files changed

Lines changed: 207 additions & 3 deletions

File tree

doc/commands/delete-preview.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
[GitOps CLI](../../README.md) » delete-preview
2+
3+
# delete-preview
4+
```
5+
usage: gitopscli create-preview [-h] [-b BRANCH] [-u USERNAME] [-p PASSWORD]
6+
[-j GIT_USER] [-e GIT_EMAIL] [-c [CREATE_PR]]
7+
[-a [AUTO_MERGE]] -o ORGANISATION -n
8+
REPOSITORY_NAME [-s GIT_PROVIDER]
9+
[-w GIT_PROVIDER_URL] -i PR_ID [-x PARENT_ID]
10+
11+
optional arguments:
12+
-h, --help show this help message and exit
13+
-b BRANCH, --branch BRANCH
14+
Branch to push the changes to
15+
-u USERNAME, --username USERNAME
16+
Git username if Basic Auth should be used
17+
-p PASSWORD, --password PASSWORD
18+
Git password if Basic Auth should be used
19+
-j GIT_USER, --git-user GIT_USER
20+
Git Username
21+
-e GIT_EMAIL, --git-email GIT_EMAIL
22+
Git User Email
23+
-c [CREATE_PR], --create-pr [CREATE_PR]
24+
Creates a Pull Request (only when --branch is not
25+
master/default branch)
26+
-a [AUTO_MERGE], --auto-merge [AUTO_MERGE]
27+
Automatically merge the created PR (only valid with
28+
--create-pr)
29+
-o ORGANISATION, --organisation ORGANISATION
30+
Apps Git organisation/projectKey
31+
-n REPOSITORY_NAME, --repository-name REPOSITORY_NAME
32+
Git repository name (not the URL, e.g. my-repo)
33+
-s GIT_PROVIDER, --git-provider GIT_PROVIDER
34+
Git server provider
35+
-w GIT_PROVIDER_URL, --git-provider-url GIT_PROVIDER_URL
36+
Git provider base API URL (e.g.
37+
https://bitbucket.example.tld)
38+
-i PR_ID, --pr-id PR_ID
39+
the id of the pull request
40+
-x PARENT_ID, --parent-id PARENT_ID
41+
the id of the parent comment, in case of a reply
42+
```
43+
44+
## Example
45+
```bash
46+
gitopscli delete-preview \
47+
--git-provider-url https://bitbucket.baloise.dev \
48+
--username $GIT_USERNAME \
49+
--password $GIT_PASSWORD \
50+
--git-user "GitOpsCLI" \
51+
--git-email "gitopscli@baloise.dev" \
52+
--organisation "my-team" \
53+
--repository-name "my-app" \
54+
--branch "some-branch-name" \
55+
--create-pr \
56+
--pr-id 4711 \
57+
--auto-merge
58+
```
59+
60+
## .gitops.config.yaml
61+
Make sure that your application repository contains a `.gitops.config.yaml` file.
62+
63+
```yaml
64+
deploymentConfig:
65+
# The organisation name of your deployment repo
66+
org: DPL
67+
# The repostiory name of your deployment repo
68+
repository: incubator-non-prod
69+
# The name of the application that is used in your deployment repo
70+
applicationName: example
71+
72+
previewConfig:
73+
route:
74+
host:
75+
# your router host.
76+
#{SHA256_8CHAR_BRANCH_HASH} gets replaced by a shortened hash of your feature branch name
77+
template: example-{SHA256_8CHAR_BRANCH_HASH}.example.tld
78+
replace:
79+
# Paths that should be replaced
80+
- path: image.tag
81+
variable: GIT_COMMIT # this is the latest git hash of the PR branch
82+
- path: route.host
83+
variable: ROUTE_HOST # this is the resolved SHA256_8CHAR_BRANCH_HASH from above
84+
85+
```

gitopscli/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from gitopscli.cliparser import create_cli
55
from gitopscli.commands.add_pr_comment import pr_comment_command
66
from gitopscli.commands.create_preview import create_preview_command
7+
from gitopscli.commands.delete_preview import delete_preview_command
78
from gitopscli.commands.deploy import deploy_command
89
from gitopscli.commands.sync_apps import sync_apps_command
910
from gitopscli.gitops_exception import GitOpsException
@@ -21,6 +22,8 @@ def main():
2122
command = pr_comment_command
2223
elif args.command == "create-preview":
2324
command = create_preview_command
25+
elif args.command == "delete-preview":
26+
command = delete_preview_command
2427

2528
try:
2629
command(**vars(args))

gitopscli/cliparser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def create_cli():
1010
__add_sync_apps_command_parser(subparsers)
1111
__add_pr_comment_command_parser(subparsers)
1212
__add_create_preview_command_parser(subparsers)
13+
__add_delete_preview_command_parser(subparsers)
1314

1415
if len(sys.argv) == 1:
1516
parser.print_help(sys.stderr)
@@ -70,6 +71,12 @@ def __add_create_preview_command_parser(subparsers):
7071
__add_create_prid_parser(add_create_preview_p)
7172

7273

74+
def __add_delete_preview_command_parser(subparsers):
75+
add_delete_preview_p = subparsers.add_parser("delete-preview", help="Delete a preview environment")
76+
__add_git_parser_args(add_delete_preview_p)
77+
__add_branch_pr_parser_args(add_delete_preview_p)
78+
79+
7380
def __add_git_parser_args(deploy_p):
7481
deploy_p.add_argument("-u", "--username", help="Git username if Basic Auth should be used")
7582
deploy_p.add_argument("-p", "--password", help="Git password if Basic Auth should be used")

gitopscli/commands/create_preview.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ def create_preview_command(
9292
if not value_replaced:
9393
__no_deployment_needed(apps_git, new_image_tag, parent_id, pr_id)
9494
sys.exit(0)
95-
root_git.commit(f"Upated preview environment for {gitops_config.application_name}.")
95+
root_git.commit(f"Upated preview environment for {gitops_config.application_name} and branch {branch}.")
9696
root_git.push(branch)
9797
logging.info("Pushed branch %s", branch)
9898
pr_comment_text = f"""
99-
New Preview Environment for {gitops_config.application_name} created successfully. Access it here:
99+
New Preview Environment for {gitops_config.application_name} and branch {branch} created successfully. Access it here:
100100
https://{route_host}
101101
"""
102102
if branch_preview_env_already_exist:
103103
pr_comment_text = f"""
104-
Preview Environment for {gitops_config.application_name} updated successfully. Access it here:
104+
Preview Environment for {gitops_config.application_name} and branch {branch} updated successfully. Access it here:
105105
https://{route_host}
106106
"""
107107
logging.info("Creating PullRequest comment for pr with id %s and content: %s", pr_id, pr_comment_text)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import hashlib
2+
import logging
3+
import os
4+
import shutil
5+
import sys
6+
import uuid
7+
8+
from gitopscli.git.create_git import create_git
9+
from gitopscli.yaml.gitops_config import GitOpsConfig
10+
11+
12+
def delete_preview_command(
13+
command,
14+
branch,
15+
username,
16+
password,
17+
git_user,
18+
git_email,
19+
create_pr,
20+
auto_merge,
21+
organisation,
22+
repository_name,
23+
git_provider,
24+
git_provider_url,
25+
):
26+
assert command == "delete-preview"
27+
28+
apps_tmp_dir = __create_tmp_dir()
29+
root_tmp_dir = __create_tmp_dir()
30+
31+
try:
32+
apps_git = create_git(
33+
username,
34+
password,
35+
git_user,
36+
git_email,
37+
organisation,
38+
repository_name,
39+
git_provider,
40+
git_provider_url,
41+
apps_tmp_dir,
42+
)
43+
44+
apps_git.checkout(branch)
45+
logging.info("App repo branch %s checkout successful", branch)
46+
shortened_branch_hash = hashlib.sha256(branch.encode("utf-8")).hexdigest()[:8]
47+
logging.info("Hashed branch %s to hash: %s", branch, shortened_branch_hash)
48+
gitops_config = GitOpsConfig(apps_git.get_full_file_path(".gitops.config.yaml"))
49+
logging.info("Read GitOpsConfig: %s", gitops_config)
50+
51+
root_git = create_git(
52+
username,
53+
password,
54+
git_user,
55+
git_email,
56+
gitops_config.team_config_org,
57+
gitops_config.team_config_repo,
58+
git_provider,
59+
git_provider_url,
60+
root_tmp_dir,
61+
)
62+
root_git.checkout("master")
63+
logging.info("Config repo branch master checkout successful")
64+
root_git.new_branch(branch)
65+
logging.info("Created branch %s in config repo", branch)
66+
new_preview_folder_name = gitops_config.application_name + "-" + shortened_branch_hash + "-preview"
67+
logging.info("New folder for preview: %s", new_preview_folder_name)
68+
branch_preview_env_exists = os.path.exists(root_git.get_full_file_path(new_preview_folder_name))
69+
logging.info("Is preview env already existing for branch? %s", branch_preview_env_exists)
70+
if branch_preview_env_exists:
71+
shutil.rmtree(root_git.get_full_file_path(new_preview_folder_name), ignore_errors=True)
72+
else:
73+
logging.info("There was no preview with name: %s", new_preview_folder_name)
74+
sys.exit(0)
75+
root_git.commit(f"Deleted preview environment for {gitops_config.application_name} and branch {branch}.")
76+
root_git.push(branch)
77+
logging.info("Pushed branch %s", branch)
78+
79+
finally:
80+
shutil.rmtree(apps_tmp_dir, ignore_errors=True)
81+
shutil.rmtree(root_tmp_dir, ignore_errors=True)
82+
if create_pr and branch != "master":
83+
pull_request = __create_pullrequest(branch, gitops_config, root_git)
84+
if auto_merge:
85+
__merge_pullrequest(branch, pull_request, root_git)
86+
87+
88+
def __create_pullrequest(branch, gitops_config, root_git):
89+
title = "Updated preview environemnt for " + gitops_config.application_name
90+
description = f"""
91+
This Pull Request is automatically created through [gitopscli](https://github.com/baloise-incubator/gitopscli).
92+
"""
93+
pull_request = root_git.create_pull_request(branch, "master", title, description)
94+
logging.info("Pull request created: %s", {root_git.get_pull_request_url(pull_request)})
95+
return pull_request
96+
97+
98+
def __merge_pullrequest(branch, pull_request, root_git):
99+
root_git.merge_pull_request(pull_request)
100+
logging.info("Pull request merged")
101+
root_git.delete_branch(branch)
102+
logging.info("Branch '%s' deleted", branch)
103+
104+
105+
def __create_tmp_dir():
106+
tmp_dir = f"/tmp/gitopscli/{uuid.uuid4()}"
107+
os.makedirs(tmp_dir)
108+
logging.info("Created directory %s", tmp_dir)
109+
return tmp_dir

0 commit comments

Comments
 (0)