Gitlab ci

From j4flmao/agent-skills · View interactive version

Gitlab ci is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about GitLab CI, .gitlab-ci.yml, stages and needs, rules, parent-child or multi-project pipelines, include templates, CI/CD components, GitLab runners, container builds, or GitLab SAST/DAST scanning. Produces GitLab pipeline configs. Not for GitHub Actions (use github-actions)

How to use this skill

Skill file

GitLab CI/CD

Purpose

Design, implement, and optimize GitLab CI/CD pipelines for automated build, test, security scanning, and deployment workflows.

Agent Protocol

Trigger

Exact user phrases: "GitLab CI", ".gitlab-ci.yml", "GitLab pipeline", "GitLab runner", "GitLab CI/CD", "CI pipeline", "GitLab security scanning", "SAST", "DAST", "child pipeline", "multi-project pipeline".

Input Context

Output Artifact

Writes to .gitlab-ci.yml, template YAML files in ci/ directory, and GitLab CI configuration.

Response Format

YAML configuration with GitLab CI keywords, ready to commit.

No preamble. No postamble. No explanations.

Completion Criteria

Architecture / Decision Trees

Pipeline Structure Decision Tree

Runner Selection

| Runner Type | Use Case | Pros | Cons |

|---|---|---|---|

| Shared (SaaS) | Small projects, quick start | No maintenance, auto-scaled | Limited concurrency, no custom HW |

| Group runner | Team-level standardization | Shared across projects, custom config | Team manages |

| Specific runner | Single project, custom HW | Full control, GPU/dedicated | Per-project maintenance |

| Auto-scaling (Docker Machine) | Variable load | Cost efficient, elastic | Complex setup |

| Auto-scaling (Kubernetes) | K8s-native, containerized | Native K8s, efficient | Requires K8s cluster |

Build Method Decision Tree

Security Scanning Integration

| Scan Type | GitLab Template | When to Run | Best For |

|---|---|---|---|

| SAST | Jobs/SAST.gitlab-ci.yml | Every PR | Source code vulnerabilities |

| Secret Detection | Jobs/Secret-Detection.gitlab-ci.yml | Every push | Hardcoded credentials |

| Dependency Scanning | Jobs/Dependency-Scanning.gitlab-ci.yml | Every PR | Known CVEs in deps |

| Container Scanning | Jobs/Container-Scanning.gitlab-ci.yml | After image build | Image vulnerabilities |

| DAST | Jobs/DAST.gitlab-ci.yml | Deploy to review app | Runtime vulnerabilities |

| API Fuzzing | Jobs/API-Fuzzing.gitlab-ci.yml | Deploy to staging | API security testing |

| License Scanning | Jobs/License-Scanning.gitlab-ci.yml | Every PR | License compliance |

Core Workflow

Step 1: Basic Pipeline Structure

stages:
  - build
  - test
  - security
  - package
  - deploy

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  DEPLOY_ENV: $CI_COMMIT_BRANCH
  REGISTRY: $CI_REGISTRY_IMAGE
  TAG: $CI_COMMIT_SHORT_SHA

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .npm/

build-app:
  stage: build
  image: node:22-alpine
  script:
    - npm ci --cache .npm --prefer-offline
    - npm run build
  artifacts:
    paths:
      - dist/
      - node_modules/
    expire_in: 1 hour

unit-test:
  stage: test
  image: node:22-alpine
  script:
    - npm ci --cache .npm --prefer-offline
    - npm run test:ci
  artifacts:
    when: always
    reports:
      junit: test-results/junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

Step 2: Security Scanning

include:
  - template: Jobs/SAST.gitlab-ci.yml
  - template: Jobs/Secret-Detection.gitlab-ci.yml
  - template: Jobs/Dependency-Scanning.gitlab-ci.yml

sast:
  stage: security
  variables:
    SAST_EXCLUDED_PATHS: node_modules, tests, vendor
    SEARCH_MAX_DEPTH: 10
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == "main"

secret_detection:
  stage: security
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

dependency_scanning:
  stage: security
  variables:
    DS_DEFAULT_ANALYZERS: node
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

