|
3 | 3 |
|
4 | 4 | if [ -n "$GITHUB_TOKEN" ]; then |
5 | 5 | echo "Using HTTPS with GitHub token" |
| 6 | + |
| 7 | + # Verify token is not empty after trimming |
| 8 | + if [ -z "${GITHUB_TOKEN// }" ]; then |
| 9 | + echo "Error: GITHUB_TOKEN is set but empty" |
| 10 | + exit 1 |
| 11 | + fi |
6 | 12 |
|
7 | 13 | # Overwrite the submodule URL with token-authenticated URL |
8 | 14 | git submodule set-url apps/web/src/content/newsletters-premium \ |
9 | 15 | https://$GITHUB_TOKEN:@github.com/apsinghdev/opensox-newsletters-premium.git |
10 | 16 |
|
11 | 17 | elif [ -n "$GIT_SSH_KEY" ]; then |
12 | 18 | echo "Using SSH key authentication" |
| 19 | + |
| 20 | + if [ -z "${GIT_SSH_KEY// }" ]; then |
| 21 | + echo "Error: GIT_SSH_KEY is set but empty" |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + |
| 25 | + if ! echo "$GIT_SSH_KEY" | grep -qE "^(ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp256|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521|-----BEGIN)"; then |
| 26 | + echo "Error: GIT_SSH_KEY does not appear to be a valid SSH key" |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | + |
| 30 | + # Determine key type from the key header and use appropriate filename |
| 31 | + if echo "$GIT_SSH_KEY" | grep -q "^ssh-ed25519"; then |
| 32 | + SSH_KEY_FILE="$HOME/.ssh/id_ed25519" |
| 33 | + elif echo "$GIT_SSH_KEY" | grep -q "^ssh-rsa"; then |
| 34 | + SSH_KEY_FILE="$HOME/.ssh/id_rsa" |
| 35 | + elif echo "$GIT_SSH_KEY" | grep -q "^ecdsa-sha2"; then |
| 36 | + SSH_KEY_FILE="$HOME/.ssh/id_ecdsa" |
| 37 | + elif echo "$GIT_SSH_KEY" | grep -q "^-----BEGIN"; then |
| 38 | + # PEM format - could be RSA, ECDSA, or Ed25519, default to RSA |
| 39 | + SSH_KEY_FILE="$HOME/.ssh/id_rsa" |
| 40 | + else |
| 41 | + # Fallback to ed25519 if type cannot be determined |
| 42 | + SSH_KEY_FILE="$HOME/.ssh/id_ed25519" |
| 43 | + fi |
| 44 | + |
13 | 45 | mkdir -p ~/.ssh |
14 | | - printf '%s' "$GIT_SSH_KEY" > ~/.ssh/id_ed25519 |
15 | | - chmod 600 ~/.ssh/id_ed25519 |
| 46 | + printf '%s' "$GIT_SSH_KEY" > "$SSH_KEY_FILE" |
| 47 | + chmod 600 "$SSH_KEY_FILE" |
16 | 48 | ssh-keyscan github.com >> ~/.ssh/known_hosts |
| 49 | + |
| 50 | + # Configure SSH to use the specific key file for GitHub |
| 51 | + cat >> ~/.ssh/config <<EOF |
| 52 | +Host github.com |
| 53 | + IdentityFile $SSH_KEY_FILE |
| 54 | + IdentitiesOnly yes |
| 55 | +EOF |
| 56 | + chmod 600 ~/.ssh/config |
| 57 | + |
17 | 58 | else |
18 | | - echo "No authentication found!" |
| 59 | + echo "⚠️ No authentication found! Skipping premium submodule initialization." |
| 60 | + echo "Note: Public newsletters will still work. Premium newsletters require authentication." |
| 61 | + echo "Status: SKIPPED (exit code 0 - build will continue without premium content)" |
| 62 | + exit 0 |
19 | 63 | fi |
20 | 64 |
|
| 65 | +# Only disable exit-on-error for submodule update to allow graceful failure |
| 66 | +set +e |
21 | 67 | git submodule update --init --recursive --force |
22 | | -echo "Submodules initialized successfully" |
| 68 | +submodule_exit_code=$? |
| 69 | +set -e |
| 70 | + |
| 71 | +if [ $submodule_exit_code -eq 0 ]; then |
| 72 | + echo "Submodules initialized successfully" |
| 73 | +else |
| 74 | + echo "Error: Submodule initialization failed after authentication was provided." |
| 75 | + echo "This may indicate network issues, invalid credentials, or repository problems." |
| 76 | + exit $submodule_exit_code |
| 77 | +fi |
0 commit comments