Builder/upload.sh

143 lines
4.7 KiB
Bash
Raw Normal View History

2024-08-03 18:11:24 +00:00
#!/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"
# Determine the upload directory based on the target
UPLOAD_DIR="$COMPONENT/$ARCH/$RELEASE/$PLATFORM"
# Construct the remote directory path
REMOTE_DIR="/home/frs/project/$SF_PROJECT/$UPLOAD_DIR"
# Handle versioning
if [ -z "$VERSION" ]; then
if [[ "$RELEASE" == "nightly" ]]; then
VERSION=$(date +"%Y%m%d")
elif [[ "$RELEASE" == "stable" ]]; then
CURRENT_VERSION=$(grep -oP '(?<="'"$COMPONENT"'": ")([^"]*)' 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
# Update packages.json
if [ ! -f packages.json ]; then
echo "{}" > packages.json
fi
jq --arg comp "$COMPONENT" --arg ver "$VERSION" '.[$comp] = $ver' packages.json > packages_temp.json && mv packages_temp.json packages.json
# 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
# 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