Skip to content
Skills Library
DevOps & cloud · Automation

gitlab-ecr-pipeline

/gitlab-ecr-pipeline

Use when creating or modifying GitLab CI/CD pipelines that push container images to AWS ECR - supports building from Containerfile/Dockerfile and mirroring upstream images

Repository
srepowers ↗
Licence
MIT
Last repository change
09.11.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
4
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 09.11.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Use when creating or modifying GitLab CI/CD pipelines that push container images to AWS ECR - supports building from Containerfile/Dockerfile and mirroring upstream images

Original author description

Use when creating or modifying GitLab CI/CD pipelines that push container images to AWS ECR - supports building from Containerfile/Dockerfile and mirroring upstream images

How it works

  1. Quick Start
  2. Workflow 1: Build & Push
  3. Workflow 2: Mirror & Push
  4. Common Patterns
  5. Creating the ECR Repository
  6. Troubleshooting
  7. Resources
  8. SRE Principles
  9. Related Reference Documentation
Original SKILL.md headings · 424 lines, 1,654 words
SKILL.md preview
---
name: gitlab-ecr-pipeline
description: Use when creating or modifying GitLab CI/CD pipelines that push container images to AWS ECR - supports building from Containerfile/Dockerfile and mirroring upstream images
---

# GitLab ECR Pipeline

Generate GitLab CI/CD pipelines that push container images to Amazon Elastic Container Registry (ECR). This skill provides tested patterns for two common workflows:

1. **Build & Push** - Build a container image from Containerfile/Dockerfile and push to ECR
2. **Mirror & Push** - Pull an upstream image, re-tag it, and push to ECR

Both patterns use **Podman** instead of Docker to avoid Docker-in-Docker issues in GitLab CI runners.

## Quick Start

Identify the workflow needed:

| Workflow | When to Use | Source |
|----------|-------------|--------|
| Build & Push | Building images from source code | Containerfile/Dockerfile in repo |
| Mirror & Push | Mirroring third-party images to private ECR | Upstream image registry (Docker Hub, GHCR, Quay) |

Then read the corresponding section below for the complete pipeline template.

---

## Workflow 1: Build & Push

Build a container image from a Containerfile (or Dockerfile) in the repository and push it to AWS ECR.

### Prerequisites

Required GitLab CI/CD variables:

| Variable | Description | Example |
|----------|-------------|---------|
| `ECR_AWS_ACCESS_KEY_ID` | AWS Access Key ID with ECR push permissions | `AKIAIOSFODNN7EXAMPLE` (⚠️ DO NOT use - example only) |
| `ECR_AWS_SECRET_ACCESS_KEY` | AWS Secret Access Key | `wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY` (⚠️ DO NOT use - example only) |
| `ECR_AWS_REGION` | AWS region for ECR | `ap-northeast-1` |
| `ECR_AWS_ACCOUNT_ID` | AWS account ID (12 digits) | `123456789012` |

### Pipeline Template

```yaml
variables:
  ECR_REGISTRY: $ECR_AWS_ACCOUNT_ID.dkr.ecr.$ECR_AWS_REGION.amazonaws.com
  IMAGE_NAME: my-app
  CONTAINER_IMAGE: $ECR_REGISTRY/$IMAGE_NAME
  CONTAINER_ENGINE: podman

stages:
  - build
  - test
  - release

# Build container image and push to AWS ECR
build:
  stage: build
  image: quay.io/podman/stable:latest
  before_script:
    # Install AWS CLI
    - microdnf install -y python3 python3-pip
    - pip3 install awscli
    # Configure AWS credentials
    - export AWS_ACCESS_KEY_ID=$ECR_AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$ECR_AWS_SECRET_ACCESS_KEY
    - export AWS_DEFAULT_REGION=$ECR_AWS_REGION
    # Login to ECR
    - echo "Logging into ECR..."
    - aws ecr get-login-password --region $ECR_AWS_REGION |
      $CONTAINER_ENGINE login --username AWS --password-stdin $ECR_REGISTRY
  script:
    - echo "Building container image..."
    - $CONTAINER_ENGINE build -t $IMAGE_NAME:$CI_COMMIT_SHORT_SHA -f Containerfile .

    # Tag with commit SHA
    - $CONTAINER_ENGINE tag $IMAGE_NAME:$CI_COMMIT_SHORT_SHA $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA

    # Tag as latest for main branch
    - |
      if [ "$CI_COMMIT_BRANCH" = "main" ]; then
        echo "Tagging as latest..."
        $CONTAINER_ENGINE tag $IMAGE_NAME:$CI_COMMIT_SHORT_SHA $CONTAINER_IMAGE:latest
      fi

    # Push to ECR
    - echo "Pushing image to ECR..."
    - $CONTAINER_ENGINE push $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA
    - |
      if [ "$CI_COMMIT_BRANCH" = "main" ]; then
        $CONTAINER_ENGINE push $CONTAINER_IMAGE:latest
      fi

    - echo "Image pushed successfully:"
    - echo "  $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA"
  rules:
    - if: $CI_COMMIT_BRANCH
    - if: $CI_COMMIT_TAG
  tags:
    - shared

# Optional: Test the built image
test:
  stage: test
  image: quay.io/podman/stable:latest
  before_script:
    - microdnf install -y python3 python3-pip
    - pip3 install awscli
    - export AWS_ACCESS_KEY_ID=$ECR_AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$ECR_AWS_SECRET_ACCESS_KEY
    - export AWS_DEFAULT_REGION=$ECR_AWS_REGION
    - aws ecr get-login-password --region $ECR_AWS_REGION |
      $CONTAINER_ENGINE login --username AWS --password-stdin $ECR_REGISTRY
  script:
    - echo "Pulling image from ECR..."
    - $CONTAINER_ENGINE pull $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA
    - echo "Running tests..."
    - $CONTAINER_ENGINE run --rm --entrypoint <command> $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA <args>
    - echo "All tests passed!"
  dependencies:
    - build
  rules:
    - if: $CI_COMMIT_BRANCH
  tags:
    - shared

# Release tagged versions
release:
  stage: release
  image: quay.io/podman/stable:latest
  before_script:
    - microdnf install -y python3 python3-pip
    - pip3 install awscli
    - export AWS_ACCESS_KEY_ID=$ECR_AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$ECR_AWS_SECRET_ACCESS_KEY
    - export AWS_DEFAULT_REGION=$ECR_AWS_REGION
    - aws ecr get-login-password --region $ECR_AWS_REGION |
      $CONTAINER_ENGINE login --username AWS --password-stdin $ECR_REGISTRY
  script:
    - echo "Creating release for tag $CI_COMMIT_TAG..."
    - $CONTAINER_ENGINE pull $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA

    # Tag with semantic version
    - $CONTAINER_ENGINE tag $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA $CONTAINER_IMAGE:$CI_COMMIT_TAG

    # Create major and minor version tags
    - export MAJOR=$(echo $CI_COMMIT_TAG | sed 's/v//' | cut -d. -f1)
    - export MINOR=$(echo $CI_COMMIT_TAG | sed 's/v//' | cut -d. -f1-2)
    - $CONTAINER_ENGINE tag $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA $CONTAINER_IMAGE:v$MAJOR
    - $CONTAINER_ENGINE tag $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA $CONTAINER_IMAGE:v$MINOR

    # Push all version tags
    - $CONTAINER_ENGINE push $CONTAINER_IMAGE:$CI_COMMIT_TAG
    - $CONTAINER_ENGINE push $CONTAINER_IMAGE:v$MAJOR
    - $CONTAINER_ENGINE push $CONTAINER_IMAGE:v$MINOR
  rules:
    - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/
  dependencies:
    - build
  tags:
    - shared
```

### Customization Points

| Variable | Change to |
|----------|----------|
| `IMAGE_NAME` | Your application name |
| `CONTAINER_ENGINE` | `docker` if using Docker (requires DinD service) |
| Containerfile path | `-f path/to/Dockerfile` if not in root |
| Test command | Modify `--entrypoint` and `<args>` for your app |

---

## Workflow 2: Mirror & Push

Pull an upstream container image from a public registry, re-tag it for ECR, and push. Use this when mirroring third-party images to your private registry.

### Pipeline Template

```yaml
variables:
  GRAVITY_IMAGE: ghcr.io/beryju/gravity
  GRAVITY_TAG: stable

stages:
  - publish

# Push upstream image to AWS ECR on tag
publish:ecr:
  stage: publish
  image: quay.io/podman/stable:latest
  before_script:
    # Install AWS CLI
    - microdnf install -y python3 python3-pip
    - pip3 install awscli
    # Configure AWS credentials
    - export AWS_ACCESS_KEY_ID=${ECR_AWS_ACCESS_KEY_ID}
    - export AWS_SECRET_ACCESS_KEY=${ECR_AWS_SECRET_ACCESS_KEY}
    - export AWS_DEFAULT_REGION=${ECR_AWS_REGION}
    # Login to ECR
    - echo "Logging into AWS ECR..."
    - aws ecr get-login-password --region ${ECR_AWS_REGION} |
      podman login --username AWS --password-stdin ${ECR_AWS_ACCOUNT_ID}.dkr.ecr.${ECR_AWS_REGION}.amazonaws.com
  script:
    - VERSION=${CI_COMMIT_TAG#v}
    - ECR_REGISTRY="${ECR_AWS_ACCOUNT_ID}.dkr.ecr.${ECR_AWS_REGION}.amazonaws.com"
    - ECR_REPOSITORY="${ECR_REGISTRY}/gravity"
    - echo "Pushing image to ECR ${ECR_REPOSITORY}:${VERSION}"

    # Pull upstream image
    - echo "Pulling upstream image..."
    - podman pull ${GRAVITY_IMAGE}:${GRAVITY_TAG}

    # Tag for ECR
    - echo "Tagging images..."
    - podman tag ${GRAVITY_IMAGE}:${GRAVITY_TAG} ${ECR_REPOSITORY}:${VERSION}
    - podman tag ${GRAVITY_IMAGE}:${GRAVITY_TAG} ${ECR_REPOSITORY}:latest

    # Push to ECR
    - echo "Pushing to ECR..."
    - podman push ${ECR_REPOSITORY}:${VERSION}
    - podman push ${ECR_REPOSITORY}:latest

    - echo "Image pushed successfully:"
    - echo "  ${ECR_REPOSITORY}:${VERSION}"
    - echo "  ${ECR_REPOSITORY}:latest"
  rules:
    - if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/
  tags:
    - shared
```

### Customization Points

| Variable | Change to |
|----------|----------|
| `GRAVITY_IMAGE` | Upstream image repository |
| `GRAVITY_TAG` | Upstream image tag (e.g., `stable`, `latest`, `v1.0.0`) |
| `ECR_REPOSITORY` | ECR repository name (after registry/) |

---

## Common Patterns

### ECR Login (Standard Pattern)

All ECR jobs require this login sequence:

```yaml
before_script:
  - microdnf install -y python3 python3-pip
  - pip3 install awscli
  - export AWS_ACCESS_KEY_ID=$ECR_AWS_ACCESS_KEY_ID
  - export AWS_SECRET_ACCESS_KEY=$ECR_AWS_SECRET_ACCESS_KEY
  - export AWS_DEFAULT_REGION=$ECR_AWS_REGION
  - aws ecr get-login-password --region $ECR_AWS_REGION |
    podman login --username AWS --password-stdin $ECR_REGISTRY
```

### Tag Patterns

| Git Variable | Description | Example Output |
|--------------|-------------|----------------|
| `$CI_COMMIT_SHORT_SHA` | First 8 characters of commit SHA | `f2ac2bc` |
| `${CI_COMMIT_TAG#v}` | Tag without 'v' prefix | `1.2.3` from `v1.2.3` |
| `$CI_COMMIT_TAG` | Full tag | `v1.2.3` |

### Version Tagging Strategy

For semantic versioning, create multiple tags:

```bash
# Given tag v1.2.3
export MAJOR=$(echo $CI_COMMIT_TAG | sed 's/v//' | cut -d. -f1)    # 1
export MINOR=$(echo $CI_COMMIT_TAG | sed 's/v//' | cut -d. -f1-2)  # 1.2
# Results in tags: v1.2.3, v1.2, v1
```

### Why Podman Instead of Docker?

Podman is recommended for GitLab CI/CD because:

1. **No DinD service required** - Docker-in-Docker adds complexity and can fail with proxy/firewall issues
2. **Daemonless** - No background daemon needed
3. **Drop-in compatible** - Same CLI syntax as Docker

To use Docker instead, add the service and adjust configuration:

```yaml
image: docker:24-cli
services:
  - docker:24-dind
variables:
  DOCKER_HOST: tcp://docker:2376
  DOCKER_TLS_CERTDIR: "/certs"
```

---

## Creating the ECR Repository

Before the pipeline runs, create the ECR repository:

```bash
aws ecr create-repository \
  --repository-name my-app \
  --region ap-northeast-1 \
  --image-scanning-configuration scanOnPush=true \
  --encryption-configuration encryptionType=AES256
```

Optional: Add lifecycle policy to keep only last N images:

```bash
aws ecr put-lifecycle-policy \
  --repository-name my-app \
  --region ap-northeast-1 \
  --lifecycle-policy-text '{
    "rules": [
      {
        "rulePriority": 1,
        "description": "Keep last 10 images",
        "selection": {
          "tagStatus": "any",
          "countType": "imageCountMoreThan",
          "countNumber": 10
        },
        "action": {
          "type": "expire"
        }
      }
    ]
  }'
```

---

## Troubleshooting

### Job fails with "Access Denied"

**Cause:** Missing or invalid AWS credentials.

**Solution:** Verify all four ECR variables are set in GitLab CI/CD configuration.

### Job fails with "link is not supported"

**Cause:** Docker-in-Docker service incompatibility with GitLab runner.

**Solution:** Switch to Podman (see templates above).

