diff --git a/scripts/build-image.sh b/scripts/build-image.sh index aa92a5cd..72bcee26 100644 --- a/scripts/build-image.sh +++ b/scripts/build-image.sh @@ -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 @@ -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 \ No newline at end of file +fi diff --git a/scripts/print-image-tag.sh b/scripts/print-image-tag.sh index 36f03b76..2ac7760f 100644 --- a/scripts/print-image-tag.sh +++ b/scripts/print-image-tag.sh @@ -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" \ No newline at end of file +echo "$REPO:$HASH" diff --git a/scripts/push-prod-image.sh b/scripts/push-prod-image.sh index 01e66cb5..0c5ec7dc 100644 --- a/scripts/push-prod-image.sh +++ b/scripts/push-prod-image.sh @@ -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 @@ -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 \ @@ -50,4 +62,4 @@ if [[ $TAG_COUNT -gt 0 ]]; then # push prod image source scripts/push-ecr-image.sh --curr-tag $PROD_TAG -fi \ No newline at end of file +fi diff --git a/scripts/tag-dev-image.sh b/scripts/tag-dev-image.sh index 5867186b..c4ce0fef 100644 --- a/scripts/tag-dev-image.sh +++ b/scripts/tag-dev-image.sh @@ -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 @@ -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\"" \ No newline at end of file +echo "tagged \"$LOCAL_IMAGE_TAG\" as \"$DEV_IMAGE_TAG\"" diff --git a/scripts/tag-merge-commit.sh b/scripts/tag-merge-commit.sh index acbe79aa..f5e272a8 100644 --- a/scripts/tag-merge-commit.sh +++ b/scripts/tag-merge-commit.sh @@ -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 @@ -29,12 +41,12 @@ 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 @@ -42,13 +54,18 @@ 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 @@ -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" \ No newline at end of file +echo "*** $REPO_NAME image retagged with $MERGE_COMMIT_HASH merge commit hash"