|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +usage() { |
| 5 | + cat <<EOF |
| 6 | +Usage: $0 <owner> <repo> <branch> <target-path> <artifact-file> <commit-message> <github-token> |
| 7 | +
|
| 8 | +Arguments: |
| 9 | + owner : GitHub user or organization of the target repo |
| 10 | + repo : Target repo name |
| 11 | + branch : Branch name (e.g. main) |
| 12 | + target-path : Full path (including folders + filename) in the target repo where to upload |
| 13 | + artifact-file : Local file path to upload |
| 14 | + commit-message : Commit message for creating/updating the file |
| 15 | + github-token : GitHub token (PAT or equivalent) with write access to the target repo |
| 16 | +
|
| 17 | +This will perform a GitHub API PUT to /repos/{owner}/{repo}/contents/{target-path}. |
| 18 | +If a file already exists at that path, it will auto-detect the SHA and update; otherwise it will create. |
| 19 | +EOF |
| 20 | + exit 1 |
| 21 | +} |
| 22 | + |
| 23 | +if [ $# -ne 7 ]; then |
| 24 | + usage |
| 25 | +fi |
| 26 | + |
| 27 | +OWNER="$1" |
| 28 | +REPO="$2" |
| 29 | +BRANCH="$3" |
| 30 | +TARGET_PATH="$4" |
| 31 | +ARTIFACT_PATH="$5" |
| 32 | +COMMIT_MSG="$6" |
| 33 | +TOKEN="$7" |
| 34 | + |
| 35 | +if [ ! -f "$ARTIFACT_PATH" ]; then |
| 36 | + echo "Error: artifact file not found: $ARTIFACT_PATH" >&2 |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +LOCAL_SHA=$(git hash-object "$ARTIFACT_PATH") |
| 41 | +echo "Local blob SHA: $LOCAL_SHA" |
| 42 | + |
| 43 | +GET_URL="https://api.github.com/repos/${OWNER}/${REPO}/contents/${TARGET_PATH}?ref=${BRANCH}" |
| 44 | +GET_RESPONSE=$(curl -s -H "Authorization: token ${TOKEN}" "$GET_URL") |
| 45 | + |
| 46 | +REMOTE_SHA=$(echo "$GET_RESPONSE" | jq -r .sha 2>/dev/null || echo "") |
| 47 | + |
| 48 | +if [ "$REMOTE_SHA" != "null" ] && [ -n "$REMOTE_SHA" ]; then |
| 49 | + echo "Remote blob SHA: $REMOTE_SHA" |
| 50 | + if [ "$LOCAL_SHA" = "$REMOTE_SHA" ]; then |
| 51 | + echo "The remote file is identical. Skipping upload." |
| 52 | + exit 0 |
| 53 | + else |
| 54 | + echo "Remote file differs. Preparing to upload." |
| 55 | + fi |
| 56 | +else |
| 57 | + echo "No existing remote file or no SHA found. Creating." |
| 58 | +fi |
| 59 | + |
| 60 | +CONTENT_TMP_BASE64=$(mktemp) |
| 61 | +if base64 --help 2>&1 | grep -q -- "-w"; then |
| 62 | + base64 -w 0 "$ARTIFACT_PATH" > "$CONTENT_TMP_BASE64" |
| 63 | +else |
| 64 | + base64 "$ARTIFACT_PATH" | tr -d '\n' > "$CONTENT_TMP_BASE64" |
| 65 | +fi |
| 66 | + |
| 67 | +PAYLOAD_TMP=$(mktemp) |
| 68 | +jq -n \ |
| 69 | + --arg message "$COMMIT_MSG" \ |
| 70 | + --arg branch "$BRANCH" \ |
| 71 | + --arg sha "$REMOTE_SHA" \ |
| 72 | + --rawfile content "$CONTENT_TMP_BASE64" \ |
| 73 | + '{ |
| 74 | + message: $message, |
| 75 | + content: $content, |
| 76 | + branch: $branch |
| 77 | + } + (if ($sha != "" and $sha != "null") then { sha: $sha } else {} end)' \ |
| 78 | + > "$PAYLOAD_TMP" |
| 79 | + |
| 80 | +UPLOAD_RESPONSE=$(curl -s -X PUT \ |
| 81 | + -H "Authorization: token ${TOKEN}" \ |
| 82 | + -H "Content-Type: application/json" \ |
| 83 | + -d @"$PAYLOAD_TMP" \ |
| 84 | + "https://api.github.com/repos/${OWNER}/${REPO}/contents/${TARGET_PATH}") |
| 85 | + |
| 86 | +echo "Upload Response:" |
| 87 | +echo "$UPLOAD_RESPONSE" |
| 88 | + |
| 89 | +rm -f "$CONTENT_TMP_BASE64" "$PAYLOAD_TMP" |
0 commit comments