### "no basic auth credentials" error

**Cause:** ECR login failed.

**Solution:** Check `ECR_AWS_REGION` and `ECR_AWS_ACCOUNT_ID` variables are correct.

### Pod image pull fails in cluster

**Cause:** Kubernetes nodes cannot pull from ECR.

**Solution:** Configure IAM role for nodes or create image pull secret.

---

## Resources

### references/build-push-template.yml
Complete Build & Push pipeline template with all stages included.

### references/mirror-push-template.yml
Complete Mirror & Push pipeline template for mirroring upstream images.

---

## SRE Principles

### Safety First
- Include a `validate` stage before `build` to verify ECR repository exists and credentials are valid
- Use `podman build --no-cache` for reproducible builds; verify image with `podman inspect` before push
- Phase structure: **Pre-check** (validate ECR access, verify Containerfile syntax) → **Execute** (build and push image) → **Verify** (confirm image digest in ECR, run vulnerability scan)

### Structured Output
- Present pipeline stages using summary tables (stage, duration, status, artifacts produced)
- Use image metadata tables (tag, digest, size, scan status, push timestamp)
- Include tag strategy documentation in tabular format (branch pattern, tag format, example)

### Evidence-Driven
- Reference image digests (`podman images --digests`) to confirm push integrity
- Include ECR scan results with specific CVE IDs and severity counts
- Cite pipeline job IDs, timestamps, and commit SHAs for every image pushed

### Audit-Ready
- Log image digests, tags, and push timestamps for every pipeline run
- Maintain image promotion history (which digest was promoted from staging to production, when, by whom)
- Document rollback procedure with exact commands to revert to previous image tag

### Communication
- Lead with deployment impact (e.g., "New image pipeline reduces build-to-deploy from 30min to 5min")
- Present image security posture in summary format (total CVEs by severity, scan pass/fail)
- Communicate pipeline failures with clear root cause and remediation steps for the team

## Related Reference Documentation

For detailed ECR setup, cross-account configuration, and authentication guidance, refer to the **container-cicd-reference** skill (auto-loaded):

- **ECR Repository Setup** - `container-cicd-reference/references/aws-ecr-setup.md`
  - Complete ECR repository creation and configuration
  - Authentication methods and best practices
  - Lifecycle policies and cost optimization

- **Cross-Account ECR Access** - `container-cicd-reference/references/aws-ecr-cross-account.md`
  - Multi-account deployment architecture
  - Repository policy configuration
  - Cross-account IAM permissions

- **IAM Authentication** - `container-cicd-reference/references/aws-iam-auth.md`
  - IAM User vs IAM Role decision guide
  - GitLab CI/CD authentication patterns
  - Security and maintenance considerations

- **GitLab Registry Check** - `container-cicd-reference/references/gitlab-registry-check.md`
  - Verify GitLab Container Registry availability
  - Configuration validation methods
  - Alternative registry options
Files in this package (4)
  • README.md 2 KB
  • SKILL.md 14 KB
  • references/build-push-template.yml 4 KB
  • references/mirror-push-template.yml 2 KB

Versions

The database has no other skills named gitlab-ecr-pipeline.

Database snapshot: 13.11.2026 · package version 461d44eaec5e (SHA-256 digest of the entire package)

Downloads in the last 30 days: 3 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100gitlab-cicd
    Use when creating, modifying, or debugging GitLab CI/CD pipelines on self-hosted GitLab instances — writing .gitlab-ci.yml files, managing GitLab Runners, using the glab CLI, automating GitLab API operations, debugging failed pipeline jobs, or setting up multi-environment…
  • 100gitlab-cicd-debug
    Debug GitLab CI/CD pipeline failures including vault auth, docker builds, K8s deployments, and job errors. Use when: (1) Pipeline jobs fail, (2) Vault JWT auth issues, (3) Docker build/push errors, (4) K8s deployment failures, (5) "permission denied" or "unauthorized" errors,…
  • 100container-registries
    Use when the user needs to publish or push container images, set up Docker registry authentication, configure CI/CD pipelines for container deployment, or work with GHCR, Docker Hub, ECR, or ACR. Creates and updates publishing workflows, tagging strategy, and multi-architecture…
  • 100ci-cd-gitlab-ci-pipelines
    Use this skill when building multi-stage continuous integration and deployment pipelines using GitLab CI/CD .gitlab-ci.yml.
  • 100container-registries
    Manage container registries including ECR, ACR, GCR, and Docker Hub. Push and pull images, configure authentication, set up repository policies, and implement image lifecycle management. Use when working with container image storage and distribution.
  • 90image
    Prepare the commands to build and push a container image to ECR, with a unique tag and matching platform, without running them. Use when publishing an image.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.