Skip to content

Commit

Permalink
fix(docker.sh): fix function returns
Browse files Browse the repository at this point in the history
  • Loading branch information
chohner committed Jan 11, 2024
1 parent a9d6be1 commit 0e4f471
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions docker.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail

HELP_TEXT="USAGE: --contentFromImage, --contentHashFromImage, --build [app/content], --push [app/content]"
HELP_TEXT="USAGE: --contentHash, --contentHashFromImage, --contentFromImage, --build [app/content], --push [app/content]"
REGISTRY=ghcr.io
IMAGE_NAME=digitalservicebund/a2j-rechtsantragstelle
CONTENT_FILENAME=content_from_image.json
DOCKERFILE=Dockerfile
APP_IMAGE=$REGISTRY/$IMAGE_NAME-app
CONTENT_IMAGE=$REGISTRY/$IMAGE_NAME-content
PROD_IMAGE=$REGISTRY/$IMAGE_NAME
DOCKERFILE=Dockerfile

function parseValidTarget() {
if [ "$#" -le 1 ]; then
Expand All @@ -27,29 +26,35 @@ function parseValidTarget() {
}

# The content file lives inside $CONTENT_IMAGE. To get its sha256 hash:
# Create tmp container, copy content.json out, store hash, delete tmp file & container
# Create tmp container, copy content.json to provided location, delete container
function getContentFromLatestImage() {
tmp_container_id=$(docker create $CONTENT_IMAGE null)
docker cp --quiet $tmp_container_id:/content.json ./$CONTENT_FILENAME && docker rm $tmp_container_id &>/dev/null
local tmp_container_id=$(docker create $CONTENT_IMAGE null)
docker cp --quiet $tmp_container_id:/content.json $1 && docker rm $tmp_container_id &>/dev/null
}
function getContentHashFromLatestImage() {
getContentFromLatestImage
CONTENT_HASH=$(sha256sum $CONTENT_FILENAME | cut -d' ' -f1)
rm $CONTENT_FILENAME
function hashFromContentFile() {
echo $(sha256sum $1 | cut -d' ' -f1)
}
function printContentHashFromLatestImage() {
getContentHashFromLatestImage
echo $CONTENT_HASH
function getContentHashFromLatestImage() {
local tmp_content_file="./content.tmp"
getContentFromLatestImage $tmp_content_file
local content_hash=$(hashFromContentFile $tmp_content_file)
rm $tmp_content_file
echo $content_hash
}

case $1 in
--contentFromImage)
echo "Extracting content from $CONTENT_IMAGE into ./$CONTENT_FILENAME..."
getContentFromLatestImage
IMAGE_CONTENT_FILE=./content_from_image.json
echo "Extracting content from $CONTENT_IMAGE into $IMAGE_CONTENT_FILE..."
getContentFromLatestImage $IMAGE_CONTENT_FILE
exit 0
;;
--contentHashFromImage)
printContentHashFromLatestImage
getContentHashFromLatestImage
exit 0
;;
--contentHash)
hashFromContentFile content.json
exit 0
;;
--build)
Expand All @@ -73,21 +78,20 @@ case $1 in
;;
--push)
parseValidTarget "$@"

LATEST_GIT_TAG=$(git rev-parse HEAD)
APP_IMAGE_TAG=$APP_IMAGE:$LATEST_GIT_TAG
CONTENT_IMAGE_TAG=$CONTENT_IMAGE:$CONTENT_HASH

case ${TARGET} in
app)
CONTENT_HASH=$(getContentHashFromLatestImage)
APP_IMAGE_TAG=$APP_IMAGE:$LATEST_GIT_TAG
echo "Tagging and pushing $APP_IMAGE_TAG"
docker tag $APP_IMAGE $APP_IMAGE:$LATEST_GIT_TAG
docker tag $APP_IMAGE $APP_IMAGE_TAG
docker push --all-tags $APP_IMAGE
getContentHashFromLatestImage # populates $CONTENT_HASH
;;
content)
CONTENT_HASH=$(hashFromContentFile content.json)
CONTENT_IMAGE_TAG=$CONTENT_IMAGE:$CONTENT_HASH
echo "Tagging and pushing $CONTENT_IMAGE_TAG"
CONTENT_HASH=$(sha256sum content.json | cut -d' ' -f1)
docker tag $CONTENT_IMAGE $CONTENT_IMAGE:$CONTENT_HASH
docker push --all-tags $CONTENT_IMAGE
;;
Expand Down

0 comments on commit 0e4f471

Please sign in to comment.