Step 3: Container Build and Push

container-build:
  stage: package
  image: quay.io/buildah/stable:<pinned-version>   # pin a tested tag or digest
  variables:
    STORAGE_DRIVER: vfs
    BUILDAH_ISOLATION: chroot
  script:
    - echo "$CI_REGISTRY_PASSWORD" | buildah login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
    - buildah build --layers --cache-to "$CI_REGISTRY_IMAGE/cache" --cache-from "$CI_REGISTRY_IMAGE/cache"
        -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" .
    - buildah push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"

container-scan:
  stage: security
  image:
    name: aquasec/trivy:<pinned-version>
    entrypoint: [""]
  script:
    - trivy image --severity CRITICAL,HIGH --format sarif --output trivy.sarif $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
  artifacts:
    reports:
      sast: trivy.sarif
  needs: [container-build]

Step 4: Deployment with Rules

.deploy-template: &deploy-template
  image: alpine:3.19
  before_script:
    - apk add --no-cache kubectl gettext
    - kubectl config use-context $KUBE_CONTEXT
  script:
    - envsubst < k8s/deployment.yaml | kubectl apply -f -
    - kubectl rollout status deployment/$APP_NAME -n $NAMESPACE --timeout=5m

deploy-staging:
  <<: *deploy-template
  stage: deploy
  variables:
    NAMESPACE: staging
    APP_NAME: myapp-staging
  environment:
    name: staging
    url: https://staging.example.com
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual
      allow_failure: true

deploy-production:
  <<: *deploy-template
  stage: deploy
  variables:
    NAMESPACE: production
    APP_NAME: myapp-prod
  environment:
    name: production
    url: https://example.com
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual
      allow_failure: false

Step 5: Child/Parent Pipelines (Monorepo)

# Parent .gitlab-ci.yml
stages:
  - triggers

trigger-backend:
  stage: triggers
  trigger:
    include: backend/.gitlab-ci.yml
    strategy: depend
  rules:
    - changes:
        - backend/**/*

trigger-frontend:
  stage: triggers
  trigger:
    include: frontend/.gitlab-ci.yml
    strategy: depend
  rules:
    - changes:
        - frontend/**/*

Step 6: Multi-Project Pipelines

trigger-lib:
  stage: triggers
  trigger:
    project: team/lib
    branch: main
    strategy: depend
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Step 7: YAML Anchors and Extends

.job-template:
  image: node:22-alpine
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
  before_script:
    - npm ci

lint:
  extends: .job-template
  stage: test
  script:
    - npm run lint

test:
  extends: .job-template
  stage: test
  script:
    - npm run test:ci
  coverage: '/Coverage: \d+\.\d+%/'

Step 8: Review Apps

review-deploy:
  stage: deploy
  image: alpine:3.19
  script:
    - apk add --no-cache kubectl
    - kubectl create namespace review-$CI_MERGE_REQUEST_IID || true
    - kubectl set image deployment/app app=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA -n review-$CI_MERGE_REQUEST_IID || true
  environment:
    name: review/$CI_MERGE_REQUEST_IID
    url: https://review-$CI_MERGE_REQUEST_IID.example.com
    on_stop: review-cleanup
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

review-cleanup:
  stage: deploy
  script:
    - kubectl delete namespace review-$CI_MERGE_REQUEST_IID || true
  environment:
    name: review/$CI_MERGE_REQUEST_IID
    action: stop
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: manual

Anti-Patterns

Anti-Pattern 1: Hardcoded Secrets

Writing secrets in .gitlab-ci.yml exposes them in pipeline logs and to anyone with repo access. Use CI/CD variables with masked and protected flags.

Anti-Pattern 2: No Caching

Without caching, every pipeline reinstalls all dependencies. Cache node_modules, .m2, vendor/bundle. Use key based on lock file hash.

Anti-Pattern 3: Using only/except

The only/except keywords are deprecated and less flexible. Use rules for all conditional logic. Rules support if, changes, exists, and variables.

Anti-Pattern 4: No Artifact Expiration

