Skip to content

Instantly share code, notes, and snippets.

@neilkuan
Created March 25, 2021 01:01
Show Gist options
  • Select an option

  • Save neilkuan/20be75e484ced6be0f3e464bfa266740 to your computer and use it in GitHub Desktop.

Select an option

Save neilkuan/20be75e484ced6be0f3e464bfa266740 to your computer and use it in GitHub Desktop.
#!/bin/bash
##Color
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
## ENV
DEFAULT_REGION=us-west-2
ACCOUNT_ID=123456789012
AWS_REGION=${2-$DEFAULT_REGION}
echo -en "${GREEN}Give me new image tag: ${NC} " ; read -p "" confirm && [ -z $confirm ] && echo -e "${RED}Please input new image Tag${NC}" && exit 1
TAG=$confirm
function composefile() {
cat <<EOM
version: "3.1"
services:
web:
container_name: web
image: "image-id:${TAG}"
ports:
- "8080:8080"
volumes:
- /home/ec2-user/web:/config
environment:
spring.config.location: "/config/"
logging:
driver: "awslogs"
options:
awslogs-region: us-west-2
awslogs-group: demoLogGroup
tag: '{{ with split .ContainerName ":" }}{{join . "_"}}{{end}}-{{.ID}}'
EOM
}
function checkimagetag () {
aws ecr describe-images \
--repository-name web \
--image-ids imageTag="RC.${TAG}" >/dev/null 2>&1
[ $? != 0 ] && echo -e "${RED}Please check image tag or describe ecr image permission!!! ${NC}" && exit 1
}
checkimagetag
composefile > docker-compose.yml
echo "check config file"
docker-compose config
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
docker-compose pull
docker-compose stop
docker-compose rm -f
docker-compose up -d && echo -e "${GREEN}Restart OK !!!${NC}" || echo -e "${RED}Restart Fail!!!${NC}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment