name: 'Archive Cache' description: 'Create cache archive and prepare for upload' inputs: cache_path: description: 'Path where cache is saved' required: true type: string cache_key: description: 'Unique key for the cache' required: true type: string cache_bucket: description: 'The tag name for the release' required: true default: 'general-cache' compression_level: description: 'Compression level (0-9)' required: false type: number default: 6 working_dir: description: 'Path where .git folder exists' required: false type: string debug: description: 'Enable Logs' required: false type: boolean default: false runs: using: 'composite' steps: - name: Archive and Split Cache shell: bash env: GH_TOKEN: ${{ inputs.github_token }} TARGET_REPO: "${{ github.repository }}" run: | set -euo pipefail if [ "${{ inputs.working_dir }}" != "" ]; then cd "${{ inputs.working_dir }}" fi git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" TAG_NAME="${{ inputs.cache_bucket }}" BASE_FILENAME="cache-${{ inputs.cache_key }}" if [ "${{ inputs.compression_level }}" -eq 0 ]; then FILENAME="$BASE_FILENAME.tar" else FILENAME="$BASE_FILENAME.tzst" fi # Check release state git fetch --tags --force if ! git rev-parse "$TAG_NAME" >/dev/null 2>&1; then echo "Creating release: $TAG_NAME" GIT_COMMITTER_DATE="2015-01-01T12:00:00" git tag -a "$TAG_NAME" -m "General Cache Storage" git push origin "$TAG_NAME" --force || true gh release create "$TAG_NAME" --title "General Cache" --notes "Automated storage for build assets" --prerelease --repo "$TARGET_REPO" || true fi # Check if path exists if [ ! -d "${{ inputs.cache_path }}" ] && [ ! -f "${{ inputs.cache_path }}" ]; then echo "[!] Target path not found. Skipping cache save." exit 0 fi DIR_NAME=$(dirname "${{ inputs.cache_path }}") BASE_NAME=$(basename "${{ inputs.cache_path }}") if [ "${{ inputs.debug }}" = "true" ]; then if [ "${{ inputs.compression_level }}" -eq 0 ]; then tar --posix -h -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" else tar --posix -h -I "zstd -T0 -${{ inputs.compression_level }}" -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" fi else if [ "${{ inputs.compression_level }}" -eq 0 ]; then tar --posix -h -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1 else tar --posix -h -I "zstd -T0 -${{ inputs.compression_level }}" -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1 fi fi FILE_SIZE=$(stat -c%s "$FILENAME") echo "Archive created: $FILENAME ($FILE_SIZE bytes)" # Determine if splitting is needed MAX_SIZE=2000000000 chunk_size="1500M" split_required=false if [ "$FILE_SIZE" -gt "$MAX_SIZE" ]; then split_required=true fi if [ "$split_required" = "true" ]; then echo "Splitting into ${chunk_size} chunks..." split -b "$chunk_size" -a 2 "$FILENAME" "${FILENAME}.part" NEW_PARTS=(${FILENAME}.part*) NEW_PARTS_COUNT=${#NEW_PARTS[@]} echo "Split complete, $NEW_PARTS_COUNT parts created" # Remove old single-file asset if switching to split parts ALL_ASSETS=$(curl -sL -A "Mozilla/5.0" -w "%{http_code}" "https://github.com/${TARGET_REPO}/releases/expanded_assets/$TAG_NAME" -o assets.html 2>/dev/null || echo "0") if [ "$ALL_ASSETS" = "200" ]; then ALL_ASSETS=$(grep -oP "download/$TAG_NAME/\K[\"' ]+" assets.html 2>/dev/null || echo "") if echo "$ALL_ASSETS" | grep -xF "${FILENAME}" >/dev/null 2>&1; then gh release delete-asset "$TAG_NAME" "${FILENAME}" --repo "$TARGET_REPO" -y || true fi # Clean up excess trailing parts from previous builds OLD_PARTS=$(echo "$ALL_ASSETS" | grep -F "${FILENAME}.part" 2>/dev/null || echo "") if [ -n "$OLD_PARTS" ]; then OLD_PARTS_COUNT=$(echo "$OLD_PARTS" | wc -l) if [ "$OLD_PARTS_COUNT" -gt "$NEW_PARTS_COUNT" ]; then EXT_PARTS=$(echo "$OLD_PARTS" | awk -v skip="$NEW_PARTS_COUNT" 'NR>skip') for asset in $EXT_PARTS; do gh release delete-asset "$TAG_NAME" "$asset" --repo "$TARGET_REPO" -y || true done fi fi fi rm -f assets.html else # Remove old split parts if new build fits in a single file ALL_ASSETS=$(curl -sL -A "Mozilla/5.0" -w "%{http_code}" "https://github.com/${TARGET_REPO}/releases/expanded_assets/$TAG_NAME" -o assets.html 2>/dev/null || echo "0") if [ "$ALL_ASSETS" = "200" ]; then OLD_PARTS=$(grep -F "${FILENAME}.part" assets.html 2>/dev/null || echo "") if [ -n "$OLD_PARTS" ]; then while read -r asset; do [ -z "$asset" ] && continue gh release delete-asset "$TAG_NAME" "$asset" --repo "$TARGET_REPO" -y || true done <<< "$OLD_PARTS" fi fi rm -f assets.html fi echo "CACHE_FILE=$FILENAME" >> $GITHUB_OUTPUT echo "CACHE_SPLIT=$split_required" >> $GITHUB_OUTPUT echo "CACHE_PARTS=${NEW_PARTS_COUNT:-1}" >> $GITHUB_OUTPUT