Configure a GitLab CI Pipeline with Multi-Stage Jobs and Environments

Build a multi-stage GitLab CI pipeline with testing, security scanning, Docker builds, and environment-based deployments.

๐Ÿ“ The Prompt

Create a complete `.gitlab-ci.yml` configuration for a [PROJECT_TYPE] application built with [TECH_STACK]. The project uses [PACKAGE_MANAGER] for dependency management and targets deployment to [INFRASTRUCTURE]. **Pipeline Stages & Jobs:** 1. **stages**: define `lint`, `test`, `build`, `security`, `deploy-staging`, `deploy-production` 2. **Global Configuration**: - Set `image: [BASE_DOCKER_IMAGE]` - Define `variables` for [GLOBAL_VARIABLES_LIST] - Configure `cache` with key `$CI_COMMIT_REF_SLUG` for [CACHE_PATHS] - Add `before_script` to install dependencies 3. **Lint Job**: Run [LINTER] with `allow_failure: false` and generate a code quality report artifact in Code Climate format 4. **Test Job**: - Execute [TEST_COMMAND] with coverage reporting - Use `services:` to spin up [SERVICE_CONTAINERS] (e.g., postgres:15, redis:7) - Parse coverage with `coverage: '/Total.*?(\d+\.\d+)%/'` - Store JUnit XML reports as artifacts for MR integration 5. **Build Job**: Build the application artifact or Docker image using [BUILD_TOOL] and push to GitLab Container Registry with tags `$CI_COMMIT_SHA` and `$CI_COMMIT_REF_NAME` 6. **Security Job**: Run `SAST`, `dependency_scanning`, and `container_scanning` using GitLab templates with `include:` 7. **Deploy Staging**: Deploy to staging environment using [DEPLOY_METHOD] with `environment: name: staging, url: [STAGING_URL]`. Trigger automatically on `develop` branch. 8. **Deploy Production**: Deploy to production with `when: manual` approval gate, `environment: name: production, url: [PROD_URL]`, and `only: [main/tags]`. Include a `rollback` job. Add `rules:` or `only/except` for branch-specific logic. Include retry logic, timeout settings, and `needs:` for DAG-based parallelism where appropriate.

๐Ÿ’ก Tips for Better Results

Use `needs:` instead of relying purely on stage ordering to enable parallel execution and speed up your pipeline. Always pin your Docker image versions in CI to avoid unexpected breakages. Use GitLab's `include:` templates for SAST and dependency scanning rather than writing custom jobs.

๐ŸŽฏ Use Cases

DevOps teams and platform engineers use this when configuring GitLab CI for projects that require structured deployment environments, security compliance gates, and manual production approval 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.