Skip to content

Commit 5bdd636

Browse files
authored
refactor(create-preview/delete-preview)!: introduce new create-pr-preview and delete-pr-preview commands and refactor old create-preview and delete-preview to use different approach create-pr-preview: handles deployment of preview env and chatops (comments in the pr) create-preview: only handled deployment of the preview env delete-pr-preview: deletes deployment of preview env which was created with create-pr-preview command delete-preview: deletes deployment of preview env which was created with create-preview command BREAKING CHANGE: if you used create-preview you now need to switch to create-pr-preview, if you used delete-preview you now need to switch to delete-pr-preview command
Create preview refactoring
2 parents 550a2e2 + e21ba4b commit 5bdd636

File tree

14 files changed

+763
-216
lines changed

14 files changed

+763
-216
lines changed

.gitpod.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
tasks:
2-
- init: make init && gitopscli --help
2+
- init: make init && export COLUMNS=80 && gitopscli --help

docs/commands/create-pr-preview.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# create-pr-preview
2+
3+
The `create-pr-preview` command can be used to create a preview environment in your *deployment config repository* for a pull request of your *app repository*. You can later easily delete this preview with the [`delete-pr-preview` command](/gitopscli/commands/delete-pr-preview/).
4+
5+
You need to provide some additional configuration files in your repositories for this command to work.
6+
7+
## Configuration
8+
### Preview Templates
9+
10+
Your *deployment config repository* needs to contain a `.preview-templates` folder with the deployment configuration templates for every application you want to use this command for.
11+
12+
For example you have to provide `.preview-templates/app-xy` for your app `app-xy`. The `create-pr-preview` command simply copies this directory to the root of the repository. Only image tag and route host will be replaced in the preview version of the deployment.
13+
14+
```
15+
deployment-config-repo/
16+
├── .preview-templates
17+
│   └── app-xy
18+
│   ├── values.yaml
19+
│   └── some-more-config-files-or-folders
20+
├── app-xy-production
21+
├── app-xy-staging
22+
├── app-xy-test
23+
└── app-xy-c7003101-preview <- This is how a created preview looks like
24+
├── values.yaml <- image tag and route host are replaced in this one
25+
   └── some-more-config-files-or-folders
26+
```
27+
28+
!!! info "Currently you have to specify image tag and route host in a `values.yaml` file. We are working on making this configurable in the future."
29+
30+
### .gitops.config.yaml
31+
32+
Make sure that your *app repository* contains a `.gitops.config.yaml` file. This file provides all information to
33+
34+
1. find the *deployment config repository*
35+
2. locate the preview template for your app
36+
3. replace image tag and route host in the template
37+
38+
```yaml
39+
deploymentConfig:
40+
# The organisation name of your deployment repo
41+
org: deployments
42+
# The repostiory name of your deployment repo
43+
repository: deployment-config-repo
44+
# The name of the application (name of the folder in `.preview-templates`)
45+
applicationName: app-xy
46+
47+
previewConfig:
48+
route:
49+
host:
50+
# Your router host
51+
# {SHA256_8CHAR_BRANCH_HASH} gets replaced by a shortened hash of your feature branch name
52+
template: app.xy-{SHA256_8CHAR_BRANCH_HASH}.example.tld
53+
replace:
54+
# Paths that should be replaced in the `values.yaml`
55+
- path: image.tag
56+
variable: GIT_COMMIT # this is the latest git hash of the pull request branch
57+
- path: route.host
58+
variable: ROUTE_HOST # this is the resolved host.template from above
59+
```
60+
61+
## Example
62+
63+
```bash
64+
gitopscli create-pr-preview \
65+
--git-provider-url https://bitbucket.baloise.dev \
66+
--username $GIT_USERNAME \
67+
--password $GIT_PASSWORD \
68+
--git-user "GitOps CLI" \
69+
--git-email "[email protected]" \
70+
--organisation "my-team" \
71+
--repository-name "app-xy" \
72+
--pr-id 4711 \
73+
--create-pr \
74+
--auto-merge
75+
```
76+
77+
## Usage
78+
```
79+
usage: gitopscli create-pr-preview [-h] --username USERNAME --password PASSWORD
80+
[--git-user GIT_USER] [--git-email GIT_EMAIL]
81+
--organisation ORGANISATION --repository-name
82+
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
83+
[--git-provider-url GIT_PROVIDER_URL]
84+
--pr-id PR_ID
85+
[--parent-id PARENT_ID] [-v [VERBOSE]]
86+
87+
optional arguments:
88+
-h, --help show this help message and exit
89+
--username USERNAME Git username
90+
--password PASSWORD Git password or token
91+
--git-user GIT_USER Git Username
92+
--git-email GIT_EMAIL
93+
Git User Email
94+
--organisation ORGANISATION
95+
Apps Git organisation/projectKey
96+
--repository-name REPOSITORY_NAME
97+
Git repository name (not the URL, e.g. my-repo)
98+
--git-provider GIT_PROVIDER
99+
Git server provider
100+
--git-provider-url GIT_PROVIDER_URL
101+
Git provider base API URL (e.g.
102+
https://bitbucket.example.tld)
103+
--pr-id PR_ID the id of the pull request
104+
--parent-id PARENT_ID
105+
the id of the parent comment, in case of a reply
106+
-v [VERBOSE], --verbose [VERBOSE]
107+
Verbose exception logging
108+
```

