Complete DevOps Pipeline Orchestration with Apply Patch & Shell Tools#
This notebook demonstrates a complete end-to-end DevOps pipeline orchestration using AG2βs support for OpenAIβs built-in apply_patch and shell tools. The system creates projects from scratch, develops code, tests, fixes issues, sets up CI/CD, builds, and deploys applications.
Overview#
This orchestration showcases:
- Apply Patch Tool: For structured file operations (create, update, delete files)
- Shell Tool: For executing commands (tests, builds, deployments, validation)
Use Case: Full Lifecycle DevOps Pipeline#
From project creation β coding β testing β fixing β CI/CD setup β deployment β validation
Orchestration Flow#
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β USER REQUEST β
β "Create a complete web application" β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π― PIPELINE ORCHESTRATOR β
β β’ Analyzes request β
β β’ Routes to appropriate agent β
β β’ Coordinates workflow β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ποΈ PROJECT CREATOR β
β Uses APPLY_PATCH tool: β
β β’ Create project structure β
β β’ Initialize config files β
β β’ Set up directories β
β β’ Create initial templates β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π» CODE DEVELOPER β
β Uses APPLY_PATCH tool: β
β β’ Write application code β
β β’ Implement features β
β β’ Create modules and classes β
β β’ Write unit tests β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π CODE QUALITY ANALYZER β
β Uses SHELL tool: β
β β’ Run test suites (pytest, unittest) β
β β’ Run linters (pylint, flake8) β
β β’ Check git status β
β β’ Analyze code coverage β
β β’ Check build status β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
β β
Issues Found? All Checks Pass?
β β
βΌ βΌ
βββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β π§ CODE FIXER β β βοΈ CONFIGURATION MANAGER β
β Uses APPLY_PATCH tool: β β Uses APPLY_PATCH tool: β
β β’ Fix bugs β β β’ Create CI/CD configs β
β β’ Resolve linting errors β β β’ Update environment files β
β β’ Refactor code β β β’ Create Docker configs β
β β’ Add missing tests β β β’ Update deployment scripts β
βββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β β
βββββββββββ¬ββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π BUILD & DEPLOY AGENT β
β Uses SHELL tool: β
β β’ Install dependencies β
β β’ Build applications β
β β’ Run deployment scripts β
β β’ Push to repositories β
β β’ Manage containers β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
DEPLOYMENT VALIDATOR β
β Uses SHELL tool: β
β β’ Run health checks β
β β’ Execute smoke tests β
β β’ Check service status β
β β’ Verify endpoints β
β β’ Monitor resource usage β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
β β
All Checks Pass? Issues Found?
β β
βΌ βΌ
βββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β π― PIPELINE ORCHESTRATORβ β Route back to CODE FIXER β
β Report SUCCESS β β or CONFIG MANAGER β
βββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β USER RECEIVES REPORT β
β Complete pipeline status and results β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Agent Responsibilities#
1. π― Pipeline Orchestrator#
- Role: Central coordinator
- Responsibilities:
- Analyze user requests
- Route tasks to appropriate agents
- Monitor pipeline progress
- Report final results
- Tools Used: None (coordination only)
2. ποΈ Project Creator#
- Role: Initialize new projects
- Responsibilities:
- Create project structure
- Generate initial files (README, .gitignore, LICENSE)
- Set up configuration files
- Create directory structure
- Tools Used: APPLY_PATCH (file creation)
3. π» Code Developer#
- Role: Write application code
- Responsibilities:
- Implement features
- Create modules and classes
- Add business logic
- Write unit tests
- Tools Used: APPLY_PATCH (code writing)
4. π Code Quality Analyzer#
- Role: Test and analyze code quality
- Responsibilities:
- Run test suites
- Execute linters
- Check code coverage
- Validate build status
- Tools Used: SHELL (command execution)
5. π§ Code Fixer#
- Role: Fix code issues
- Responsibilities:
- Fix bugs
- Resolve linting errors
- Refactor code
- Add missing tests
- Tools Used: APPLY_PATCH (code modifications)
6. βοΈ Configuration Manager#
- Role: Manage configurations
- Responsibilities:
- Create CI/CD configs
- Update environment files
- Create Docker configs
- Update deployment scripts
- Tools Used: APPLY_PATCH (config file operations)
7. π Build & Deploy Agent#
- Role: Build and deploy applications
- Responsibilities:
- Install dependencies
- Build applications
- Run deployment scripts
- Manage containers
- Tools Used: SHELL (build and deployment commands)
8. β Deployment Validator#
- Role: Validate deployments
- Responsibilities:
- Run health checks
- Execute smoke tests
- Check service status
- Verify endpoints
- Tools Used: SHELL (validation commands)
Installation and Setup#
Import Dependencies#
import os
from dotenv import load_dotenv
from autogen import ConversableAgent, LLMConfig, UserProxyAgent
from autogen.agentchat import initiate_group_chat
from autogen.agentchat.group.patterns.auto import AutoPattern
load_dotenv()
Configure LLM with Built-in Tools#
We configure the LLM to use both apply_patch_async and shell tools:
- apply_patch_async: For structured file operations (create, update, delete)
- shell: For executing shell commands (tests, builds, deployments)
- workspace_dir: Directory where all operations will be performed
# Configure LLM with both built-in tools
llm_config = LLMConfig(
config_list={
"api_type": "responses",
"model": "gpt-5.1",
"api_key": os.getenv("OPENAI_API_KEY"),
"built_in_tools": ["apply_patch_async", "shell"],
"workspace_dir": "./project_dir",
},
)
Agent Definitions#
Agent 1: Pipeline Orchestrator#
The orchestrator acts as the central coordinator, analyzing requests and routing to appropriate agents.
orchestrator = ConversableAgent(
name="pipeline_orchestrator",
system_message="""π― You are the Pipeline Orchestrator. Your role is to:
β’ Analyze user requests and determine the workflow path
β’ Route tasks to appropriate specialized agents
β’ Monitor pipeline progress and coordinate handoffs
β’ Report final results to the user
Workflow Decision Logic:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. New project request? β Route to project_creator β
β 2. Code development? β Route to code_developer β
β 3. Code analysis needed? β Route to code_quality_analyzerβ
β 4. Configuration updates? β Route to config_manager β
β 5. Build/deploy request? β Route to build_deploy_agent β
β 6. Validation needed? β Route to deployment_validator β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Always provide clear routing decisions with reasoning.""",
llm_config=llm_config,
)
Agent 2: Project Creator#
Uses APPLY_PATCH tool to create project structure and initial files.
project_creator = ConversableAgent(
name="project_creator",
system_message="""ποΈ You are the Project Creator. Your role is to:
Use the APPLY_PATCH tool to:
β Create project structure and directories
β Generate initial files (README.md, .gitignore, LICENSE)
β Set up configuration files (package.json, requirements.txt, pom.xml)
β Create initial source code templates
β Set up test directory structure
Project Creation Checklist:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β β‘ Project directory structure created? β
β β‘ Configuration files initialized? β
β β‘ Initial source code files created? β
β β‘ Test directory structure set up? β
β β‘ Documentation files added? β
ββββββββββββββββββββββββββββββββββββββββββββββββ
After project creation:
β Route to code_developer for implementation
Create clean, well-structured project foundations.""",
llm_config=llm_config,
)
Agent 3: Code Developer#
Uses APPLY_PATCH tool to write application code and implement features.
code_developer = ConversableAgent(
name="code_developer",
system_message="""π» You are the Code Developer. Your role is to:
Use the APPLY_PATCH tool to:
β Write application code and implement features
β Create modules, classes, and functions
β Add business logic and algorithms
β Implement API endpoints and routes
β Write unit tests for new code
Development Guidelines:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. Follow best practices and coding standardsβ
β 2. Write clean, maintainable code β
β 3. Include proper error handling β
β 4. Add comprehensive unit tests β
β 5. Document complex logic β
ββββββββββββββββββββββββββββββββββββββββββββββββ
After development:
β Route to code_quality_analyzer for testing and validation
Write production-ready code with tests!""",
llm_config=llm_config,
)
Agent 4: Code Quality Analyzer#
Uses SHELL tool to run tests, linters, and analyze code quality.
code_analyzer = ConversableAgent(
name="code_quality_analyzer",
system_message="""π You are the Code Quality Analyzer. Your role is to:
Use the SHELL tool to:
β Run test suites: pytest, unittest, npm test, mvn test
β Run linters: pylint, flake8, black --check, eslint, prettier
β Check git status: git status, git diff, git log
β Analyze code coverage: coverage report, pytest --cov
β Check build status: npm run build, mvn compile
Analysis Checklist:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β β‘ All tests passing? β
β β‘ No linting errors? β
β β‘ Code coverage acceptable? β
β β‘ No merge conflicts? β
β β‘ Build successful? β
ββββββββββββββββββββββββββββββββββββββββββββββββ
Routing Rules:
β’ Issues found β Route to code_fixer
β’ All checks pass β Route to config_manager for CI/CD setup
Provide detailed analysis reports with specific issues identified.""",
llm_config=llm_config,
)
Agent 5: Code Fixer#
Uses APPLY_PATCH tool to fix bugs and resolve issues found during analysis.
code_fixer = ConversableAgent(
name="code_fixer",
system_message="""π§ You are the Code Fixer. Your role is to:
Use the APPLY_PATCH tool to:
β Fix bugs identified by tests
β Resolve linting errors
β Refactor code for better quality
β Add missing tests
β Update documentation
Fixing Guidelines:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. Fix one issue at a time β
β 2. Use precise diffs (unified diff format) β
β 3. Maintain code style consistency β
β 4. Add tests for new fixes β
β 5. Update related documentation β
ββββββββββββββββββββββββββββββββββββββββββββββββ
After applying fixes:
β Always route back to code_quality_analyzer for re-validation
Never skip validation after fixes!""",
llm_config=llm_config,
)
Agent 6: Configuration Manager#
Uses APPLY_PATCH tool to create and update CI/CD configurations, Docker files, and deployment scripts.
config_manager = ConversableAgent(
name="config_manager",
system_message="""βοΈ You are the Configuration Manager. Your role is to:
Use the APPLY_PATCH tool to:
β Create/update CI/CD configurations (.github/workflows/*.yml, .gitlab-ci.yml)
β Create/update environment files (.env, .env.example, config.yaml)
β Create/update deployment scripts (deploy.sh, docker-compose.yml)
β Update build configurations (package.json, pom.xml, requirements.txt)
β Create Dockerfile and containerization configs
Configuration Types:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β β’ CI/CD Pipeline Configs β
β β’ Environment Variables β
β β’ Docker & Container Configs β
β β’ Build & Dependency Files β
β β’ Deployment Scripts β
ββββββββββββββββββββββββββββββββββββββββββββββββ
After updating configs:
β Route to build_deploy_agent for execution
Always validate config syntax before routing!""",
llm_config=llm_config,
)
Agent 7: Build & Deploy Agent#
Uses SHELL tool to install dependencies, build applications, and deploy services.
build_deploy_agent = ConversableAgent(
name="build_deploy_agent",
system_message="""π You are the Build & Deploy Agent. Your role is to:
Use the SHELL tool to:
β Install dependencies: npm install, pip install -r requirements.txt, mvn install
β Build applications: npm run build, mvn package, docker build
β Run deployment scripts: ./deploy.sh, kubectl apply
β Push to repositories: git push, docker push
β Manage containers: docker-compose up, docker run
β Start services: systemctl start, pm2 start
Build & Deploy Checklist:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β β‘ Dependencies installed correctly? β
β β‘ Build completed successfully? β
β β‘ Docker images built and tagged? β
β β‘ Deployment scripts executed? β
β β‘ Services started successfully? β
ββββββββββββββββββββββββββββββββββββββββββββββββ
After deployment:
β Route to deployment_validator for verification
Report build/deploy status with timestamps and logs.""",
llm_config=llm_config,
)
Agent 8: Deployment Validator#
Uses SHELL tool to validate deployments by running health checks and smoke tests.
deployment_validator = ConversableAgent(
name="deployment_validator",
system_message="""β
You are the Deployment Validator. Your role is to:
Use the SHELL tool to:
β Run health checks: curl http://endpoint/health, wget --spider
β Execute smoke tests: npm run smoke-test, pytest tests/smoke
β Check service status: docker ps, kubectl get pods, systemctl status
β Verify endpoints: curl API endpoints, check logs
β Monitor resource usage: top, htop, docker stats
Validation Checklist:
ββββββββββββββββββββββββββββββββββββββββββββββββ
β β‘ Health endpoints responding? β
β β‘ Smoke tests passing? β
β β‘ Services running correctly? β
β β‘ No critical errors in logs? β
β β‘ Performance metrics acceptable? β
ββββββββββββββββββββββββββββββββββββββββββββββββ
After validation:
β’ All checks pass β Route to pipeline_orchestrator (SUCCESS)
β’ Issues found β Route to code_fixer or config_manager (FAILURE)
Provide detailed validation reports with metrics.""",
llm_config=llm_config,
)
User Agent Configuration#
user = UserProxyAgent(
name="user", human_input_mode="TERMINATE", code_execution_config={"work_dir": "coding", "use_docker": False}
)
Orchestration Pattern Setup#
We use AutoPattern which allows the LLM to intelligently select the next speaker based on conversation context. This provides more dynamic and adaptive conversation flow compared to DefaultPattern.
pattern = AutoPattern(
initial_agent=orchestrator,
agents=[
orchestrator,
project_creator,
code_developer,
code_analyzer,
code_fixer,
config_manager,
build_deploy_agent,
deployment_validator,
],
user_agent=user,
group_manager_args={"llm_config": llm_config},
)
Execute the Pipeline#
This example creates a complete REST API for a task management system from scratch, going through the entire development lifecycle.
result, context, last_agent = initiate_group_chat(
pattern=pattern,
messages="""π Create a complete web application from scratch use apply patch and shell tools:
Project: Build a REST API for a task management system
Requirements:
1. Create project structure (Python FastAPI application)
2. Implement core features:
- Task CRUD operations
- User authentication
- Task filtering and search
3. Write comprehensive unit tests
4. Set up CI/CD pipeline (GitHub Actions)
5. Create Docker configuration
6. Build and deploy the application
7. Validate deployment
Start from project creation and go through the complete development lifecycle.""",
max_rounds=30,
)
Tool Usage Summary#
Apply Patch Tool Usage#
| Agent | Use Case | Operations |
|---|---|---|
| Project Creator | Create project structure | create_file for directories, config files, templates |
| Code Developer | Write application code | create_file, update_file for source code and tests |
| Code Fixer | Fix bugs and issues | update_file with unified diffs to fix code |
| Configuration Manager | Manage configs | create_file, update_file for CI/CD, Docker, env files |
,
| Agent | Use Case | Commands |
|---|---|---|
| Code Quality Analyzer | Test and analyze | pytest, pylint, flake8, git status, coverage |
| Build & Deploy Agent | Build and deploy | pip install, npm build, docker build, docker-compose up |
| Deployment Validator | Validate deployment | curl, pytest tests/smoke, docker ps, systemctl status |
Shell Tool Usage#
Key Benefits#
- Structured File Operations: Apply patch tool ensures precise, controlled file modifications
- Command Execution: Shell tool enables testing, building, and deployment automation
- Complete Lifecycle: From project creation to deployment validation
- Iterative Improvement: Automatic routing back to fixers when issues are found
- Production Ready: Real-world DevOps workflow implementation
Next Steps#
- Customize agent system messages for your specific use case
- Adjust workspace directory and paths
- Modify handoff conditions for different workflows
- Add more specialized agents as needed
- Integrate with external tools and services