Configure a GitLab CI/CD Pipeline with Multi-Stage Deployment
Create a production-grade GitLab CI/CD pipeline with linting, testing, security scanning, and multi-environment deployments.
๐ The Prompt
Generate a complete `.gitlab-ci.yml` configuration for a [PROJECT_TYPE] project (e.g., Python microservice, React SPA, Go CLI tool) that implements a multi-stage CI/CD pipeline.
**Project Context:**
- Language/Runtime: [LANGUAGE_AND_VERSION]
- Build tool: [BUILD_TOOL]
- Deployment targets: [DEPLOYMENT_TARGETS] (e.g., AWS ECS, Kubernetes, S3 + CloudFront)
- Branch strategy: `main` (production), `develop` (staging), feature branches (review)
**Pipeline Stages to Include:**
1. **๐ Lint & Static Analysis**: Run [LINTER_TOOLS] with fail-on-warning. Cache linter dependencies.
2. **๐งช Test**: Execute unit and integration tests with coverage threshold of [COVERAGE_PERCENT]%. Generate JUnit XML reports and Cobertura coverage reports for GitLab merge request integration.
3. **๐๏ธ Build**: Compile/bundle the application. For containerized apps, build a Docker image using multi-stage Dockerfile, tag with `$CI_COMMIT_SHORT_SHA` and `latest`, and push to [CONTAINER_REGISTRY].
4. **๐ Security Scan**: Integrate SAST, dependency scanning, and container scanning using GitLab templates or [SECURITY_TOOL].
5. **๐ Deploy Staging**: Auto-deploy to staging on `develop` branch merges. Use [DEPLOYMENT_METHOD] with environment URL and rollback capability.
6. **๐ Deploy Production**: Manual approval gate for production deployment from `main`. Include smoke test job that runs after deployment.
**Also include:**
- Global caching strategy for [DEPENDENCY_MANAGER] (e.g., npm, pip, go mod)
- `rules:` syntax (not `only/except`) for conditional job execution
- Environment-specific variables using GitLab CI/CD variable groups
- `needs:` for DAG pipeline optimization
- Retry configuration for flaky network-dependent jobs
- Clear inline comments explaining each decision
๐ก Tips for Better Results
Specify your exact runner tags and executor type (Docker, Kubernetes, shell) since image availability and caching strategies differ significantly. Use the `needs` keyword to create a DAG pipeline โ this can cut total pipeline time by 40-60% compared to sequential stages. Store secrets as masked and protected CI/CD variables and reference them by name in your prompt so the output uses proper variable substitution.
๐ฏ Use Cases
DevOps engineers and development teams use this when setting up or modernizing their GitLab CI/CD pipeline, especially during new project bootstrapping or migration from other CI systems like Jenkins or GitHub Actions.