Created
October 6, 2022 07:07
-
-
Save AsP3X/16f19303387f5f38706ee62792873029 to your computer and use it in GitHub Desktop.
docker-build-script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if docker is installed otherwise refuse to continue | |
if [ -d "/usr/bin/docker" ]; then | |
echo "Docker is already installed" | |
else | |
echo "Docker is not installed. Please install Docker and try again." | |
exit 1 | |
fi | |
# Check if the file build_info.json exists and if it does, read it | |
if [ -f "build_info.json" ]; then | |
echo "build_info.json exists" | |
BUILD_INFO=$(cat build_info.json) | |
else | |
# Ask for app name | |
echo "What is the name of your app?" | |
read -p "Enter the apps name: " NEW_NAME | |
# Create build_info.json and write the app name to it | |
echo "Creating build_info.json" | |
echo "{\"name\": \"$NEW_NAME\"}" > build_info.json | |
echo "build_info.json does not exist" | |
BUILD_INFO="{}" | |
fi | |
# Get the last build version and date from the build_info.json file | |
APP_NAME=$(echo $BUILD_INFO | jq -r '.name') | |
LAST_BUILD_VERSION=$(echo $BUILD_INFO | jq -r '.last_build_version') | |
LAST_BUILD_DATE=$(echo $BUILD_INFO | jq -r '.last_build_date') | |
LAST_BUILD_TYPE=$(echo $BUILD_INFO | jq -r '.last_build_type') | |
# Get the current date and time | |
CURRENT_DATE=$(date +%Y-%m-%d) | |
CURRENT_TIME=$(date +%H:%M:%S) | |
# Display the last build version and date | |
echo "APP_NAME: $APP_NAME" | |
echo "Last build version: $LAST_BUILD_VERSION" | |
echo "Last build date: $LAST_BUILD_DATE" | |
echo "Last build type: $LAST_BUILD_TYPE" | |
# Asks if the user wants to build a new version | |
read -p "Do you want to build a new version? (y/n) " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
# Get the new version number | |
read -p "Enter the new version number: " NEW_VERSION | |
echo "New version: $NEW_VERSION" | |
# Get the new build type | |
read -p "Enter the new build type: " NEW_BUILD_TYPE | |
echo "New build type: $NEW_BUILD_TYPE" | |
# Get a short description of the changes | |
read -p "Enter a short description of the changes: " NEW_BUILD_DESCRIPTION | |
echo "New build description: $NEW_BUILD_DESCRIPTION" | |
# Build the new version | |
echo "Building the new version" | |
docker build -t "$APP_NAME:$NEW_VERSION" --build-arg BUILD_TYPE="$NEW_BUILD_TYPE" . | |
# Tag the new version | |
echo "Tagging the new version" | |
docker tag "$APP_NAME:$NEW_VERSION" "myapp:latest" | |
# Push the new version | |
echo "Pushing the new version" | |
docker push "$APP_NAME:$NEW_VERSION" | |
docker push "$APP_NAME:latest" | |
# Update the build_info.json file | |
echo "Updating build_info.json" | |
echo "{\"name\": \"$APP_NAME\", \"last_build_version\": \"$NEW_VERSION\", \"last_build_date\": \"$CURRENT_DATE\", \"last_build_time\": \"$CURRENT_TIME\", \"last_build_type\": \"$NEW_BUILD_TYPE\", \"last_build_description\": \"$NEW_BUILD_DESCRIPTION\"}" > build_info.json | |
else | |
echo "No new version will be built" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment