Skip to content
Snippets Groups Projects
Commit 4bf8fc95 authored by michael.minelli's avatar michael.minelli
Browse files

Add deploy to Docker Hub script

parent 4e1cde90
No related branches found
No related tags found
No related merge requests found
Pipeline #34763 passed
#!/bin/bash
# Define color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
DOCKERFILE=Dockerfile_AssignmentChecker
PROJECT_FOLDER=AssignmentChecker
test_if_command_unsuccessful() {
local status=$1
local title=$2
local text_if_failed=$3
if [ "$status" -ne 0 ]; then
echo -e "${title}: ${RED}${text_if_failed}: $status${NC}\n"
exit 1
fi
}
##########################################################
# Function to prompt for input if not provided
prompt_if_empty() {
local var_name=$1
local prompt_message=$2
local var_value=${!var_name}
if [ -z "$var_value" ]; then
read -p "$prompt_message: " var_value
export "$var_name"="$var_value"
fi
}
# Parse parameters
while [ "$1" != "" ]; do
case $1 in
--dockerhub-user ) shift
DOCKER_REGISTRY_USER=$1
;;
--dockerhub-password ) shift
DOCKER_REGISTRY_PASSWORD=$1
;;
--dockerhub-repo ) shift
DOCKER_REGISTRY_IMAGE=$1
;;
--backend-url ) shift
API_URL=$1
;;
* ) echo -e "${RED}Invalid parameter: $1${NC}"
exit 1
esac
shift
done
# Prompt for values if not provided
prompt_if_empty "DOCKER_REGISTRY_USER" "Docker Hub user"
prompt_if_empty "DOCKER_REGISTRY_PASSWORD" "Docker Hub password"
prompt_if_empty "DOCKER_REGISTRY_IMAGE" "Docker Hub repository"
prompt_if_empty "API_URL" "Backend url"
##########################################################
# Function to check if a command exists
check_dependency() {
local cmd=$1
if ! command -v $cmd &> /dev/null; then
echo -e "${RED}Error: $cmd is not installed.${NC}"
exit 1
fi
}
# Check for dependencies
echo -e "Dependencies: ${YELLOW}Checking...${NC}"
check_dependency jq
check_dependency docker
check_dependency sed
echo -e "Dependencies: ${GREEN}OK${NC}\n"
VERSION=$(jq -r .version $PROJECT_FOLDER/package.json)
CONTAINER_IMAGE=$DOCKER_REGISTRY_IMAGE:$VERSION
##########################################################
# Login to Docker Hub
echo -e "Docker Hub: ${YELLOW}Login in...${NC}"
echo "$DOCKER_REGISTRY_PASSWORD" | docker login -u "$DOCKER_REGISTRY_USER" --password-stdin "$DOCKER_REGISTRY" > /dev/null
test_if_command_unsuccessful $? "Docker Hub" "FAILED to login in"
echo -e "Docker Hub: ${GREEN}Logged in${NC}\n"
##########################################################
echo -e "Project files: ${YELLOW}Configuring...${NC}"
(
cd $PROJECT_FOLDER || exit 1
sed -i -r "s/\{\{VERSION\}\}/${VERSION}/g" src/app.ts 2> /dev/null
if [ $? -ne 0 ]; then # If on macOS
sed -i -r "s/{{VERSION}}/${VERSION}/g" src/app.ts 2> /dev/null
fi
sed -i -r "s/(DOTENV_KEY[ ]*:[ ]*[\'\"\`])[^'\"\`]*([\'\"\`])([ ]*\,)?//g" src/init.ts
sed -i -r "s/,[\ \n]*\}/\}/g" src/init.ts
# Write the content to the file
cat <<EOL > ".env"
##################### App env vars
API_URL=${API_URL}
DOCKERHUB_ASSIGNMENT_CHECKER_REPOSITORY=${DOCKER_REGISTRY_IMAGE}
EOL
)
echo -e "Project files: ${GREEN}Configured${NC}\n"
##########################################################
echo -e "Docker: ${YELLOW}Building/Uploading to Docker Hub...${NC}"
docker buildx create --use --platform=linux/amd64 > /dev/null 2> /dev/null
test_if_command_unsuccessful $? "Docker" "FAILED to initialize builder"
docker buildx build --platform=linux/amd64 --file $DOCKERFILE --push --tag "$CONTAINER_IMAGE" . > /dev/null 2> /dev/null
test_if_command_unsuccessful $? "Docker" "FAILED to build"
docker buildx imagetools create "$CONTAINER_IMAGE" --tag "$DOCKER_REGISTRY_IMAGE":latest > /dev/null 2> /dev/null
test_if_command_unsuccessful $? "Docker" "FAILED to set latest tag"
echo -e "Docker: ${GREEN}Uploaded to Docker Hub${NC}\n"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment