diff --git a/.github/workflows/agentkit-build.yaml b/.github/workflows/agentkit-build.yaml new file mode 100644 index 0000000..fd14c3e --- /dev/null +++ b/.github/workflows/agentkit-build.yaml @@ -0,0 +1,52 @@ +name: agentkit-build + +on: + pull_request: + types: + - closed + workflow_dispatch: + inputs: + base_sha: + description: "Base commit SHA (optional)" + required: false + head_sha: + description: "Head commit SHA (optional)" + required: false + +jobs: + agentkit-build: + if: > + ${{ (github.event_name == 'pull_request' && github.event.pull_request.merged == true) || github.event_name == 'workflow_dispatch' }} + runs-on: ubuntu-latest + + env: + VOLCENGINE_ACCESS_KEY: ${{ secrets.VOLCENGINE_ACCESS_KEY }} + VOLCENGINE_SECRET_KEY: ${{ secrets.VOLCENGINE_SECRET_KEY }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install AgentKit CLI + run: | + python -m pip install --upgrade pip + pip install agentkit-sdk-python + + - name: Generate .env for CI + run: | + cat > .env << 'EOF' + EOF + + - name: Run main.py in changed use-case directories (build) + env: + BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.inputs.base_sha }} + HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.event.inputs.head_sha }} + AGENTKIT_COMMAND: build + run: | + python -m workflow_utils.check_usecases diff --git a/.github/workflows/agentkit-check.yaml b/.github/workflows/agentkit-check.yaml new file mode 100644 index 0000000..5c95469 --- /dev/null +++ b/.github/workflows/agentkit-check.yaml @@ -0,0 +1,47 @@ +name: agentkit-check + +on: + pull_request: + workflow_dispatch: + inputs: + base_sha: + description: "Base commit SHA (optional)" + required: false + head_sha: + description: "Head commit SHA (optional)" + required: false + +jobs: + agentkit-launch: + runs-on: ubuntu-latest + + env: + VOLCENGINE_ACCESS_KEY: ${{ secrets.VOLCENGINE_ACCESS_KEY }} + VOLCENGINE_SECRET_KEY: ${{ secrets.VOLCENGINE_SECRET_KEY }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install AgentKit CLI + run: | + python -m pip install --upgrade pip + pip install agentkit-sdk-python + + - name: Generate .env for CI + run: | + cat > .env << 'EOF' + EOF + + - name: Run main.py in changed use-case directories + env: + BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.inputs.base_sha }} + HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.event.inputs.head_sha }} + run: | + python -m workflow_utils.check_usecases diff --git a/workflow_utils/__init__.py b/workflow_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/workflow_utils/check_usecases.py b/workflow_utils/check_usecases.py new file mode 100644 index 0000000..bc6634b --- /dev/null +++ b/workflow_utils/check_usecases.py @@ -0,0 +1,110 @@ +import os +import subprocess +import sys +from pathlib import Path + + +def get_changed_files(base_sha: str, head_sha: str) -> list[str]: + if not base_sha or not head_sha: + return [] + try: + output = subprocess.check_output( + ["git", "diff", "--name-only", base_sha, head_sha], + text=True, + stderr=subprocess.STDOUT, + ) + except subprocess.CalledProcessError as exc: + sys.stderr.write(exc.output) + return [] + return [line.strip() for line in output.splitlines() if line.strip()] + + +def main() -> None: + base_sha = os.environ.get("BASE_SHA", "") + head_sha = os.environ.get("HEAD_SHA", "") + + changed = get_changed_files(base_sha, head_sha) + changed_use_cases = [p for p in changed if p.startswith("02-use-cases/")] + + if not changed_use_cases: + print("No changes under 02-use-cases, skipping main.py checks.") + return + + candidate_dirs: set[Path] = set() + for rel_path in changed_use_cases: + parts = Path(rel_path).parts + if len(parts) >= 2 and parts[0] == "02-use-cases" and parts[1] != "beginner": + candidate_dirs.add(Path(parts[0]) / parts[1]) + + if not candidate_dirs: + print( + "No top-level 02-use-cases/* directories detected, skipping main.py checks." + ) + return + + print("Use-case directories to check:") + for d in sorted(candidate_dirs): + print(f" - {d}") + + failed_dirs: list[Path] = [] + + for d in sorted(candidate_dirs): + # deploy_sh = d / "deploy.sh" + # if deploy_sh.is_file(): + # print(f"Found deploy.sh in {d}, running it") + # result = subprocess.run(["bash", "deploy.sh"], cwd=str(d)) + # if result.returncode != 0: + # failed_dirs.append(d) + # continue + + agent_py = d / "agent.py" + + if not agent_py.is_file(): + print(f"No agent.py in {d}, skipping agentkit commands now.") + continue + + agent_name = d.name + workflow_name = os.environ.get("GITHUB_WORKFLOW", "") + if workflow_name == "agentkit-check": + agent_name = f"check_{agent_name}" + print(f"Running 'agentkit config' in {d} for agent_name={agent_name}") + config_cmd = [ + "agentkit", + "config", + "--agent_name", + agent_name, + "--entry_point", + "agent.py", + "--description", + "a helpful agent", + "--launch_type", + "cloud", + "--image_tag", + "v1.0.0", + "--region", + "cn-beijing", + ] + result = subprocess.run(config_cmd, cwd=str(d)) + if result.returncode != 0: + failed_dirs.append(d) + print(f"'agentkit config' failed in {d}, skipping launch.") + continue + + command = os.environ.get("AGENTKIT_COMMAND", "launch") + print(f"Running 'agentkit {command}' in {d}") + launch_cmd = ["agentkit", command] + result = subprocess.run(launch_cmd, cwd=str(d)) + if result.returncode != 0: + failed_dirs.append(d) + + if failed_dirs: + sys.stderr.write( + "agentkit checks failed in directories: " + + ", ".join(str(d) for d in failed_dirs) + + "\n" + ) + raise SystemExit(1) + + +if __name__ == "__main__": + main()