aws-build-push-ecr-configuration

Configure your GitLab CI/CD pipeline to build and push Docker images to Amazon ECR. This guide provides a step-by-step process and necessary variables.

AWS ECR Configuration

GitLab CI/CD Configuration for AWS ECR

This document outlines the configuration required to build and push Docker images to Amazon Elastic Container Registry (ECR) using GitLab CI/CD.

Required Variables

Add the following environment variables in your GitLab project's CI/CD settings under "Variables":

  • AWS_ACCESS_KEY_ID: Your AWS access key ID.
  • AWS_SECRET_ACCESS_KEY: Your AWS secret access key.
  • AWS_DEFAULT_REGION: The AWS region where your ECR repository is located (e.g., us-east-1).
  • AWS_ACCOUNT_ID: Your AWS account ID.

Further Steps (Example .gitlab-ci.yml)

You will also need to configure your .gitlab-ci.yml file to utilize these variables and build/push your Docker image. A sample configuration is shown below:


stages:
  - build
  - deploy

build:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME

deploy:
  stage: deploy
  image: amazon/aws-cli
  script:
    - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
    - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$REPOSITORY_NAME:$CI_COMMIT_REF_NAME

Remember to replace $REPOSITORY_NAME with the name of your ECR repository.

Additional Resources

For more information, refer to the following resources: