|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script for building and distributing release artifacts. |
| 4 | +# Used by the build-distribute.yml workflow. |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +COMMAND="${1}" |
| 9 | +shift |
| 10 | + |
| 11 | +case "${COMMAND}" in |
| 12 | + |
| 13 | + ############################################################################### |
| 14 | + # Generate a version string from the current commit SHA. |
| 15 | + # Outputs: |
| 16 | + # commit_sha: First 8 characters of GITHUB_SHA. |
| 17 | + # version: VERSION_PREFIX-commit_sha. |
| 18 | + ############################################################################### |
| 19 | + generate-version) |
| 20 | + # GITHUB_SHA, VERSION_PREFIX, and GITHUB_OUTPUT are set by the GitHub Actions runner. |
| 21 | + # shellcheck disable=SC2154 |
| 22 | + COMMIT_SHA="${GITHUB_SHA:0:8}" |
| 23 | + # shellcheck disable=SC2154 |
| 24 | + VERSION="${VERSION_PREFIX}-${COMMIT_SHA}" |
| 25 | + # shellcheck disable=SC2154 |
| 26 | + echo "commit_sha=${COMMIT_SHA}" >> "${GITHUB_OUTPUT}" |
| 27 | + echo "version=${VERSION}" >> "${GITHUB_OUTPUT}" |
| 28 | + echo "Generated version: ${VERSION} (commit: ${COMMIT_SHA})" |
| 29 | + ;; |
| 30 | + |
| 31 | + ############################################################################### |
| 32 | + # Create the distribution directory for a given architecture and copy action |
| 33 | + # files into it. |
| 34 | + # Arguments: |
| 35 | + # 1: Architecture name (e.g., X64, ARM64, ARM, X86). |
| 36 | + ############################################################################### |
| 37 | + create-distribute-directory) |
| 38 | + ARCH="${1}" |
| 39 | + DIST_DIR="distribute/${ARCH}" |
| 40 | + mkdir -p "${DIST_DIR}" |
| 41 | + echo "Created distribution directory: ${DIST_DIR}" |
| 42 | + |
| 43 | + # Copy shell scripts and action definition. |
| 44 | + cp ./*.sh "${DIST_DIR}/" |
| 45 | + cp action.yml "${DIST_DIR}/" |
| 46 | + echo "Copied action files to ${DIST_DIR}" |
| 47 | + ;; |
| 48 | + |
| 49 | + ############################################################################### |
| 50 | + # Clone the apt-fast repository for bundling with the distribution. |
| 51 | + ############################################################################### |
| 52 | + clone-apt-fast) |
| 53 | + echo "Cloning apt-fast repository..." |
| 54 | + git clone --depth=1 https://github.com/ilikenwf/apt-fast.git apt-fast-repo |
| 55 | + echo "Cloned apt-fast repository to apt-fast-repo/" |
| 56 | + ;; |
| 57 | + |
| 58 | + ############################################################################### |
| 59 | + # Build the apt_query binary for a target architecture. |
| 60 | + # Arguments: |
| 61 | + # 1: GOOS (e.g., linux). |
| 62 | + # 2: GOARCH (e.g., amd64, arm64, arm, 386). |
| 63 | + # 3: GOARCH variant (e.g., "6" for armv6; empty string if not applicable). |
| 64 | + # 4: Architecture name (e.g., X64, ARM64, ARM, X86). |
| 65 | + ############################################################################### |
| 66 | + build-binary) |
| 67 | + GOOS="${1}" |
| 68 | + GOARCH="${2}" |
| 69 | + GOARCH_VARIANT="${3}" |
| 70 | + ARCH="${4}" |
| 71 | + |
| 72 | + ARCH_LOWER="$(echo "${ARCH}" | tr '[:upper:]' '[:lower:]')" |
| 73 | + DIST_DIR="distribute/${ARCH}" |
| 74 | + BINARY_NAME="apt_query-${ARCH_LOWER}" |
| 75 | + OUTPUT="${DIST_DIR}/${BINARY_NAME}" |
| 76 | + |
| 77 | + echo "Building ${BINARY_NAME} for ${GOOS}/${GOARCH} (variant: ${GOARCH_VARIANT:-none})..." |
| 78 | + GOOS="${GOOS}" GOARCH="${GOARCH}" GOARM="${GOARCH_VARIANT}" CGO_ENABLED=0 \ |
| 79 | + go build -o "${OUTPUT}" ./src/cmd/apt_query |
| 80 | + chmod +x "${OUTPUT}" |
| 81 | + echo "Binary built: ${OUTPUT} ($(du -h "${OUTPUT}" | cut -f1))" |
| 82 | + |
| 83 | + # Copy the apt-fast install script if available. |
| 84 | + if [[ -f "apt-fast-repo/apt-fast" ]]; then |
| 85 | + cp apt-fast-repo/apt-fast "${DIST_DIR}/" |
| 86 | + echo "Bundled apt-fast script in ${DIST_DIR}" |
| 87 | + fi |
| 88 | + ;; |
| 89 | + |
| 90 | + ############################################################################### |
| 91 | + # Generate SHA256 checksums for all files in the distribution directory. |
| 92 | + # Arguments: |
| 93 | + # 1: Architecture name (e.g., X64, ARM64, ARM, X86). |
| 94 | + ############################################################################### |
| 95 | + generate-checksums) |
| 96 | + ARCH="${1}" |
| 97 | + DIST_DIR="distribute/${ARCH}" |
| 98 | + CHECKSUM_FILE="${DIST_DIR}/checksums.txt" |
| 99 | + |
| 100 | + echo "Generating checksums for ${DIST_DIR}..." |
| 101 | + (cd "${DIST_DIR}" && sha256sum -- * > checksums.txt) |
| 102 | + echo "Checksums written to ${CHECKSUM_FILE}" |
| 103 | + cat "${CHECKSUM_FILE}" |
| 104 | + ;; |
| 105 | + |
| 106 | + ############################################################################### |
| 107 | + # Verify the build output for a given architecture. |
| 108 | + # Arguments: |
| 109 | + # 1: Architecture name (e.g., X64, ARM64, ARM, X86). |
| 110 | + ############################################################################### |
| 111 | + verify-build) |
| 112 | + ARCH="${1}" |
| 113 | + ARCH_LOWER="$(echo "${ARCH}" | tr '[:upper:]' '[:lower:]')" |
| 114 | + DIST_DIR="distribute/${ARCH}" |
| 115 | + BINARY_NAME="apt_query-${ARCH_LOWER}" |
| 116 | + OUTPUT="${DIST_DIR}/${BINARY_NAME}" |
| 117 | + |
| 118 | + if [[ ! -f "${OUTPUT}" ]]; then |
| 119 | + echo "Error: Binary not found: ${OUTPUT}" >&2 |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | + |
| 123 | + echo "Verifying build: ${OUTPUT}" |
| 124 | + file "${OUTPUT}" |
| 125 | + |
| 126 | + # Verify it's an ELF executable. |
| 127 | + if ! file "${OUTPUT}" | grep -q "ELF"; then |
| 128 | + echo "Error: ${OUTPUT} is not a valid ELF executable" >&2 |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | + |
| 132 | + echo "Build verified successfully: ${OUTPUT}" |
| 133 | + ;; |
| 134 | + |
| 135 | + ############################################################################### |
| 136 | + # Reorganize downloaded artifacts from distribute-artifacts/ into the |
| 137 | + # distribute/<arch>/ layout expected by the release step. |
| 138 | + # Artifact directories are named cache-apt-pkgs-<ARCH>-<commit_sha> and |
| 139 | + # are mapped to distribute/<arch_lowercase>/. |
| 140 | + ############################################################################### |
| 141 | + reorganize-artifacts) |
| 142 | + echo "Reorganizing artifacts..." |
| 143 | + for artifact_dir in distribute-artifacts/cache-apt-pkgs-*/; do |
| 144 | + if [[ ! -d "${artifact_dir}" ]]; then |
| 145 | + continue |
| 146 | + fi |
| 147 | + |
| 148 | + dir_name="$(basename "${artifact_dir}")" |
| 149 | + # Extract arch: strip prefix "cache-apt-pkgs-" and trailing "-<commit_sha>" |
| 150 | + arch_upper="$(echo "${dir_name}" | sed 's/^cache-apt-pkgs-//; s/-[^-]*$//')" |
| 151 | + arch_lower="$(echo "${arch_upper}" | tr '[:upper:]' '[:lower:]')" |
| 152 | + dest_dir="distribute/${arch_lower}" |
| 153 | + |
| 154 | + mkdir -p "${dest_dir}" |
| 155 | + cp -r "${artifact_dir}"* "${dest_dir}/" |
| 156 | + echo "Reorganized ${artifact_dir} -> ${dest_dir}" |
| 157 | + done |
| 158 | + echo "Artifact reorganization complete" |
| 159 | + ;; |
| 160 | + |
| 161 | + *) |
| 162 | + echo "Error: Unknown command: ${COMMAND}" >&2 |
| 163 | + echo "Usage: distribute.sh <command> [args...]" >&2 |
| 164 | + echo "Commands:" >&2 |
| 165 | + echo " generate-version" >&2 |
| 166 | + echo " create-distribute-directory <arch>" >&2 |
| 167 | + echo " clone-apt-fast" >&2 |
| 168 | + echo " build-binary <goos> <goarch> <goarch_variant> <arch>" >&2 |
| 169 | + echo " generate-checksums <arch>" >&2 |
| 170 | + echo " verify-build <arch>" >&2 |
| 171 | + echo " reorganize-artifacts" >&2 |
| 172 | + exit 1 |
| 173 | + ;; |
| 174 | + |
| 175 | +esac |
0 commit comments