Skip to content

Instantly share code, notes, and snippets.

@tanmay-bhat
Last active July 11, 2024 06:33
Show Gist options
  • Save tanmay-bhat/6fa65b9cd9d5f7f5e780dbe3efcb1fb7 to your computer and use it in GitHub Desktop.
Save tanmay-bhat/6fa65b9cd9d5f7f5e780dbe3efcb1fb7 to your computer and use it in GitHub Desktop.
gitlab-CI file to build the Docker image and pushes to ECR
image: "python:3.6" # base dockerimage on which the stages will run. Can be different for each stage.
stages: # each stage runs on a new Docker image. data is not persisted between stages by default.
- publish_image # build docker image and push to registry
build and push docker image:
stage: publish_image
only: # only runs the step when git tag is done and it matches one of the mentioned regex patterns
variables:
- $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+-[0-9]+\.[0-9]+\.[0-9]+$/
- $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/
variables:
DOCKER_REGISTRY: <your-ecr-address>.amazonaws.com # your ECR registry address
AWS_DEFAULT_REGION: ap-south-1 #region of your ECR repository
APP_NAME: myapp #your repo name for the image
DOCKER_HOST: tcp://docker:2375
image:
name: amazon/aws-cli
entrypoint: [""]
services:
- docker:dind
before_script:
- echo "$CI_COMMIT_TAG"
- amazon-linux-extras install docker
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build -t $DOCKER_REGISTRY/$APP_NAME:$DOCKER_TAG .
- aws ecr get-login-password | docker login --username AWS --password-stdin $DOCKER_REGISTRY
- docker push $DOCKER_REGISTRY/$APP_NAME:$CI_COMMIT_TAG
- docker push $DOCKER_REGISTRY/$APP_NAME:$DOCKER_TAG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment