|
| 1 | +name: 'Sync External Files' |
| 2 | +description: 'Fetch specific files from an external GitHub repository and overwrite local copies when they differ' |
| 3 | + |
| 4 | +inputs: |
| 5 | + repository: |
| 6 | + description: 'GitHub repository in owner/repo format (e.g., redhat-developer/rhdh)' |
| 7 | + required: true |
| 8 | + ref: |
| 9 | + description: 'Branch, tag, or ref to fetch from' |
| 10 | + required: false |
| 11 | + default: 'main' |
| 12 | + files: |
| 13 | + description: 'Newline-separated list of source-path:destination-path file mappings' |
| 14 | + required: true |
| 15 | + current-hash: |
| 16 | + description: 'Previously tracked commit hash. If provided and matches the remote HEAD, the clone and diff are skipped entirely.' |
| 17 | + required: false |
| 18 | + default: '' |
| 19 | + |
| 20 | +outputs: |
| 21 | + changes_detected: |
| 22 | + description: 'Whether any file content differed (true/false)' |
| 23 | + value: ${{ steps.sync.outputs.changes_detected }} |
| 24 | + commit_hash: |
| 25 | + description: 'HEAD commit SHA of the fetched ref' |
| 26 | + value: ${{ steps.sync.outputs.commit_hash }} |
| 27 | + |
| 28 | +runs: |
| 29 | + using: 'composite' |
| 30 | + steps: |
| 31 | + - name: Sync files from external repository |
| 32 | + id: sync |
| 33 | + shell: bash |
| 34 | + env: |
| 35 | + INPUT_REPOSITORY: ${{ inputs.repository }} |
| 36 | + INPUT_REF: ${{ inputs.ref }} |
| 37 | + INPUT_FILES: ${{ inputs.files }} |
| 38 | + INPUT_CURRENT_HASH: ${{ inputs.current-hash }} |
| 39 | + run: | |
| 40 | + set -euo pipefail |
| 41 | +
|
| 42 | + REPO_URL="https://github.com/${INPUT_REPOSITORY}.git" |
| 43 | +
|
| 44 | + if [[ -n "$INPUT_CURRENT_HASH" ]]; then |
| 45 | + echo "::group::Check remote HEAD for ${INPUT_REPOSITORY}@${INPUT_REF}" |
| 46 | + REMOTE_HASH=$(git ls-remote "$REPO_URL" "refs/heads/${INPUT_REF}" | cut -f1) |
| 47 | + if [[ -z "$REMOTE_HASH" ]]; then |
| 48 | + REMOTE_HASH=$(git ls-remote "$REPO_URL" "${INPUT_REF}" | cut -f1) |
| 49 | + fi |
| 50 | + echo "Remote HEAD: ${REMOTE_HASH:-unknown}" |
| 51 | + echo "Current hash: ${INPUT_CURRENT_HASH}" |
| 52 | + echo "::endgroup::" |
| 53 | +
|
| 54 | + if [[ "$REMOTE_HASH" == "$INPUT_CURRENT_HASH" ]]; then |
| 55 | + echo "Remote hash unchanged - skipping clone and sync" |
| 56 | + echo "commit_hash=${INPUT_CURRENT_HASH}" >> "$GITHUB_OUTPUT" |
| 57 | + echo "changes_detected=false" >> "$GITHUB_OUTPUT" |
| 58 | + exit 0 |
| 59 | + fi |
| 60 | + echo "Remote hash differs - proceeding with sync" |
| 61 | + fi |
| 62 | +
|
| 63 | + CLONE_DIR=$(mktemp -d) |
| 64 | + trap "rm -rf '$CLONE_DIR'" EXIT |
| 65 | +
|
| 66 | + # Parse source file paths from mappings for sparse checkout |
| 67 | + SOURCE_FILES=() |
| 68 | + while IFS= read -r mapping; do |
| 69 | + mapping=$(echo "$mapping" | xargs) |
| 70 | + [[ -z "$mapping" ]] && continue |
| 71 | + src="${mapping%%:*}" |
| 72 | + SOURCE_FILES+=("$src") |
| 73 | + done <<< "$INPUT_FILES" |
| 74 | +
|
| 75 | + if [[ ${#SOURCE_FILES[@]} -eq 0 ]]; then |
| 76 | + echo "Error: No file mappings provided" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | +
|
| 80 | + echo "::group::Sparse clone ${INPUT_REPOSITORY}@${INPUT_REF}" |
| 81 | + git clone --depth 1 --branch "$INPUT_REF" --filter=blob:none --no-checkout \ |
| 82 | + "$REPO_URL" "$CLONE_DIR" |
| 83 | +
|
| 84 | + pushd "$CLONE_DIR" > /dev/null |
| 85 | + git sparse-checkout init --no-cone |
| 86 | + git sparse-checkout set "${SOURCE_FILES[@]}" |
| 87 | + git checkout |
| 88 | + COMMIT_HASH=$(git rev-parse HEAD) |
| 89 | + popd > /dev/null |
| 90 | + echo "::endgroup::" |
| 91 | +
|
| 92 | + echo "Fetched commit: ${COMMIT_HASH}" |
| 93 | + echo "commit_hash=${COMMIT_HASH}" >> "$GITHUB_OUTPUT" |
| 94 | +
|
| 95 | + CHANGES_DETECTED="false" |
| 96 | +
|
| 97 | + echo "::group::Compare and sync files" |
| 98 | + while IFS= read -r mapping; do |
| 99 | + mapping=$(echo "$mapping" | xargs) |
| 100 | + [[ -z "$mapping" ]] && continue |
| 101 | +
|
| 102 | + src="${mapping%%:*}" |
| 103 | + dest="${mapping#*:}" |
| 104 | + src_full="${CLONE_DIR}/${src}" |
| 105 | +
|
| 106 | + if [[ ! -f "$src_full" ]]; then |
| 107 | + echo "::warning::Source file not found: ${src}" |
| 108 | + continue |
| 109 | + fi |
| 110 | +
|
| 111 | + if [[ ! -f "$dest" ]] || ! diff -q "$src_full" "$dest" > /dev/null 2>&1; then |
| 112 | + echo "Updating: ${src} -> ${dest}" |
| 113 | + mkdir -p "$(dirname "$dest")" |
| 114 | + cp -f "$src_full" "$dest" |
| 115 | + CHANGES_DETECTED="true" |
| 116 | + else |
| 117 | + echo "No changes: ${dest}" |
| 118 | + fi |
| 119 | + done <<< "$INPUT_FILES" |
| 120 | + echo "::endgroup::" |
| 121 | +
|
| 122 | + echo "changes_detected=${CHANGES_DETECTED}" >> "$GITHUB_OUTPUT" |
| 123 | +
|
| 124 | + if [[ "$CHANGES_DETECTED" == "true" ]]; then |
| 125 | + echo "Changes detected - files have been updated" |
| 126 | + else |
| 127 | + echo "No changes detected - all files are up to date" |
| 128 | + fi |
0 commit comments