gitlab-ci-artifacts-caching

$npx mdskill add TheBushidoCollective/han/gitlab-ci-artifacts-caching

Optimize build speed by configuring GitLab CI artifacts and caching.

  • Enables inter-job data passing and faster builds through artifact management.
  • Integrates with GitLab CI YAML syntax for pipeline configuration.
  • Selects cache strategies based on file paths and build conditions.
  • Outputs optimized pipeline configurations directly into CI files.

SKILL.md

.github/skills/gitlab-ci-artifacts-cachingView on GitHub ↗
---
name: gitlab-ci-artifacts-caching
user-invocable: false
description: Use when configuring artifacts for inter-job data passing or caching for faster builds. Covers cache strategies and artifact management.
allowed-tools:
  - Read
  - Write
  - Edit
  - Bash
  - Grep
  - Glob
---

# GitLab CI - Artifacts & Caching

Configure artifacts and caching for efficient pipeline execution.

## Artifacts

### Basic Artifact Configuration

```yaml
build:
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week
```

### Artifact Reports

```yaml
test:
  script:
    - npm test -- --coverage
  artifacts:
    reports:
      junit: junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml
```

### Conditional Artifacts

```yaml
build:
  artifacts:
    paths:
      - dist/
    when: on_success  # on_success, on_failure, always
    exclude:
      - dist/**/*.map
```

### Artifact Dependencies

```yaml
build:
  artifacts:
    paths:
      - dist/

test:
  dependencies:
    - build  # Downloads build artifacts
  script:
    - npm test

deploy:
  dependencies: []  # Skip all artifact downloads
  script:
    - ./deploy.sh
```

## Caching

### Basic Cache Configuration

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

### Cache Key Strategies

```yaml
# Per-branch cache
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

# Lock file based cache
cache:
  key:
    files:
      - package-lock.json
  paths:
    - node_modules/

# Combined key
cache:
  key:
    prefix: ${CI_JOB_NAME}
    files:
      - package-lock.json
  paths:
    - node_modules/
```

### Cache Policy

```yaml
install:
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
    policy: push  # Only upload cache
  script:
    - npm ci

test:
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
    policy: pull  # Only download cache
  script:
    - npm test
```

### Fallback Keys

```yaml
cache:
  key: ${CI_COMMIT_REF_SLUG}
  fallback_keys:
    - ${CI_DEFAULT_BRANCH}
    - main
  paths:
    - node_modules/
```

## Distributed Cache (S3)

Configure in GitLab Runner:

```toml
[runners.cache]
  Type = "s3"
  Shared = true
  [runners.cache.s3]
    ServerAddress = "s3.amazonaws.com"
    BucketName = "gitlab-runner-cache"
    BucketLocation = "us-east-1"
```

## Artifacts vs Cache

| Feature | Artifacts | Cache |
|---------|-----------|-------|
| Purpose | Pass data between jobs | Speed up job execution |
| Storage | GitLab server | Runner local or S3 |
| Reliability | Guaranteed | Best effort |
| Expiration | Configurable | Configurable |
| Cross-pipeline | Yes (with dependencies) | Yes (with keys) |

## Best Practices

1. Use cache for dependencies (node_modules, vendor)
2. Use artifacts for build outputs
3. Set appropriate expiration times
4. Use lock file-based cache keys
5. Exclude source maps and unnecessary files
6. Use `policy: pull` for jobs that only read cache

More from TheBushidoCollective/han

SkillDescription
absinthe-resolversUse when implementing GraphQL resolvers with Absinthe. Covers resolver patterns, dataloader integration, batching, and error handling.
absinthe-schemaUse when designing GraphQL schemas with Absinthe. Covers type definitions, interfaces, unions, enums, and schema organization patterns.
absinthe-subscriptionsUse when implementing real-time GraphQL subscriptions with Absinthe. Covers Phoenix channels, PubSub, and subscription patterns.
act-docker-setupUse when configuring Docker environments for act, selecting runner images, managing container resources, or troubleshooting Docker-related issues with local GitHub Actions testing.
act-local-testingUse when testing GitHub Actions workflows locally with act. Covers act CLI usage, Docker configuration, debugging workflows, and troubleshooting common issues when running workflows on your local machine.
act-workflow-syntaxUse when creating or modifying GitHub Actions workflow files. Provides guidance on workflow syntax, triggers, jobs, steps, and expressions for creating valid GitHub Actions workflows that can be tested locally with act.
ameba-configurationUse when configuring Ameba rules and settings for Crystal projects including .ameba.yml setup, rule management, severity levels, and code quality enforcement.
ameba-custom-rulesUse when creating custom Ameba rules for Crystal code analysis including rule development, AST traversal, issue reporting, and rule testing.
ameba-integrationUse when integrating Ameba into development workflows including CI/CD pipelines, pre-commit hooks, GitHub Actions, and automated code review processes.
analyze-performanceAnalyze performance metrics and identify slow transactions in Sentry