Skip to content

Commit 7aa5c92

Browse files
authored
feat: add workflow yml for preview and apply (#155)
1 parent a6e8836 commit 7aa5c92

File tree

7 files changed

+233
-29
lines changed

7 files changed

+233
-29
lines changed

.github/workflows/apply.yml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Apply
2+
on:
3+
push:
4+
branches:
5+
- "main"
6+
7+
jobs:
8+
get-changed-project-stack:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout Code
12+
uses: actions/checkout@v4
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Install Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: 3.9
20+
21+
- name: Install Pytest Html
22+
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
23+
24+
- name: Git Diff
25+
id: git-diff
26+
uses: technote-space/get-diff-action@v6
27+
28+
- name: Get Changed Projects and Stacks
29+
id: get-changed-projects-and-stacks
30+
env:
31+
CHANGED_PATHS: ${{ steps.git-diff.outputs.diff }}
32+
run: |
33+
export CHANGED_PATHS="${{ steps.git-diff.outputs.diff }}"
34+
python3 hack/get_changed_projects_and_stacks.py
35+
36+
- name: Print Changed Projects and Stacks
37+
run: |
38+
echo "Changed projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}"
39+
echo "Changed stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}"
40+
outputs:
41+
changed_projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}
42+
changed_stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}
43+
44+
apply:
45+
needs: [ get-changed-project-stack ]
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Checkout Code
49+
uses: actions/checkout@v4
50+
with:
51+
fetch-depth: 0
52+
53+
- name: Install Kusion
54+
run: curl https://www.kusionstack.io/scripts/install.sh | sh -s 0.12.0-rc.1
55+
56+
- name: Install Python
57+
uses: actions/setup-python@v4
58+
with:
59+
python-version: 3.9
60+
61+
- name: Install Pytest Html
62+
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
63+
64+
- name: Setup K3d&K3s
65+
uses: nolar/setup-k3d-k3s@v1
66+
67+
- name: Apply
68+
id: apply
69+
env:
70+
CHANGED_STACKS: ${{ needs.get-changed-project-stack.outputs.changed_stacks }}
71+
OSS_ACCESS_KEY_ID: '${{ secrets.OSS_ACCESS_KEY_ID }}'
72+
OSS_ACCESS_KEY_SECRET: '${{ secrets.OSS_ACCESS_KEY_SECRET }}'
73+
WORKSPACE_FILE_DIR: workspaces
74+
run: |
75+
# manually source kusion env file
76+
source "$HOME/.kusion/.env"
77+
78+
# setup remote backend for kusion cli
79+
kusion config set backends.oss_test '{"type":"oss","configs":{"bucket":"kusion-test","endpoint":"oss-cn-shanghai.aliyuncs.com"}}'
80+
kusion config set backends.current oss_test
81+
82+
# execute python script for applying
83+
python3 hack/apply_changed_stacks.py
84+
85+
- name: Upload Report
86+
id: upload-report
87+
if: ${{ steps.apply.outputs.apply_success == 'true' }}
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: apply-report
91+
path: hack/report/apply-result.zip
92+
93+
- name: Echo URL
94+
if: ${{ steps.apply.outputs.apply_success == 'true' }}
95+
run: |
96+
echo "Please check the apply result at: ${{ steps.upload-report.outputs.artifact-url }}"

.github/workflows/preview.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: Preview
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "main"
7+
8+
jobs:
9+
get-changed-project-stack:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout Code
13+
uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0
16+
17+
- name: Install Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: 3.9
21+
22+
- name: Install Pytest Html
23+
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
24+
25+
- name: Git Diff
26+
id: git-diff
27+
uses: technote-space/get-diff-action@v6
28+
29+
- name: Get Changed Projects and Stacks
30+
id: get-changed-projects-and-stacks
31+
env:
32+
CHANGED_PATHS: ${{ steps.git-diff.outputs.diff }}
33+
run: |
34+
export CHANGED_PATHS="${{ steps.git-diff.outputs.diff }}"
35+
python3 hack/get_changed_projects_and_stacks.py
36+
37+
- name: Print Changed Projects and Stacks
38+
run: |
39+
echo "Changed projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}"
40+
echo "Changed stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}"
41+
outputs:
42+
changed_projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}
43+
changed_stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}
44+
45+
preview:
46+
needs: [ get-changed-project-stack ]
47+
runs-on: ubuntu-latest
48+
steps:
49+
- name: Checkout Code
50+
uses: actions/checkout@v4
51+
with:
52+
fetch-depth: 0
53+
54+
- name: Install Kusion
55+
run: curl https://www.kusionstack.io/scripts/install.sh | sh -s 0.12.0-rc.1
56+
57+
- name: Install Python
58+
uses: actions/setup-python@v4
59+
with:
60+
python-version: 3.9
61+
62+
- name: Install Pytest Html
63+
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
64+
65+
- name: Setup K3d&K3s
66+
uses: nolar/setup-k3d-k3s@v1
67+
68+
- name: Preview
69+
id: preview
70+
env:
71+
CHANGED_STACKS: ${{ needs.get-changed-project-stack.outputs.changed_stacks }}
72+
OSS_ACCESS_KEY_ID: '${{ secrets.OSS_ACCESS_KEY_ID }}'
73+
OSS_ACCESS_KEY_SECRET: '${{ secrets.OSS_ACCESS_KEY_SECRET }}'
74+
WORKSPACE_FILE_DIR: workspaces
75+
run: |
76+
# manually source kusion env file
77+
source "$HOME/.kusion/.env"
78+
79+
# setup remote backend for kusion cli
80+
kusion config set backends.oss_test '{"type":"oss","configs":{"bucket":"kusion-test","endpoint":"oss-cn-shanghai.aliyuncs.com"}}'
81+
kusion config set backends.current oss_test
82+
83+
# execute python script for previewing
84+
python3 hack/preview_changed_stacks.py
85+
86+
- name: Upload report
87+
id: upload-report
88+
if: ${{ steps.preview.outputs.preview_success == 'true' }}
89+
uses: actions/upload-artifact@v4
90+
with:
91+
name: preview-report
92+
path: hack/report/preview-result.zip
93+
94+
- name: Echo URL
95+
if: ${{ steps.preview.outputs.preview_success == 'true' }}
96+
run: echo "${{ steps.upload-report.outputs.artifact-url }}"
97+
98+
- name: Approve preview
99+
if: ${{ steps.preview.outputs.preview_success == 'true' }}
100+
uses: trstringer/manual-approval@v1
101+
with:
102+
secret: ${{ secrets.PACKAGE_TOKEN }}
103+
approvers: ${{ github.event.pull_request.user.login }}
104+
minimum-approvals: 1
105+
issue-title: "Deploying ${{ needs.get-changed-project-stack.outputs.changed_stacks }}"
106+
issue-body: "Please check the preview report at: ${{ steps.upload-report.outputs.artifact-url }}"

hack/apply_changed_stacks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def apply_stacks(stack_dirs: List[str]) -> bool:
3434

3535
def apply(stack_dir: str):
3636
print(f"Apply stack {stack_dir}...")
37-
cmd = [KUSION_CMD, APPLY_CMD, YES_FLAG, NO_STYLE_FLAG]
37+
cmd = [KUSION_CMD, APPLY_CMD, YES_FLAG, NO_STYLE_FLAG, NO_WATCH_FLAG]
3838
process = subprocess.run(
3939
cmd, capture_output=True, cwd=Path(stack_dir), env=dict(os.environ)
4040
)
@@ -62,7 +62,7 @@ def pack_result_files():
6262

6363

6464
stack_dirs = util.get_changed_stacks()
65-
util.create_workspaces(stack_dirs)
65+
# util.create_workspaces(stack_dirs)
6666
success = apply_stacks(stack_dirs)
6767
if success:
6868
pack_result_files()

hack/lib/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
NO_STYLE_FLAG = "--no-style"
2525
YES_FLAG = "--yes"
2626
FILE_FLAG = "--file"
27+
NO_DETAIL_FLAG = "--detail=false"
28+
NO_WATCH_FLAG = "--watch=false"
2729

2830
IGNORE_PROJECTS = ["example/wordpress"]

hack/lib/util.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,31 @@ def should_ignore_stack(stack_path: str) -> bool:
4040
return False
4141

4242

43-
def create_workspaces(stack_paths: List[str]):
44-
workspaces = detect_workspaces(stack_paths)
45-
workspace_file_dir = get_workspace_file_dir()
46-
for workspace in workspaces:
47-
create_workspace(workspace, workspace_file_dir)
48-
49-
50-
def create_workspace(workspace: str, workspace_file_dir: str):
51-
workspace_file = workspace_file_path(workspace_file_dir, workspace)
52-
cmd = [KUSION_CMD, WORKSPACE_CMD, CREATE_CMD, workspace, FILE_FLAG, workspace_file]
53-
process = subprocess.run(
54-
cmd, capture_output=True, env=dict(os.environ)
55-
)
56-
if process.returncode != 0:
57-
raise Exception(f"Create workspace {workspace} with file {workspace_file} failed",
58-
f"stdout = {process.stdout.decode().strip()}",
59-
f"stderr = {process.stderr.decode().strip()}",
60-
f"returncode = {process.returncode}")
61-
62-
63-
def detect_workspaces(stack_paths: List[str]) -> List[str]:
64-
workspaces = []
65-
for stack_path in stack_paths:
66-
workspaces.append(get_stack_name(Path(stack_path)))
67-
return list(set(filter(None, workspaces)))
43+
# def create_workspaces(stack_paths: List[str]):
44+
# workspaces = detect_workspaces(stack_paths)
45+
# workspace_file_dir = get_workspace_file_dir()
46+
# for workspace in workspaces:
47+
# create_workspace(workspace, workspace_file_dir)
48+
49+
50+
# def create_workspace(workspace: str, workspace_file_dir: str):
51+
# workspace_file = workspace_file_path(workspace_file_dir, workspace)
52+
# cmd = [KUSION_CMD, WORKSPACE_CMD, CREATE_CMD, workspace, FILE_FLAG, workspace_file]
53+
# process = subprocess.run(
54+
# cmd, capture_output=True, env=dict(os.environ)
55+
# )
56+
# if process.returncode != 0:
57+
# raise Exception(f"Create workspace {workspace} with file {workspace_file} failed",
58+
# f"stdout = {process.stdout.decode().strip()}",
59+
# f"stderr = {process.stderr.decode().strip()}",
60+
# f"returncode = {process.returncode}")
61+
62+
63+
# def detect_workspaces(stack_paths: List[str]) -> List[str]:
64+
# workspaces = []
65+
# for stack_path in stack_paths:
66+
# workspaces.append(get_stack_name(Path(stack_path)))
67+
# return list(set(filter(None, workspaces)))
6868

6969

7070
def get_stack_name(stack_dir: Path) -> str:

hack/preview_changed_stacks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def preview_stacks(stack_dirs: List[str]) -> bool:
3434

3535
def preview(stack_dir: str):
3636
print(f"Preview stack {stack_dir}...")
37-
cmd = [KUSION_CMD, PREVIEW_CMD, NO_STYLE_FLAG]
37+
cmd = [KUSION_CMD, PREVIEW_CMD, NO_STYLE_FLAG, NO_DETAIL_FLAG]
3838
process = subprocess.run(
3939
cmd, capture_output=True, cwd=Path(stack_dir), env=dict(os.environ)
4040
)
@@ -62,7 +62,7 @@ def pack_result_files():
6262

6363

6464
stack_dirs = util.get_changed_stacks()
65-
util.create_workspaces(stack_dirs)
65+
# util.create_workspaces(stack_dirs)
6666
success = preview_stacks(stack_dirs)
6767
if success:
6868
pack_result_files()

0 commit comments

Comments
 (0)