Skip to content

Instantly share code, notes, and snippets.

@zeroc0d3
Last active February 13, 2024 13:41
Show Gist options
  • Save zeroc0d3/674d391a7077dc144c075c21a02f613f to your computer and use it in GitHub Desktop.
Save zeroc0d3/674d391a7077dc144c075c21a02f613f to your computer and use it in GitHub Desktop.
Build CI/CD Container Image for ECR (Elastic Container Registry)
#!/usr/bin/env sh
# -----------------------------------------------------------------------------
# Docker Build Container (Elastic Container Registry - ECR)
# -----------------------------------------------------------------------------
# Author : Dwi Fahni Denni
# License : Apache v2
# -----------------------------------------------------------------------------
set -e
export AWS_ACCOUNT_ID=$1
export CI_REGISTRY="${AWS_ACCOUNT_ID}.dkr.ecr.ap-southeast-1.amazonaws.com"
export CI_ECR_PATH=$3
export IMAGE="$CI_REGISTRY/$CI_ECR_PATH"
docker_build() {
export FILE=$2
export BASE_IMAGE=$4
export TAGS_ID=$5
export CUSTOM_TAGS=$6
if [ "$CUSTOM_TAGS" = "" ]; then
echo "Build Image => $IMAGE:${BASE_IMAGE}"
echo ">> docker build -t $IMAGE:${BASE_IMAGE} -f $FILE ."
docker build -t $IMAGE:${BASE_IMAGE} -f $FILE .
echo '---'
echo "Build Image => $IMAGE:${TAGS_ID}"
echo ">> docker build -t $IMAGE:${TAGS_ID} -f $FILE ."
docker build -t $IMAGE:${TAGS_ID} -f $FILE .
echo '---'
echo "Build Image => $IMAGE:${BASE_IMAGE}-${TAGS_ID}"
echo ">> docker build -t $IMAGE:${BASE_IMAGE}-${TAGS_ID} -f $FILE ."
docker build -t $IMAGE:${BASE_IMAGE}-${TAGS_ID} -f $FILE .
echo '---'
else
echo "Build Image => $IMAGE:${BASE_IMAGE}"
echo ">> docker build -t $IMAGE:${BASE_IMAGE} -f $FILE ."
docker build -t $IMAGE:${BASE_IMAGE} -f $FILE .
echo '---'
echo "Build Image => $IMAGE:${TAGS_ID}"
echo "docker build -t $IMAGE:${TAGS_ID} -f $FILE ."
docker build -t $IMAGE:${TAGS_ID} -f $FILE .
echo '---'
echo "Build Image => $IMAGE:${BASE_IMAGE}-${TAGS_ID}"
echo ">> docker build -t $IMAGE:${BASE_IMAGE}-${TAGS_ID} -f $FILE ."
docker build -t $IMAGE:${BASE_IMAGE}-${TAGS_ID} -f $FILE .
echo '---'
echo "Build Image => $IMAGE:${TAGS_ID}-${CUSTOM_TAGS}"
docker build -t $IMAGE:${TAGS_ID}-${CUSTOM_TAGS} -f $FILE .
echo ">> docker build -t $IMAGE:${TAGS_ID}-${CUSTOM_TAGS} -f $FILE ."
echo '---'
echo "Build Image => $IMAGE:${BASE_IMAGE}-${TAGS_ID}-${CUSTOM_TAGS}"
echo ">> docker build -t $IMAGE:${BASE_IMAGE}-${TAGS_ID}-${CUSTOM_TAGS} -f $FILE ."
docker build -t $IMAGE:${BASE_IMAGE}-${TAGS_ID}-${CUSTOM_TAGS} -f $FILE .
echo '---'
fi
}
main() {
# docker_build [AWS_ACCOUNT] Dockerfile devopscorner/cicd [alpine|ubuntu|codebuild] [version|latest|tags] [custom-tags]
docker_build $1 $2 $3 $4 $5 $6
echo ''
echo '-- ALL DONE --'
}
### START HERE ###
main $1 $2 $3 $4 $5 $6
### How to Execute ###
# ./ecr-build.sh [AWS_ACCOUNT] Dockerfile [ECR_PATH] [alpine|ubuntu|codebuild] [version|latest|tags] [custom-tags]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment