-
-
Save leonardobiffi/c518c232d5736256df5346e93b6866c2 to your computer and use it in GitHub Desktop.
Simple Makefile to build, run, tag and publish a docker containier to AWS-ECR
This file contains hidden or 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
| # Port to run the container | |
| PORT=4000 | |
| # Until here you can define all the individual configurations for your app |
This file contains hidden or 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
| # You have to define teh values in {} | |
| APP_NAME=my-super-app | |
| DOCKER_REPO={account-nr}.dkr.ecr.{region}.amazonaws.com | |
| # optional aws-cli options | |
| AWS_CLI_PROFILE={aws-cli-profile} | |
| AWS_CLI_REGION={aws-cli-region} |
This file contains hidden or 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
| # import config | |
| include config.env | |
| export $(shell sed 's/=.*//' config.env) | |
| # import deploy config | |
| include deploy.env | |
| export $(shell sed 's/=.*//' deploy.env) | |
| # grep the version from the mix file | |
| VERSION=$(shell ./version.sh) | |
| # DOCKER TASKS | |
| # Build the container | |
| build: | |
| docker build -t $(APP_NAME) . | |
| # Build the container without cache | |
| build-nc: | |
| docker build --no-cache -t $(APP_NAME) . | |
| # Run container on port configured in `config.env` | |
| run: | |
| docker run --env-file=./config.env -p=$(PORT):$(PORT) --name="$(APP_NAME)" $(APP_NAME) | |
| # Build and run the container | |
| up: build run | |
| stop: | |
| docker stop $(APP_NAME); docker rm $(APP_NAME) | |
| # Docker release - build, tag and push the container | |
| release: build-nc publish | |
| # Docker publish | |
| publish: repo-login publish-latest publish-version | |
| publish-latest: tag-latest | |
| @echo 'publish latest to $(DOCKER_REPO)' | |
| docker push $(DOCKER_REPO):latest | |
| publish-version: tag-version | |
| @echo 'publish $(VERSION) to $(DOCKER_REPO)' | |
| docker push $(DOCKER_REPO):$(VERSION) | |
| # Docker tagging | |
| tag: tag-latest tag-version | |
| tag-latest: | |
| @echo 'create tag latest' | |
| docker tag $(APP_NAME) $(DOCKER_REPO)/$(APP_NAME):latest | |
| tag-version: | |
| @echo 'create tag $(VERSION)' | |
| docker tag $(APP_NAME) $(DOCKER_REPO)/$(APP_NAME):$(VERSION) | |
| # HELPERS | |
| # generate script to login to aws docker repo | |
| CMD_REPOLOGIN := "eval $$\( aws ecr" | |
| ifdef AWS_CLI_PROFILE | |
| CMD_REPOLOGIN += " --profile $(AWS_CLI_PROFILE)" | |
| endif | |
| ifdef AWS_CLI_REGION | |
| CMD_REPOLOGIN += " --region $(AWS_CLI_REGION)" | |
| endif | |
| CMD_REPOLOGIN += " get-login \)" | |
| # login to AWS-ECR | |
| repo-login: | |
| @eval $(CMD_REPOLOGIN) | |
| # output to version | |
| version: | |
| @echo $(VERSION) | |
This file contains hidden or 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
| # Build the container | |
| make build | |
| # Build and publish the container | |
| make release | |
| # Publish a container to AWS-ECR. | |
| # This includes the login to the repo | |
| make publish | |
| # Run the container | |
| make run | |
| # Build an run the container | |
| make up | |
| # Stop the running container (needs to executed in a seperate shell) | |
| make stop |
This file contains hidden or 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
| # grep the version from a mix file | |
| cat mix.exs | grep version | grep '\([0-9]\+\.\?\)\{3\}' -o | |
| # grep the version from a package.json file with jq | |
| jq -rM '.version' package.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment