Skip to content

Commit

Permalink
feat: add workflow yml for preview and apply (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 authored Jun 7, 2024
1 parent a6e8836 commit 7aa5c92
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 29 deletions.
96 changes: 96 additions & 0 deletions .github/workflows/apply.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Apply
on:
push:
branches:
- "main"

jobs:
get-changed-project-stack:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install Pytest Html
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

- name: Git Diff
id: git-diff
uses: technote-space/get-diff-action@v6

- name: Get Changed Projects and Stacks
id: get-changed-projects-and-stacks
env:
CHANGED_PATHS: ${{ steps.git-diff.outputs.diff }}
run: |
export CHANGED_PATHS="${{ steps.git-diff.outputs.diff }}"
python3 hack/get_changed_projects_and_stacks.py
- name: Print Changed Projects and Stacks
run: |
echo "Changed projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}"
echo "Changed stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}"
outputs:
changed_projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}
changed_stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}

apply:
needs: [ get-changed-project-stack ]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Kusion
run: curl https://www.kusionstack.io/scripts/install.sh | sh -s 0.12.0-rc.1

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install Pytest Html
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

- name: Setup K3d&K3s
uses: nolar/setup-k3d-k3s@v1

- name: Apply
id: apply
env:
CHANGED_STACKS: ${{ needs.get-changed-project-stack.outputs.changed_stacks }}
OSS_ACCESS_KEY_ID: '${{ secrets.OSS_ACCESS_KEY_ID }}'
OSS_ACCESS_KEY_SECRET: '${{ secrets.OSS_ACCESS_KEY_SECRET }}'
WORKSPACE_FILE_DIR: workspaces
run: |
# manually source kusion env file
source "$HOME/.kusion/.env"
# setup remote backend for kusion cli
kusion config set backends.oss_test '{"type":"oss","configs":{"bucket":"kusion-test","endpoint":"oss-cn-shanghai.aliyuncs.com"}}'
kusion config set backends.current oss_test
# execute python script for applying
python3 hack/apply_changed_stacks.py
- name: Upload Report
id: upload-report
if: ${{ steps.apply.outputs.apply_success == 'true' }}
uses: actions/upload-artifact@v4
with:
name: apply-report
path: hack/report/apply-result.zip

- name: Echo URL
if: ${{ steps.apply.outputs.apply_success == 'true' }}
run: |
echo "Please check the apply result at: ${{ steps.upload-report.outputs.artifact-url }}"
106 changes: 106 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: Preview

on:
pull_request:
branches:
- "main"

jobs:
get-changed-project-stack:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install Pytest Html
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

- name: Git Diff
id: git-diff
uses: technote-space/get-diff-action@v6

- name: Get Changed Projects and Stacks
id: get-changed-projects-and-stacks
env:
CHANGED_PATHS: ${{ steps.git-diff.outputs.diff }}
run: |
export CHANGED_PATHS="${{ steps.git-diff.outputs.diff }}"
python3 hack/get_changed_projects_and_stacks.py
- name: Print Changed Projects and Stacks
run: |
echo "Changed projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}"
echo "Changed stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}"
outputs:
changed_projects: ${{ steps.get-changed-projects-and-stacks.outputs.changed_projects }}
changed_stacks: ${{ steps.get-changed-projects-and-stacks.outputs.changed_stacks }}

preview:
needs: [ get-changed-project-stack ]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Kusion
run: curl https://www.kusionstack.io/scripts/install.sh | sh -s 0.12.0-rc.1

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install Pytest Html
run: python3 -m pip install pytest-html pyyaml -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

- name: Setup K3d&K3s
uses: nolar/setup-k3d-k3s@v1

- name: Preview
id: preview
env:
CHANGED_STACKS: ${{ needs.get-changed-project-stack.outputs.changed_stacks }}
OSS_ACCESS_KEY_ID: '${{ secrets.OSS_ACCESS_KEY_ID }}'
OSS_ACCESS_KEY_SECRET: '${{ secrets.OSS_ACCESS_KEY_SECRET }}'
WORKSPACE_FILE_DIR: workspaces
run: |
# manually source kusion env file
source "$HOME/.kusion/.env"
# setup remote backend for kusion cli
kusion config set backends.oss_test '{"type":"oss","configs":{"bucket":"kusion-test","endpoint":"oss-cn-shanghai.aliyuncs.com"}}'
kusion config set backends.current oss_test
# execute python script for previewing
python3 hack/preview_changed_stacks.py
- name: Upload report
id: upload-report
if: ${{ steps.preview.outputs.preview_success == 'true' }}
uses: actions/upload-artifact@v4
with:
name: preview-report
path: hack/report/preview-result.zip

- name: Echo URL
if: ${{ steps.preview.outputs.preview_success == 'true' }}
run: echo "${{ steps.upload-report.outputs.artifact-url }}"

- name: Approve preview
if: ${{ steps.preview.outputs.preview_success == 'true' }}
uses: trstringer/manual-approval@v1
with:
secret: ${{ secrets.PACKAGE_TOKEN }}
approvers: ${{ github.event.pull_request.user.login }}
minimum-approvals: 1
issue-title: "Deploying ${{ needs.get-changed-project-stack.outputs.changed_stacks }}"
issue-body: "Please check the preview report at: ${{ steps.upload-report.outputs.artifact-url }}"
4 changes: 2 additions & 2 deletions hack/apply_changed_stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def apply_stacks(stack_dirs: List[str]) -> bool:

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


stack_dirs = util.get_changed_stacks()
util.create_workspaces(stack_dirs)
# util.create_workspaces(stack_dirs)
success = apply_stacks(stack_dirs)
if success:
pack_result_files()
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions hack/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@
NO_STYLE_FLAG = "--no-style"
YES_FLAG = "--yes"
FILE_FLAG = "--file"
NO_DETAIL_FLAG = "--detail=false"
NO_WATCH_FLAG = "--watch=false"

IGNORE_PROJECTS = ["example/wordpress"]
50 changes: 25 additions & 25 deletions hack/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ def should_ignore_stack(stack_path: str) -> bool:
return False


def create_workspaces(stack_paths: List[str]):
workspaces = detect_workspaces(stack_paths)
workspace_file_dir = get_workspace_file_dir()
for workspace in workspaces:
create_workspace(workspace, workspace_file_dir)


def create_workspace(workspace: str, workspace_file_dir: str):
workspace_file = workspace_file_path(workspace_file_dir, workspace)
cmd = [KUSION_CMD, WORKSPACE_CMD, CREATE_CMD, workspace, FILE_FLAG, workspace_file]
process = subprocess.run(
cmd, capture_output=True, env=dict(os.environ)
)
if process.returncode != 0:
raise Exception(f"Create workspace {workspace} with file {workspace_file} failed",
f"stdout = {process.stdout.decode().strip()}",
f"stderr = {process.stderr.decode().strip()}",
f"returncode = {process.returncode}")


def detect_workspaces(stack_paths: List[str]) -> List[str]:
workspaces = []
for stack_path in stack_paths:
workspaces.append(get_stack_name(Path(stack_path)))
return list(set(filter(None, workspaces)))
# def create_workspaces(stack_paths: List[str]):
# workspaces = detect_workspaces(stack_paths)
# workspace_file_dir = get_workspace_file_dir()
# for workspace in workspaces:
# create_workspace(workspace, workspace_file_dir)


# def create_workspace(workspace: str, workspace_file_dir: str):
# workspace_file = workspace_file_path(workspace_file_dir, workspace)
# cmd = [KUSION_CMD, WORKSPACE_CMD, CREATE_CMD, workspace, FILE_FLAG, workspace_file]
# process = subprocess.run(
# cmd, capture_output=True, env=dict(os.environ)
# )
# if process.returncode != 0:
# raise Exception(f"Create workspace {workspace} with file {workspace_file} failed",
# f"stdout = {process.stdout.decode().strip()}",
# f"stderr = {process.stderr.decode().strip()}",
# f"returncode = {process.returncode}")


# def detect_workspaces(stack_paths: List[str]) -> List[str]:
# workspaces = []
# for stack_path in stack_paths:
# workspaces.append(get_stack_name(Path(stack_path)))
# return list(set(filter(None, workspaces)))


def get_stack_name(stack_dir: Path) -> str:
Expand Down
4 changes: 2 additions & 2 deletions hack/preview_changed_stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def preview_stacks(stack_dirs: List[str]) -> bool:

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


stack_dirs = util.get_changed_stacks()
util.create_workspaces(stack_dirs)
# util.create_workspaces(stack_dirs)
success = preview_stacks(stack_dirs)
if success:
pack_result_files()
Expand Down

0 comments on commit 7aa5c92

Please sign in to comment.