docs/commands/create-preview.md

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# create-preview
22

3-
The `create-preview` command can be used to create a preview environment in your *deployment config repository* for a pull request in you *app repository*. You can later easily delete this preview with the [`delete-preview` command](/gitopscli/commands/delete-preview/).
3+
The `create-preview` command can be used to create a preview environment in your *deployment config repository* for a commit hash of your *app repository*. You can later easily delete this preview with the [`delete-preview` command](/gitopscli/commands/delete-preview/).
44

55
You need to provide some additional configuration files in your repositories for this command to work.
66

@@ -48,12 +48,12 @@ previewConfig:
4848
route:
4949
host:
5050
# Your router host
51-
# {SHA256_8CHAR_BRANCH_HASH} gets replaced by a shortened hash of your feature branch name
51+
# {SHA256_8CHAR_BRANCH_HASH} gets replaced by a shortened hash of your preview_id
5252
template: app.xy-{SHA256_8CHAR_BRANCH_HASH}.example.tld
5353
replace:
5454
# Paths that should be replaced in the `values.yaml`
5555
- path: image.tag
56-
variable: GIT_COMMIT # this is the latest git hash of the pull request branch
56+
variable: GIT_COMMIT # this is the git hash of your app repo
5757
- path: route.host
5858
variable: ROUTE_HOST # this is the resolved host.template from above
5959
```
@@ -69,9 +69,8 @@ gitopscli create-preview \
6969
--git-email "[email protected]" \
7070
--organisation "my-team" \
7171
--repository-name "app-xy" \
72-
--pr-id 4711 \
73-
--create-pr \
74-
--auto-merge
72+
--git-hash "c0784a34e834117e1489973327ff4ff3c2582b94" \
73+
--preview-id "test-preview-id" \
7574
```
7675

7776
## Usage
@@ -81,9 +80,8 @@ usage: gitopscli create-preview [-h] --username USERNAME --password PASSWORD
8180
--organisation ORGANISATION --repository-name
8281
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
8382
[--git-provider-url GIT_PROVIDER_URL]
84-
[--create-pr [CREATE_PR]]
85-
[--auto-merge [AUTO_MERGE]] --pr-id PR_ID
86-
[--parent-id PARENT_ID] [-v [VERBOSE]]
83+
--preview-id PREVIEW_ID
84+
[-v [VERBOSE]]
8785
8886
optional arguments:
8987
-h, --help show this help message and exit
@@ -101,14 +99,8 @@ optional arguments:
10199
--git-provider-url GIT_PROVIDER_URL
102100
Git provider base API URL (e.g.
103101
https://bitbucket.example.tld)
104-
--create-pr [CREATE_PR]
105-
Creates a Pull Request
106-
--auto-merge [AUTO_MERGE]
107-
Automatically merge the created PR (only valid with
108-
--create-pr)
109-
--pr-id PR_ID the id of the pull request
110-
--parent-id PARENT_ID
111-
the id of the parent comment, in case of a reply
102+
--git-hash GIT_HASH the git hash of the app repo which should be deployed
103+
--preview-id PREVIEW_ID the id of the created preview environment
112104
-v [VERBOSE], --verbose [VERBOSE]
113105
Verbose exception logging
114106
```

docs/commands/delete-pr-preview.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# delete-pr-preview
2+
3+
The `delete-pr-preview` command can be used to delete a preview previously created with the [`create-pr-preview` command](/gitopscli/commands/create-pr-preview/). Please refer to `create-pr-preview` documentation for the needed configuration files.
4+
5+
## Example
6+
7+
```bash
8+
gitopscli delete-pr-preview \
9+
--git-provider-url https://bitbucket.baloise.dev \
10+
--username $GIT_USERNAME \
11+
--password $GIT_PASSWORD \
12+
--git-user "GitOps CLI" \
13+
--git-email "[email protected]" \
14+
--organisation "my-team" \
15+
--repository-name "app-xy" \
16+
--branch "my-pr-branch" \
17+
```
18+
19+
## Usage
20+
```
21+
usage: gitopscli delete-pr-preview [-h] --username USERNAME --password PASSWORD
22+
[--git-user GIT_USER] [--git-email GIT_EMAIL]
23+
--organisation ORGANISATION --repository-name
24+
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
25+
[--git-provider-url GIT_PROVIDER_URL] --branch
26+
BRANCH
27+
[-v [VERBOSE]]
28+
29+
optional arguments:
30+
-h, --help show this help message and exit
31+
--username USERNAME Git username
32+
--password PASSWORD Git password or token
33+
--git-user GIT_USER Git Username
34+
--git-email GIT_EMAIL
35+
Git User Email
36+
--organisation ORGANISATION
37+
Apps Git organisation/projectKey
38+
--repository-name REPOSITORY_NAME
39+
Git repository name (not the URL, e.g. my-repo)
40+
--git-provider GIT_PROVIDER
41+
Git server provider
42+
--git-provider-url GIT_PROVIDER_URL
43+
Git provider base API URL (e.g.
44+
https://bitbucket.example.tld)
45+
--branch BRANCH The branch for which the preview was created for
46+
-v [VERBOSE], --verbose [VERBOSE]
47+
Verbose exception logging
48+
```

docs/commands/delete-preview.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ gitopscli delete-preview \
1313
--git-email "[email protected]" \
1414
--organisation "my-team" \
1515
--repository-name "app-xy" \
16-
--branch "my-pr-branch" \
17-
--create-pr \
18-
--auto-merge
16+
--preview-id "test123" \
1917
```
2018

2119
## Usage
@@ -24,9 +22,9 @@ usage: gitopscli delete-preview [-h] --username USERNAME --password PASSWORD
2422
[--git-user GIT_USER] [--git-email GIT_EMAIL]
2523
--organisation ORGANISATION --repository-name
2624
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
27-
[--git-provider-url GIT_PROVIDER_URL] --branch
28-
BRANCH [--create-pr [CREATE_PR]]
29-
[--auto-merge [AUTO_MERGE]] [-v [VERBOSE]]
25+
[--git-provider-url GIT_PROVIDER_URL] --preview-id
26+
PREVIEW_ID
27+
[-v [VERBOSE]]
3028
3129
optional arguments:
3230
-h, --help show this help message and exit
@@ -44,12 +42,7 @@ optional arguments:
4442
--git-provider-url GIT_PROVIDER_URL
4543
Git provider base API URL (e.g.
4644
https://bitbucket.example.tld)
47-
--branch BRANCH The branch for which the preview was created for
48-
--create-pr [CREATE_PR]
49-
Creates a Pull Request
50-
--auto-merge [AUTO_MERGE]
51-
Automatically merge the created PR (only valid with
52-
--create-pr)
45+
--preview-id PREVIEW_ID The preview id for which the preview was created for
5346
-v [VERBOSE], --verbose [VERBOSE]
5447
Verbose exception logging
5548
```

gitopscli/__main__.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
from gitopscli.cliparser import create_cli
55
from gitopscli.commands import (
66
pr_comment_command,
7+
create_pr_preview_command,
78
create_preview_command,
89
delete_preview_command,
910
deploy_command,
1011
sync_apps_command,
1112
version_command,
13+
delete_pr_preview_command,
1214
)
1315
from gitopscli.gitops_exception import GitOpsException
1416

@@ -17,18 +19,7 @@ def main():
1719
logging.basicConfig(level=logging.INFO, format="%(levelname)-2s %(funcName)s: %(message)s")
1820
args = create_cli(sys.argv[1:])
1921

20-
if args.command == "deploy":
21-
command = deploy_command
22-
elif args.command == "sync-apps":
23-
command = sync_apps_command
24-
elif args.command == "add-pr-comment":
25-
command = pr_comment_command
26-
elif args.command == "create-preview":
27-
command = create_preview_command
28-
elif args.command == "delete-preview":
29-
command = delete_preview_command
30-
elif args.command == "version":
31-
command = version_command
22+
command = get_command(args.command)
3223

3324
if "verbose" in args:
3425
verbose = args.verbose
@@ -48,3 +39,24 @@ def main():
4839

4940
if __name__ == "__main__":
5041
main()
42+
43+
44+
def get_command(command):
45+
command_func = None
46+
if command == "deploy":
47+
command_func = deploy_command
48+
elif command == "sync-apps":
49+
command_func = sync_apps_command
50+
elif command == "add-pr-comment":
51+
command_func = pr_comment_command
52+
elif command == "create-pr-preview":
53+
command_func = create_pr_preview_command
54+
elif command == "create-preview":
55+
command_func = create_preview_command
56+
elif command == "delete-pr-preview":
57+
command_func = delete_pr_preview_command
58+
elif command == "delete-preview":
59+
command_func = delete_preview_command
60+
elif command == "version":
61+
command_func = version_command
62+
return command_func

gitopscli/cliparser.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ def create_cli(args):
1010
__add_sync_apps_command_parser(subparsers)
1111
__add_pr_comment_command_parser(subparsers)
1212
__add_create_preview_command_parser(subparsers)
13+
__add_create_pr_preview_command_parser(subparsers)
1314
__add_delete_preview_command_parser(subparsers)
15+
__add_delete_pr_preview_command_parser(subparsers)
1416
__add_version_command_parser(subparsers)
1517

1618
if len(args) == 0:
@@ -68,20 +70,34 @@ def __add_pr_comment_command_parser(subparsers):
6870

6971

7072
def __add_create_preview_command_parser(subparsers):
71-
add_create_preview_p = subparsers.add_parser("create-preview", help="Create a preview environment")
72-
__add_git_parser_args(add_create_preview_p)
73-
__add_branch_pr_parser_args(add_create_preview_p)
74-
__add_create_prid_parser(add_create_preview_p)
75-
__add_verbose_parser(add_create_preview_p)
73+
add_create_preview_common_p = subparsers.add_parser("create-preview", help="Create a preview environment")
74+
__add_git_parser_args(add_create_preview_common_p)
75+
__add_create_githash_previewid_parser(add_create_preview_common_p)
76+
__add_verbose_parser(add_create_preview_common_p)
77+
78+
79+
def __add_create_pr_preview_command_parser(subparsers):
80+
add_create_pr_preview_p = subparsers.add_parser("create-pr-preview", help="Create a preview environment")
81+
__add_git_parser_args(add_create_pr_preview_p)
82+
__add_create_prid_parser(add_create_pr_preview_p)
83+
__add_verbose_parser(add_create_pr_preview_p)
7684

7785

7886
def __add_delete_preview_command_parser(subparsers):
7987
add_delete_preview_p = subparsers.add_parser("delete-preview", help="Delete a preview environment")
8088
__add_git_parser_args(add_delete_preview_p)
89+
add_delete_preview_p.add_argument(
90+
"--preview-id", help="The preview-id for which the preview was created for", required=True
91+
)
92+
__add_verbose_parser(add_delete_preview_p)
93+
94+
95+
def __add_delete_pr_preview_command_parser(subparsers):
96+
add_delete_preview_p = subparsers.add_parser("delete-pr-preview", help="Delete a pr preview environment")
97+
__add_git_parser_args(add_delete_preview_p)
8198
add_delete_preview_p.add_argument(
8299
"--branch", help="The branch for which the preview was created for", required=True
83100
)
84-
__add_branch_pr_parser_args(add_delete_preview_p)
85101
__add_verbose_parser(add_delete_preview_p)
86102

87103

@@ -115,6 +131,13 @@ def __add_branch_pr_parser_args(deploy_p):
115131
)
116132

117133

134+
def __add_create_githash_previewid_parser(subparsers):
135+
subparsers.add_argument("--git-hash", help="the git hash which should be deployed", type=str, required=True)
136+
subparsers.add_argument(
137+
"--preview-id", help="the id of folder in the config repo which will be created", type=str, required=True
138+
)
139+
140+
118141
def __add_create_prid_parser(subparsers):
119142
subparsers.add_argument("--pr-id", help="the id of the pull request", type=int, required=True)
120143
subparsers.add_argument("--parent-id", help="the id of the parent comment, in case of a reply", type=int)

0 commit comments

Comments
 (0)