Create a GitLab CI/CD Pipeline Configuration for Multi-Stage Deployments
Build a multi-stage GitLab CI/CD pipeline with caching, testing, Docker builds, and manual production deployment gates.
๐ The Prompt
Generate a complete `.gitlab-ci.yml` configuration file for a [PROJECT_TYPE] project (e.g., Node.js microservice, Python Django app, Java Spring Boot) that deploys to [DEPLOYMENT_TARGET] (e.g., Kubernetes, AWS ECS, bare metal).
**Pipeline Stages** โ Define and configure these stages in order:
1. **Build**:
- Use the [DOCKER_IMAGE] base image
- Cache [DEPENDENCY_DIRECTORY] (e.g., `node_modules`, `.venv`, `.m2`) using a key based on the lock file hash
- Build the application artifact and store it using `artifacts:paths`
2. **Test**:
- Run unit tests with [TEST_FRAMEWORK] and generate a JUnit XML report for GitLab's test visualization
- Run linting with [LINTER_TOOL]
- Run security scanning using `sast` or a custom [SECURITY_SCANNER] job
- Set `coverage` regex to parse coverage percentage from stdout
3. **Build Docker Image** (if containerized):
- Use Docker-in-Docker or Kaniko for building
- Tag images with `$CI_COMMIT_SHORT_SHA` and `latest` for the default branch
- Push to [CONTAINER_REGISTRY]
4. **Deploy Staging**:
- Deploy to the staging environment with `environment: name: staging`
- Run smoke tests after deployment as a verification step
- Trigger only on `merge_request` events to the `main` branch
5. **Deploy Production**:
- Require `when: manual` approval
- Only trigger on tagged releases matching `v*.*.*`
- Include rollback instructions as comments
**Additional Configuration**:
- Define global variables for [ENVIRONMENT_VARIABLES]
- Use `rules` (not `only/except`) for all conditional logic
- Add `retry: 2` for flaky network-dependent jobs
- Include a `workflow:rules` block to prevent duplicate pipelines
Add comments explaining each decision and any GitLab-specific gotchas.
๐ก Tips for Better Results
1. Always use `rules` instead of the deprecated `only/except` syntax โ it gives you more granular control and avoids duplicate pipeline issues. 2. Test your pipeline in a branch first using `CI_DEBUG_TRACE: 'true'` to diagnose caching and artifact problems. 3. Pin your CI image versions (e.g., `node:20.11-alpine` not `node:latest`) to prevent unexpected build failures.
๐ฏ Use Cases
DevOps engineers and development teams use this when setting up or migrating CI/CD pipelines in GitLab for projects requiring multi-environment deployment workflows.
๐ Related Prompts
๐ป Coding
beginner
Explain Code Like Im a Beginner
Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.
๐ป Coding
beginner
Debug My Code and Explain the Fix
Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.
๐ป Coding
intermediate
Write Unit Tests for My Code
Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.
๐ป Coding
intermediate
Convert Code Between Languages
Convert code between any programming languages while maintaining idiomatic patterns and best practices.
๐ป Coding
intermediate
Write a REST API Endpoint
Generate production-ready REST API endpoints with validation, error handling, and documentation.
๐ป Coding
advanced
Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment
Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.