Artifacts without expire_in accumulate indefinitely, filling storage quotas. Set expire_in on all artifacts (1 hour for temporary, 30 days for reports).

Anti-Pattern 5: Auto-Deploy to Production

Automatically deploying to production without manual approval risks deploying broken code. Always use when: manual for production deployments.

Anti-Pattern 6: Overusing DIND

Docker-in-Docker requires privileged mode, which bypasses security controls. Use a daemonless builder (Buildah, rootless BuildKit) on K8s runners. Reserve DIND for when Docker socket is required.

Anti-Pattern 7: Too Many Stages

Having too many stages slows pipeline execution (each stage completes before next starts). Combine independent jobs into the same stage using needs for DAG execution.

Production Considerations

Pipeline Performance

Security

Monitoring

Rules

Compared With

GitLab CI vs GitHub Actions

GitLab CI: single app for code + CI, auto DevOps, built-in registry, self-managed option. GitHub Actions: marketplace ecosystem, simpler YAML, tighter GitHub integration. GitLab CI excels at security scanning and Kubernetes deployment.

GitLab CI vs Jenkins

GitLab CI: cloud-native, simpler configuration, integrated with GitLab. Jenkins: extensive plugin ecosystem, any VCS, mature. GitLab CI is better for GitLab-centric teams; Jenkins for complex multi-VCS organizations.

DIND vs daemonless builders

DIND: requires privileged mode and a full Docker daemon. Buildah / rootless BuildKit: no privileged mode, safer on shared K8s runners. Kaniko (GoogleContainerTools) was archived in June 2025 and gets no upstream fixes; if you keep it, use a maintained fork (e.g. Chainguard's) or move to Buildah/BuildKit rootless.

Operations & Maintenance

Runner Management

Pipeline Maintenance

References

Handoff

After completing this skill:

Architecture Decision Trees

GitLab CI vs GitLab CI + External CI

| Decision | GitLab CI Only | Hybrid (Jenkins/Argo) |

|---|---|---|

| Simplicity | Single platform | Multiple platforms to manage |

| Pipeline as Code | Full .gitlab-ci.yml | Split across tools |

| Features | Built-in registry, SAST, DAST | Specialized tool capabilities |

| Migration | Easier (all in GitLab) | Harder (multi-tool) |

| Cost | Same license | Additional licenses |

| Best for | GitLab-native orgs | Existing CI investments |

Docker vs Shell Executor

| Aspect | Docker Executor | Shell Executor |

|---|---|---|

| Isolation | Full container isolation | Same host environment |

| Reproducibility | High (same image everywhere) | Low (depends on host state) |

| Performance | Slight overhead | Native speed |

| Cache | Volume mounts | Direct filesystem access |

| Use case | Standard CI/CD | Performance-critical or hardware access |

Implementation Patterns

YAML: Multi-project Pipeline with Downstream Triggers

stages:
  - build
  - test
  - package
  - deploy

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .npm/

build-app:
  stage: build
  image: node:22
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

unit-test:
  stage: test
  image: node:22
  script:
    - npm ci
    - npm run test:unit
    - npm run test:coverage
  coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

deploy-staging:
  stage: deploy
  image: alpine/k8s:1.28
  script:
    - kubectl set image deployment/app app=${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}
  environment:
    name: staging
    url: https://staging.example.com
  only:
    - main

trigger-downstream:
  stage: deploy
  trigger:
    project: myorg/e2e-tests
    branch: main
    strategy: depend
  only:
    - tags

Bash: GitLab CI Pipeline Status Check

#!/usr/bin/env bash
wait_for_pipeline() {
  local project_id=$1
  local pipeline_id=$2
  local token=$3

  while true; do
    status=$(curl -sf --header "PRIVATE-TOKEN: $token" \
      "https://gitlab.com/api/v4/projects/${project_id}/pipelines/${pipeline_id}" \
      | jq -r '.status')

    echo "Pipeline status: $status"

    case "$status" in
      success) return 0 ;;
      failed|canceled) return 1 ;;
      *) sleep 10 ;;
    esac
  done
}

Production Considerations

Anti-Patterns

Performance Optimization

Security Considerations

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.