Skip to content

Commit

Permalink
standardize short git sha character count
Browse files Browse the repository at this point in the history
  • Loading branch information
mxfactorial committed Apr 24, 2024
1 parent 645ab4d commit e8152f1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 54 deletions.
29 changes: 19 additions & 10 deletions scripts/build-image.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
#!/bin/bash

if [[ "$#" -ne 4 ]]; then
cat <<- 'EOF'
use:
bash scripts/build-image.sh --app-name rule --build-ctx .
cat <<-'EOF'
use:
bash scripts/build-image.sh --app-name rule --build-ctx .
EOF
exit 1
fi

while [[ "$#" -gt 0 ]]; do
case $1 in
--app-name) APP_NAME="$2"; shift ;;
--build-ctx) BUILD_CTX="$2"; shift ;;
*) echo "unknown parameter passed: $1"; exit 1 ;;
esac
case $1 in
--app-name)
APP_NAME="$2"
shift
;;
--build-ctx)
BUILD_CTX="$2"
shift
;;
*)
echo "unknown parameter passed: $1"
exit 1
;;
esac
shift
done

HASH=$(git rev-parse --short HEAD)
HASH=$(git rev-parse --short=7 HEAD)
IMAGE_TAG="$APP_NAME:$HASH"
DOCKERFILE_PATH=./docker/$APP_NAME.Dockerfile

Expand All @@ -27,4 +36,4 @@ if [[ $COUNT -gt 0 ]]; then
docker tag $APP_NAME:latest $IMAGE_TAG
else
docker build -f $DOCKERFILE_PATH -t $IMAGE_TAG "$BUILD_CTX"
fi
fi
39 changes: 27 additions & 12 deletions scripts/print-image-tag.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
#/bin/bash

if [[ "$#" -ne 6 ]] && [[ "$#" -ne 8 ]]; then
cat <<- 'EOF'
use:
bash scripts/print-image-tag.sh --app-name rule --env dev --env-id 12345 # OPTIONAL: --hash 12345678
cat <<-'EOF'
use:
bash scripts/print-image-tag.sh --app-name rule --env dev --env-id 12345 # OPTIONAL: --hash 12345678
EOF
exit 1
fi

while [[ "$#" -gt 0 ]]; do
case $1 in
--app-name) APP_NAME="$2"; shift ;;
--env) ENV="$2"; shift ;;
--env-id) ENV_ID="$2"; shift ;;
--hash) HASH="$2"; shift ;;
*) echo "unknown parameter passed: $1"; exit 1 ;;
esac
case $1 in
--app-name)
APP_NAME="$2"
shift
;;
--env)
ENV="$2"
shift
;;
--env-id)
ENV_ID="$2"
shift
;;
--hash)
HASH="$2"
shift
;;
*)
echo "unknown parameter passed: $1"
exit 1
;;
esac
shift
done

if [[ -z $HASH ]]; then
HASH=$(git rev-parse --short HEAD)
HASH=$(git rev-parse --short=7 HEAD)
fi

REPO=$(source scripts/print-ecr-repo-uri.sh --app-name $APP_NAME --env $ENV --env-id $ENV_ID)
echo "$REPO:$HASH"
echo "$REPO:$HASH"
34 changes: 23 additions & 11 deletions scripts/push-prod-image.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#!/bin/bash

if [[ "$#" -ne 6 ]]; then
cat <<- 'EOF'
use:
bash scripts/push-prod-image.sh --app-name rule --env dev --env-id 12345
cat <<-'EOF'
use:
bash scripts/push-prod-image.sh --app-name rule --env dev --env-id 12345
EOF
exit 1
fi

while [[ "$#" -gt 0 ]]; do
case $1 in
--app-name) APP_NAME="$2"; shift ;;
--env) ENV="$2"; shift ;;
--env-id) ENV_ID="$2"; shift ;;
*) echo "unknown parameter passed: $1"; exit 1 ;;
esac
case $1 in
--app-name)
APP_NAME="$2"
shift
;;
--env)
ENV="$2"
shift
;;
--env-id)
ENV_ID="$2"
shift
;;
*)
echo "unknown parameter passed: $1"
exit 1
;;
esac
shift
done

Expand All @@ -25,7 +37,7 @@ REGION=$(yq '.infrastructure.terraform.aws.modules.environment.env_var.set.REGIO
REPO_NAME="$ID_ENV_PREFIX/$APP_NAME"
PROD_ENV_ID=$(yq '.infrastructure.terraform.env-id.prod.env_var.set.PROD_ENV_ID.default' $PROJECT_CONF)

MERGE_COMMIT_HASH=$(git rev-parse --short HEAD)
MERGE_COMMIT_HASH=$(git rev-parse --short=7 HEAD)

TAG_COUNT=$(aws ecr batch-get-image \
--repository-name=$REPO_NAME \
Expand All @@ -50,4 +62,4 @@ if [[ $TAG_COUNT -gt 0 ]]; then

# push prod image
source scripts/push-ecr-image.sh --curr-tag $PROD_TAG
fi
fi
20 changes: 13 additions & 7 deletions scripts/tag-dev-image.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#!/bin/bash

if [[ "$#" -ne 2 ]]; then
cat <<- 'EOF'
use:
bash scripts/tag-dev-image.sh --app-name go-migrate
cat <<-'EOF'
use:
bash scripts/tag-dev-image.sh --app-name go-migrate
EOF
exit 1
fi

while [[ "$#" -gt 0 ]]; do
case $1 in
--app-name) APP_NAME="$2"; shift ;;
*) echo "unknown parameter passed: $1"; exit 1 ;;
--app-name)
APP_NAME="$2"
shift
;;
*)
echo "unknown parameter passed: $1"
exit 1
;;
esac
shift
done

HASH=$(git rev-parse --short HEAD)
HASH=$(git rev-parse --short=7 HEAD)
LOCAL_IMAGE_TAG="$APP_NAME:$HASH"

ENV=dev
Expand All @@ -28,4 +34,4 @@ DEV_IMAGE_TAG="$DEV_REPO:$HASH"

docker tag $LOCAL_IMAGE_TAG $DEV_IMAGE_TAG

echo "tagged \"$LOCAL_IMAGE_TAG\" as \"$DEV_IMAGE_TAG\""
echo "tagged \"$LOCAL_IMAGE_TAG\" as \"$DEV_IMAGE_TAG\""
45 changes: 31 additions & 14 deletions scripts/tag-merge-commit.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
#!/bin/bash

if [[ "$#" -ne 6 ]]; then
cat <<- 'EOF'
use:
bash scripts/tag-merge-commit.sh --app-name go-migrate --env dev --env-id 12345
cat <<-'EOF'
use:
bash scripts/tag-merge-commit.sh --app-name go-migrate --env dev --env-id 12345
EOF
exit 1
fi

while [[ "$#" -gt 0 ]]; do
case $1 in
--app-name) APP_NAME="$2"; shift ;;
--env) ENV="$2"; shift ;;
--env-id) ENV_ID="$2"; shift ;;
*) echo "unknown parameter passed: $1"; exit 1 ;;
esac
case $1 in
--app-name)
APP_NAME="$2"
shift
;;
--env)
ENV="$2"
shift
;;
--env-id)
ENV_ID="$2"
shift
;;
*)
echo "unknown parameter passed: $1"
exit 1
;;
esac
shift
done

Expand All @@ -29,26 +41,31 @@ LAMBDA_NAME="$APP_NAME-$ID_ENV"
DEPLOYED_IMAGE=$(aws lambda get-function --function-name $LAMBDA_NAME --region $REGION --query 'Code.ImageUri' --output text)

# test for image manifest sha tag
if [[ $(tr -dc '@' <<< "$DEPLOYED_IMAGE" | wc -c | xargs) -gt 0 ]]; then
IFS='@' read -ra DEPLOYED_IMAGE <<< "$DEPLOYED_IMAGE"
if [[ $(tr -dc '@' <<<"$DEPLOYED_IMAGE" | wc -c | xargs) -gt 0 ]]; then
IFS='@' read -ra DEPLOYED_IMAGE <<<"$DEPLOYED_IMAGE"
REPO_NAME=${DEPLOYED_IMAGE[0]}
DEPLOYED_IMAGE_TAG_VERSION=${DEPLOYED_IMAGE[1]}
else # assume git sha image tag
IFS=':' read -ra DEPLOYED_IMAGE <<< "$DEPLOYED_IMAGE"
IFS=':' read -ra DEPLOYED_IMAGE <<<"$DEPLOYED_IMAGE"
REPO_NAME=${DEPLOYED_IMAGE[0]}
DEPLOYED_IMAGE_TAG_VERSION=${DEPLOYED_IMAGE[1]}
fi

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

# get the commit hash after merge
MERGE_COMMIT_HASH=$(git rev-parse --short HEAD)
MERGE_COMMIT_HASH=$(git rev-parse --short=7 HEAD)

for TAG_VERSION in "${LATEST_ECR_IMAGE_TAG_VERSIONS[@]}"; do
if [[ "$TAG_VERSION" == "$DEPLOYED_IMAGE_TAG_VERSION" ]]; then
echo "*** $LAMBDA_NAME has latest image tag $DEPLOYED_IMAGE_TAG_VERSION deployed. skipping $MERGE_COMMIT_HASH retag"
exit 0
fi

if [[ "$TAG_VERSION" == "$MERGE_COMMIT_HASH" ]]; then
echo "*** $IMAGE_NAME has latest image tag $MERGE_COMMIT_HASH. skipping $MERGE_COMMIT_HASH retag"
exit 0
fi
done

# pick first image tag version
Expand All @@ -65,4 +82,4 @@ MANIFEST=$(aws ecr batch-get-image \
# retag image with merge commit hash
aws ecr put-image --repository-name $IMAGE_NAME --image-tag $MERGE_COMMIT_HASH --region $REGION --image-manifest "$MANIFEST" 1>/dev/null

echo "*** $REPO_NAME image retagged with $MERGE_COMMIT_HASH merge commit hash"
echo "*** $REPO_NAME image retagged with $MERGE_COMMIT_HASH merge commit hash"

0 comments on commit e8152f1

Please sign in to comment.