Skip to content

Instantly share code, notes, and snippets.

@faramozzayw
Last active September 20, 2021 15:01
Show Gist options
  • Save faramozzayw/b0aa1b5f2614978af3f0fa34fe70b352 to your computer and use it in GitHub Desktop.
Save faramozzayw/b0aa1b5f2614978af3f0fa34fe70b352 to your computer and use it in GitHub Desktop.
deploy
#!/bin/bash
# импортируем утилиты
. ./utils.sh
while getopts ":t:L" opt; do
case $opt in
t) tag="$OPTARG"
;;
L) latest="1"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
docker-compose build &&
if [[ ! -z "$latest" ]]; then
docker push uportaller/prozorro-sale-front
fi
if [[ ! -z "$tag" ]]; then
docker tag uportaller/prozorro-sale-front uportaller/prozorro-sale-front:$tag
docker push uportaller/prozorro-sale-front:$tag
else
docker tag uportaller/prozorro-sale-front uportaller/prozorro-sale-front:$(parse_git_branch)
docker push uportaller/prozorro-sale-front:$(parse_git_branch)
fi
#!/bin/bash
# импортируем утилиты
. ./utils.sh
while getopts ":t:e:L" opt; do
case $opt in
t) tag="$OPTARG"
;;
e) env="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
if [[ ! -z "$tag" && ! -z "$env" ]]; then
$(pull_image tag)
export FRONT_IMAGE_TAG=$tag
docker-compose -f docker-compose.yml -f docker-compose.$env.yml up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front:$tag=3
elif [[ ! -z "$env" ]]; then
tag_is_undefined
docker-compose -f docker-compose.yml -f docker-compose.$env.yml up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front=3
elif [[ ! -z "$tag" ]]; then
env_is_undefined
# https://stackoverflow.com/questions/32113330/check-if-imagetag-combination-already-exists-on-docker-hub/37799618
if [[ "$(check_image_exist $tag)" == "success" ]]; then
$(pull_image $tag)
export FRONT_IMAGE_TAG=$tag
docker-compose up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front:$tag=3
fi
else
env_is_undefined
tag_is_undefined
docker-compose up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front=3
fi
.PHONY: deploy,build,try_set
.SILENT: deploy,build,try_set
get_nginx:
docker pull nginx:latest
build:
docker-compose build
ifdef tag
docker tag uportaller/prozorro-sale-front uportaller/prozorro-sale-front:$(tag)
docker push uportaller/prozorro-sale-front:$(tag)
else
docker tag uportaller/prozorro-sale-front uportaller/prozorro-sale-front:$(shell $(call parse_git_branch))
docker push uportaller/prozorro-sale-front:$(shell $(call parse_git_branch))
endif
ifdef push_with_latest
docker push uportaller/prozorro-sale-front
endif
deploy:
ifneq ($(and $(env),$(tag)),)
$(call is_compose_exist,$(env))
$(call pull_image,$(tag))
docker-compose -f docker-compose.yml -f docker-compose.$(env).yml up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front:$(tag)=3
else ifdef env
$(call tag_is_undefined)
$(call is_compose_exist,$(env))
docker-compose -f docker-compose.yml -f docker-compose.$(env).yml up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front=3
else ifdef tag
$(call env_is_undefined)
$(call pull_image,$(tag))
docker-compose up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front:$(tag)=3
else
$(call env_is_undefined)
$(call tag_is_undefined)
docker-compose up --no-build --force-recreate --detach --scale uportaller/prozorro-sale-front=3
endif
define parse_git_branch =
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
endef
define tag_is_undefined =
echo "'tag' is undefined => 'latest' is used by default"
endef
define env_is_undefined
echo "'env' is undefined => 'docker-compose.yml' is used"
endef
define is_compose_exist =
test -s ./docker-compose.$1.yml || { echo "'docker-compose.$1.yml' does not exist!"; exit 1; }
endef
define pull_image =
docker pull uportaller/prozorro-sale-front:$1
endef
#!/bin/bash
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
# extract app version from package.json
function parse_app_version() {
grep -oP '"version": "\K.*(?=")' package.json
}
function get_tag() {
local branch=$(parse_git_branch)
if [ ! -z "$1" ]; then
echo "$1"
elif ! [ "$branch" == "prod" ]; then
echo "$branch-$(parse_app_version)"
else
echo $(parse_app_version)
fi
}
function get_docker_compose_overrides () {
if [[ "$1" = "prod" ]]; then
echo "-f docker-compose.yml -f docker-compose.prod.yml"
elif [[ "$1" = "test" ]]; then
echo "-f docker-compose.yml -f docker-compose.test.yml"
else
echo ""
fi
}
function tag_is_undefined () {
# https://stackoverflow.com/questions/31656645/how-do-i-echo-directly-on-standard-output-inside-a-shell-function
echo "'tag' is undefined => 'latest' is used by default"
return;
}
function env_is_undefined () {
# https://stackoverflow.com/questions/31656645/how-do-i-echo-directly-on-standard-output-inside-a-shell-function
echo "'env' is undefined => 'docker-compose.yml' is used"
return;
}
function pull_image() {
# procedurally generated command
local command="docker pull uportaller/prozorro-sale-front:$1"
# execute command
echo $command
}
function check_image_exist () {
docker manifest inspect uportaller/prozorro-sale-front:$1 > /dev/null && echo "success" || echo "failed"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment