Builder/upload.sh
2024-09-04 19:11:37 +02:00

165 lines
6.2 KiB
Bash
Executable file

#!/bin/bash
# Load SourceForge configuration
source sourceforge_config.sh
# Function to print usage instructions
print_help() {
echo "Usage: ./upload.sh -p=<path-to-build> -t=<target> [-c|--compress] [-v|--version=<version>]"
echo ""
echo "Options:"
echo " -p, --path : Path to the build directory"
echo " -t, --target : Target location format: component-arch-release-platform"
echo " -c, --compress : Compress the build directory into a tar.gz file before uploading"
echo " -v, --version : Specify version for the package. For nightly, use current date if not specified. For stable, increment version if not specified."
echo " -h, --help : Display this help message"
echo ""
echo "Example use:"
echo " # Without compression"
echo " ./upload.sh -p=./mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin -t=browser-x86_64-stable-linux -v=1.0"
echo ""
echo " # With compression"
echo " ./upload.sh -p=./mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin -t=browser-x86_64-stable-linux -c -v=1.0"
echo ""
echo " # Nightly build without specifying version"
echo " ./upload.sh -p=./mozilla-central/obj-x86_64-pc-linux-gnu/dist/bin -t=browser-x86_64-nightly-linux -c"
exit 0
}
COMPRESS=false
VERSION=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-p=*|--path=*)
BUILD_PATH="${1#*=}"
shift
;;
-t=*|--target=*)
TARGET="${1#*=}"
shift
;;
-c|--compress)
COMPRESS=true
shift
;;
-v=*|--version=*)
VERSION="${1#*=}"
shift
;;
-h|--help)
print_help
;;
*)
echo "Invalid option: $1"
print_help
;;
esac
done
# Check if both required arguments are provided
if [ -z "$BUILD_PATH" ] || [ -z "$TARGET" ]; then
echo "Error: Both path and target must be specified."
print_help
fi
# Split the target into its components
IFS='-' read -r COMPONENT ARCH RELEASE PLATFORM <<< "$TARGET"
# Download the existing packages.json
echo "Downloading existing packages.json from SourceForge..."
scp -i "$SF_KEY_PATH" "$SF_USER@$SF_HOST:/home/frs/project/$SF_PROJECT/packages.json" packages.json 2>/dev/null || { echo "Failed to download packages.json. Creating a new one."; echo "{}" > packages.json; }
# Check if packages.json is a valid JSON object
if ! jq empty packages.json >/dev/null 2>&1; then
echo "Invalid packages.json format. Resetting to an empty JSON object."
echo "{}" > packages.json
fi
# Handle versioning
if [ -z "$VERSION" ]; then
if [[ "$RELEASE" == "nightly" ]]; then
VERSION=$(date +"%Y-%m-%d")
elif [[ "$RELEASE" == "stable" ]]; then
CURRENT_VERSION=$(jq -r --arg comp "$COMPONENT" --arg arch "$ARCH" --arg rel "$RELEASE" --arg plat "$PLATFORM" '.[$comp][$arch][$rel][$plat] | keys | map(select(test("^[0-9]+\\.[0-9]+$"))) | max' packages.json)
if [ -n "$CURRENT_VERSION" ]; then
MAJOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f1)
MINOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f2)
MINOR_VERSION=$((MINOR_VERSION + 1))
VERSION="${MAJOR_VERSION}.${MINOR_VERSION}"
else
VERSION="1.0"
fi
fi
fi
# Determine the upload directory based on the target and version
if [[ "$RELEASE" == "nightly" ]]; then
if [[ "$VERSION" == $(date +"%Y-%m-%d") ]]; then
UPLOAD_DIR="$COMPONENT/$ARCH/$RELEASE/latest"
else
UPLOAD_DIR="$COMPONENT/$ARCH/$RELEASE/$VERSION"
fi
elif [[ "$RELEASE" == "stable" ]]; then
if [[ "$VERSION" =~ ^[0-9]+\.[0-9]+$ ]]; then
UPLOAD_DIR="$COMPONENT/$ARCH/$RELEASE/$VERSION"
else
UPLOAD_DIR="$COMPONENT/$ARCH/$RELEASE/latest"
fi
fi
# Construct the remote directory path
REMOTE_DIR="/home/frs/project/$SF_PROJECT/$UPLOAD_DIR"
# Handle compression if specified
if [ "$COMPRESS" = true ]; then
COMPRESSED_FILE="/tmp/${TARGET}.tar.gz"
echo "Compressing $BUILD_PATH into $COMPRESSED_FILE..."
tar -czf "$COMPRESSED_FILE" -C "$BUILD_PATH" .
BUILD_PATH="$COMPRESSED_FILE"
fi
# Upload the files to SourceForge
echo "Uploading files from $BUILD_PATH to $REMOTE_DIR on SourceForge..."
scp -i "$SF_KEY_PATH" "$BUILD_PATH" "$SF_USER@$SF_HOST:$REMOTE_DIR/" 2>/dev/null
UPLOAD_STATUS=$?
if [ $UPLOAD_STATUS -ne 0 ]; then
echo "Failed to upload files directly. Creating local directory structure and uploading..."
# Create the local directory structure
TEMP_DIR=$(mktemp -d)
mkdir -p "$TEMP_DIR/$UPLOAD_DIR"
# Upload the directory structure
rsync -av --omit-dir-times --no-perms -e "ssh -i $SF_KEY_PATH" "$TEMP_DIR/" "$SF_USER@$SF_HOST:/home/frs/project/$SF_PROJECT/" || { echo "Failed to upload directory structure. Exiting."; rm -rf "$TEMP_DIR"; exit 1; }
# Clean up the temporary directory
rm -rf "$TEMP_DIR"
# Retry uploading the files
scp -i "$SF_KEY_PATH" "$BUILD_PATH" "$SF_USER@$SF_HOST:$REMOTE_DIR/" || { echo "Failed to upload files after creating directory structure. Exiting."; exit 1; }
fi
# Update packages.json with the new information
jq --arg comp "$COMPONENT" --arg arch "$ARCH" --arg rel "$RELEASE" --arg plat "$PLATFORM" --arg ver "$VERSION" \
'if .[$comp] == null then .[$comp] = {} else .[$comp] end |
if .[$comp][$arch] == null then .[$comp][$arch] = {} else .[$comp][$arch] end |
if .[$comp][$arch][$rel] == null then .[$comp][$arch][$rel] = {} else .[$comp][$arch][$rel] end |
if .[$comp][$arch][$rel][$plat] == null then .[$comp][$arch][$rel][$plat] = {} else .[$comp][$arch][$rel][$plat] end |
.[$comp][$arch][$rel][$plat][$ver] = $ver' packages.json > packages_temp.json && mv packages_temp.json packages.json
# Upload the updated packages.json to the root directory
echo "Uploading packages.json to the root directory on SourceForge..."
scp -i "$SF_KEY_PATH" packages.json "$SF_USER@$SF_HOST:/home/frs/project/$SF_PROJECT/" || { echo "Failed to upload packages.json. Exiting."; exit 1; }
echo "Upload completed successfully."
# Clean up compressed file if it was created
if [ "$COMPRESS" = true ]; then
rm "$COMPRESSED_FILE"
fi
exit 0