Skip to content

Instantly share code, notes, and snippets.

@Turgon37
Last active January 29, 2022 14:07
Show Gist options
  • Save Turgon37/2ba8685893807e3637ea3879ef9d2062 to your computer and use it in GitHub Desktop.
Save Turgon37/2ba8685893807e3637ea3879ef9d2062 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Docker image tools shim.
#
# License: MIT
# Exit on any individual command failure.
set -e
##
## ENVIRONMENT SETUP
##
# shellcheck disable=SC2155
export GIT_PATH=$(command -v git)
# shellcheck disable=SC2155
export DOCKER_PATH=$(command -v docker)
# shellcheck disable=SC2155
export JQ_PATH=$(command -v jq)
# Versionned project
if [[ -f "VERSION" ]]; then
# shellcheck disable=SC2155
export PROJECT_VERSION=$(cat VERSION)
echo "-> project version ${PROJECT_VERSION}"
fi
# Travis environment
if [[ "x$TRAVIS" == "xtrue" ]]; then
echo '=> TRAVIS support'
# shellcheck disable=SC2155
export VCS_REF=$([[ "$TRAVIS_PULL_REQUEST" == "false" ]] && echo "$TRAVIS_COMMIT" || echo "$TRAVIS_PULL_REQUEST_SHA")
# shellcheck disable=SC2155
export VCS_BRANCH=$([[ "$TRAVIS_PULL_REQUEST" == "false" ]] && echo "$TRAVIS_BRANCH" || echo "$TRAVIS_PULL_REQUEST_BRANCH")
if [[ ${TRAVIS_REPO_SLUG} =~ ([^/]+)/([^/]+) ]]; then
export VCS_OWNER="${BASH_REMATCH[1]}"
export VCS_NAME="${BASH_REMATCH[2]}"
fi
fi
# Git project
if [[ -n "$GIT_PATH" && -d .git/ ]]; then
echo '=> GIT support'
# shellcheck disable=SC2155
[[ -z "$VCS_REF" ]] && export VCS_REF=$(git rev-parse --short HEAD)
echo "-> current vcs reference '${VCS_REF}'"
# shellcheck disable=SC2155
[[ -z "$VCS_BRANCH" ]] && export VCS_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
echo "-> current vcs branch '${VCS_BRANCH}'"
# shellcheck disable=SC2155
[[ -z "$VCS_NAME" ]] && export VCS_NAME=$(basename "${PWD}")
echo "-> current vcs name '${VCS_NAME}'"
# shellcheck disable=SC2155
export VCS_MASTER_BRANCH=${MASTER_BRANCH:-master}
echo "-> default vcs master branch '${VCS_MASTER_BRANCH}'"
fi
# Docker project
if [[ -n "$DOCKER_PATH" ]]; then
echo '=> DOCKER support'
# docker hub username
export DOCKERHUB_REGISTRY_USERNAME=turgon37
export DOCKER_USERNAME="${DOCKERHUB_REGISTRY_USERNAME}"
echo "-> docker username ${DOCKER_USERNAME}"
# image name => remove docker- prefix
export DOCKER_IMAGE_NAME=${VCS_NAME#docker-}
# full image path
export DOCKER_IMAGE="${DOCKER_USERNAME}/${DOCKER_IMAGE_NAME}"
echo "-> docker image ${DOCKER_IMAGE}"
# if we are not on master branch, append branch as tags prefix
if [[ "${VCS_BRANCH}" != "${VCS_MASTER_BRANCH}" ]]; then
export DOCKER_IMAGE_TAG_BRANCH_PREFIX="${VCS_BRANCH}-"
echo "-> docker branch tag prefix $DOCKER_IMAGE_TAG_BRANCH_PREFIX"
fi
# the name of the temporary file in which you can put produced
# image tags
export DOCKER_PRODUCED_TAGS_FILE=.docker_build_tags
if [[ -f "$DOCKER_PRODUCED_TAGS_FILE" ]]; then
rm -f "$DOCKER_PRODUCED_TAGS_FILE"
fi
fi
# jq tool
if [[ -n "$JQ_PATH" ]]; then
echo '=> JQ support'
fi
##
## FUNCTIONS
##
export docker_wait_for_logs_default_attempt=5
# Wait for a container to be up by checking regularly its logs for a pattern
# param1 : the container name
# param2 : the string to search for in logs
function wait_for_string_in_container_logs() {
[[ -n "${1}" && -n "${2}" ]]
attempt=0
while [[ $attempt -le $docker_wait_for_logs_default_attempt ]]; do
attempt=$((attempt + 1))
echo "-> Waiting for container '${1}' to be up (attempt: ${attempt})..."
if docker logs "${1}" | grep --quiet "${2}"; then
echo "-> Container ${1} is up"
return 0
fi
sleep 4
done
echo "-> Container ${1} is unable to be up until ${docker_wait_for_logs_default_attempt} attempts"
return 1
}
# Stop and remove properly a container
# param1 : the container name
function stop_and_remove_container() {
[[ -n "${1}" ]]
docker stop "${1}" > /dev/null
docker rm --volumes "${1}" > /dev/null
echo "-> Container ${1} is stopped and removed"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment