Example
This pipeline clones a git repository and uses finally tasks to ensure cleanup happens regardless of success or failure.How It Works
Clone Git Repository
The
clone-app-repo task uses a git-clone task to clone the repository into a subdirectory of the shared workspace. It outputs the commit SHA as a result.Regular Tasks Complete
In a real pipeline, you would have additional tasks here (like build, test, etc.) that use the cloned source code.
Finally Tasks Execute
The
finally section contains tasks that run after all regular tasks complete, regardless of success or failure. This ensures cleanup always happens.Cleanup Workspace
The
cleanup task removes the cloned repository from the workspace. It verifies the directory exists before cleanup and verifies it’s gone afterward.Finally Tasks
Thefinally section runs tasks that:
- Execute after all regular tasks complete
- Run regardless of whether tasks succeeded or failed
- Can access results from regular tasks using
$(tasks.NAME.results.RESULT) - Are useful for cleanup, notifications, and status reporting
Git Clone Task Pattern
The git-clone task follows a common pattern: Inputs:url: Repository URLrevision: Branch, tag, or SHA to checkout (default: main)subdirectory: Where to clone within the workspace- Various other options (depth, SSL verify, proxies, etc.)
commit: The exact SHA that was cloned
output: Where the repository will be cloned
Workspace Storage
The PipelineRun uses avolumeClaimTemplate to dynamically provision storage:
- Exists only for this PipelineRun
- Is automatically cleaned up when the PipelineRun is deleted
- Is accessible to all tasks in the pipeline
Cleanup Task
The cleanup task demonstrates defensive cleanup:Expected Output
Clone task:Key Concepts
- Finally Tasks: Tasks that always run at the end of a pipeline
- Git Clone: Common pattern for checking out source code
- Workspace Cleanup: Removing sensitive or large data after pipeline completes
- Volume Claim Templates: Dynamic storage provisioning per PipelineRun
- Task Results: Passing commit SHAs and other metadata between tasks
- Defensive Programming: Verifying state before and after operations
Real-World Extensions
In production, you might add:- Build tasks: Compile code, run tests
- Image building: Using Kaniko or Buildah
- Security scanning: Scan dependencies and images
- Deployment: Push to registry, deploy to cluster
- Notifications: Send status to Slack, email, etc.
Next Steps
- See a complete multi-stage pipeline
- Learn about building Docker images