Skip to content

Commit

Permalink
use image builder workflow when deploying
Browse files Browse the repository at this point in the history
  • Loading branch information
mxfactorial committed Apr 20, 2024
1 parent a949a02 commit d735099
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
8 changes: 7 additions & 1 deletion scripts/build-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ HASH=$(git rev-parse --short HEAD)
IMAGE_TAG="$APP_NAME:$HASH"
DOCKERFILE_PATH=./docker/$APP_NAME.Dockerfile

docker build -f $DOCKERFILE_PATH -t $IMAGE_TAG "$BUILD_CTX"
COUNT=$(docker image ls | grep --color=never -E "^$APP_NAME.*latest" | wc -l | xargs)

if [[ $COUNT -gt 0 ]]; then
docker tag $APP_NAME:latest $IMAGE_TAG
else
docker build -f $DOCKERFILE_PATH -t $IMAGE_TAG "$BUILD_CTX"
fi
75 changes: 46 additions & 29 deletions scripts/deploy-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,77 @@

set -e

# store list in memory in case it
# requires filtering to deploy services only
INVENTORY_LIST=$(cat inventory)

if [[ "$#" -lt 2 ]] || [[ "$#" -gt 4 ]]; then
cat <<- 'EOF'
use:
bash scripts/deploy-all.sh --env dev
OPTIONAL ARGS:
"--initial", prepares for terraform apply
"--services-only", deploys services only
"--transaction-services-only", deploys transaction services only
cat <<-'EOF'
use:
bash scripts/deploy-all.sh --env dev
OPTIONAL ARGS:
"--initial", prepares for terraform apply
"--services", deploys services only
"--transaction-services", deploys transaction services only
EOF
exit 1
fi

INVENTORY=($(bash scripts/list-dir-paths.sh --type app))

while [[ "$#" -gt 0 ]]; do
case $1 in
--env) ENV="$2"; shift ;;
--initial) INITIAL='initial-' ;;
--services-only) INVENTORY_LIST=$(cat inventory | grep services/) ;;
case $1 in
--env)
ENV="$2"
shift
;;
--initial) INITIAL=1 ;;
--services) INVENTORY=($(bash scripts/list-dir-paths.sh --type app |
grep --color=never services/)) ;;
# for convenience, not currently referenced in makefiles
--transaction-services-only) INVENTORY_LIST=$(cat inventory | grep --color=never -e transaction -e request -e rule -e graphql) ;;
*) echo "unknown parameter passed: $1"; exit 1 ;;
esac
--transaction-services) INVENTORY=($(bash scripts/list-dir-paths.sh --type app |
grep --color=never \
-e transact \
-e request \
-e rule \
-e graphql)) ;;
*)
echo "unknown parameter passed: $1"
exit 1
;;
esac
shift
done

# options are "deploy", or "initial-deploy" to prep for terraform
MAKE_CMD="${INITIAL}"deploy
MAKE_CMD=deploy

# wc -l returns 0 for single line file with 0 newline characters
INVENTORY_SIZE=$(echo "$INVENTORY_LIST" | wc -l | awk '{print $1}')
if [[ $INVENTORY_SIZE -eq 0 ]]; then
INVENTORY_SIZE=1
# options are "deploy", or "initial-deploy" to prep for terraform
if [[ -n $INITIAL ]]; then
MAKE_CMD="initial-deploy"
fi

INVENTORY_SIZE="${#INVENTORY[@]}"

# store script exec start time
SCRIPT_START_TIME=$(date +%s)

echo "*** starting $MAKE_CMD of $INVENTORY_SIZE apps at $(date)"

if [[ -n $GITHUB_PAT ]]; then
source scripts/build-all-images.sh
fi

# loop through app directories and deploy or initial-deploy
for app_dir in $INVENTORY_LIST; do
for app_dir in "${INVENTORY[@]}"; do
DEPLOY_START_TIME=$(date +%s)
DEPLOY_START_LAPSE=$(($DEPLOY_START_TIME - $SCRIPT_START_TIME))
echo "*** starting $MAKE_CMD of $app_dir after $(printf '%02dh:%02dm:%02ds\n' $(($DEPLOY_START_LAPSE/3600)) $(($DEPLOY_START_LAPSE%3600/60)) $(($DEPLOY_START_LAPSE%60)))"
(cd $app_dir; make --no-print-directory $MAKE_CMD ENV=$ENV)
echo "*** starting $MAKE_CMD of $app_dir after $(printf '%02dh:%02dm:%02ds\n' $(($DEPLOY_START_LAPSE / 3600)) $(($DEPLOY_START_LAPSE % 3600 / 60)) $(($DEPLOY_START_LAPSE % 60)))"
(
cd $app_dir
make --no-print-directory $MAKE_CMD ENV=$ENV
)
done

# store script exec end time
SCRIPT_END_TIME=$(date +%s)
SCRIPT_DURATION=$(($SCRIPT_END_TIME - $SCRIPT_START_TIME))

# https://stackoverflow.com/a/39452629
echo "*** $MAKE_CMD time duration of $INVENTORY_SIZE apps: $(printf '%02dh:%02dm:%02ds\n' $(($SCRIPT_DURATION/3600)) $(($SCRIPT_DURATION%3600/60)) $(($SCRIPT_DURATION%60)))"
echo "*** $MAKE_CMD time duration of $INVENTORY_SIZE apps: $(printf '%02dh:%02dm:%02ds\n' $(($SCRIPT_DURATION / 3600)) $(($SCRIPT_DURATION % 3600 / 60)) $(($SCRIPT_DURATION % 60)))"
2 changes: 1 addition & 1 deletion scripts/deploy-dev-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ REPO_URI=$(aws ecr describe-repositories \
--output text \
--region $REGION)

LATEST_ECR_IMAGE_TAG_VERSIONS=($(aws ecr describe-images --repository-name $IMAGE_NAME --output text --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags' | xargs))
LATEST_ECR_IMAGE_TAG_VERSIONS=($(aws ecr describe-images --repository-name $REPO_NAME --output text --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags' | xargs))
LATEST_ECR_IMAGE_TAG_VERSION="${LATEST_ECR_IMAGE_TAG_VERSIONS[0]}"

IMAGE_TAG=$REPO_URI:$LATEST_ECR_IMAGE_TAG_VERSION
Expand Down
4 changes: 2 additions & 2 deletions services/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ all:
@$(MAKE) -s test-cmd-arg
@$(MAKE) -s test-cmd-val
ifeq (deploy,$(CMD))
@cd ..; bash scripts/deploy-all.sh --env $(ENV) --services-only
@cd ..; bash scripts/deploy-all.sh --env $(ENV) --services
endif
ifeq (initial-deploy,$(CMD))
@cd ..; bash scripts/deploy-all.sh --env $(ENV) --initial --services-only
@cd ..; bash scripts/deploy-all.sh --env $(ENV) --initial --services
endif

inventory:
Expand Down

0 comments on commit d735099

Please sign in to comment.