52 lines
1.4 KiB
Bash
52 lines
1.4 KiB
Bash
#!/usr/bin/env bash
|
|
# Clones or updates the repo to WORKSPACE_DIR/<repo-name> and checks out the
|
|
# target commit. Sets REPO_DIR in $GITHUB_ENV on success.
|
|
#
|
|
# Required env vars (provided by GitHub Actions context):
|
|
# WORKSPACE_DIR, GITHUB_SERVER_URL, GITHUB_REPOSITORY, github.sha,
|
|
# github.ref, github.ref_name, github.event.repository.name
|
|
# Optional:
|
|
# WORKFLOW secret (used as OAuth token for private repos)
|
|
set -euo pipefail
|
|
|
|
REPO_NAME="${REPO_NAME}"
|
|
REPO_DIR="${WORKSPACE_DIR}/${REPO_NAME}"
|
|
TOKEN="${TOKEN:-}"
|
|
|
|
if [ -n "$TOKEN" ]; then
|
|
REPO_URL="https://oauth2:${TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git"
|
|
else
|
|
REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
|
|
fi
|
|
|
|
if [ -d "$REPO_DIR" ]; then
|
|
if [ -d "$REPO_DIR/.git" ]; then
|
|
cd "$REPO_DIR"
|
|
git clean -fdx
|
|
git reset --hard
|
|
git fetch --all --tags --force --prune --prune-tags
|
|
else
|
|
rm -rf "$REPO_DIR"
|
|
fi
|
|
fi
|
|
|
|
if [ ! -d "$REPO_DIR/.git" ]; then
|
|
mkdir -p "$WORKSPACE_DIR"
|
|
git clone "$REPO_URL" "$REPO_DIR"
|
|
cd "$REPO_DIR"
|
|
fi
|
|
|
|
if git -C "$REPO_DIR" cat-file -e "${TARGET_SHA}^{commit}" 2>/dev/null; then
|
|
git -C "$REPO_DIR" checkout -f "$TARGET_SHA"
|
|
else
|
|
if [ -n "${TARGET_REF:-}" ]; then
|
|
git -C "$REPO_DIR" fetch origin "$TARGET_REF"
|
|
git -C "$REPO_DIR" checkout -f FETCH_HEAD
|
|
else
|
|
git -C "$REPO_DIR" checkout -f "$TARGET_REF_NAME"
|
|
fi
|
|
fi
|
|
|
|
git config --global --add safe.directory "$REPO_DIR"
|
|
echo "REPO_DIR=$REPO_DIR" >> "$GITHUB_ENV"
|