Last active
June 11, 2024 00:17
-
-
Save miguelmota/c13bd2f5cc5493c82689c40117846571 to your computer and use it in GitHub Desktop.
Makefile docker push to AWS Elastic Container Registry (ECR)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Login to AWS registry (must have docker running) | |
docker-login: | |
$$(aws ecr get-login --no-include-email --region us-east-1 --profile=mycompany) | |
# Build docker target | |
docker-build: | |
docker build -f Dockerfile --no-cache -t mycompany/myapp . | |
# Tag docker image | |
docker-tag: | |
$(eval REV=$(shell git rev-parse HEAD | cut -c1-7)) | |
docker tag mycompany/myapp:latest 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:latest | |
docker tag mycompany/myapp:latest 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:$(REV) | |
# Push to registry | |
docker-push: | |
$(eval REV=$(shell git rev-parse HEAD | cut -c1-7)) | |
docker push 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:latest | |
docker push 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:$(REV) | |
# Build docker image and push to AWS registry | |
docker-build-and-push: docker-login docker-build docker-tag docker-push |
❤️ $$(...)
This one works for me:
repo-login: ## Auto login to AWS-ECR unsing aws-cli
$(eval AWS_ACCOUNT_ID=$(shell aws sts get-caller-identity --query Account --output text))
aws ecr get-login-password --region $(AWS_CLI_REGION) | docker login --password-stdin --username AWS "$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_CLI_REGION).amazonaws.com"
I ended up with this Makefile, which uses get-login-password and uses variables to reduce duplication:
.phony: all
all: docker-build docker-push
IMG_NAME = myapp
$(eval AWS_ACCOUNT_ID=$(shell aws sts get-caller-identity --query Account --output text))
$(eval REV=$(shell git rev-parse HEAD | cut -c1-7))
$(eval REG=$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com)
$(eval REPO=$(REG)/$(IMG_NAME))
# Login to ECR
ecr-login:
aws ecr get-login-password --region $(AWS_REGION) | docker login --password-stdin --username AWS $(REG)
# Build and tag docker image
docker-build:
docker build -f Dockerfile --no-cache -t $(IMG_NAME) .
# Push docker image
docker-push: ecr-login
docker tag $(IMG_NAME):latest $(REPO):latest
docker tag $(IMG_NAME):latest $(REPO):$(REV)
docker push $(REPO):latest
docker push $(REPO):$(REV)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answered my own question, haha. Use bash: