#!/usr/bin/env bash # clones repositories using Github Actions environment variables # https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables set -eu repo_name="$(echo "${GITHUB_REPOSITORY}" | cut -d'/' -f2)" git config --global --add safe.directory "/__w/$repo_name/$repo_name" # default clone depth for push events PUSH_DEPTH="${PUSH_DEPTH:-2}" # default clone depth for PR events PULL_DEPTH="${PULL_DEPTH:-1}" # only used when event isnt a push or pull_request DEFAULT_BRANCH="${DEFAULT_BRANCH:-master}" "$(dirname "${BASH_SOURCE[0]}")/delete-repo.sh" git init # Fake username because some combination of GitHub and Git are total... Gits. git remote add origin "https://token:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY" if [ "$GITHUB_EVENT_NAME" == "push" ]; then echo "This is a push event, only cloning pushed ref." # fetch by Sha instead of Ref because there may be multiple pushes in rapid # sequence to eg, master. this ensures we get commit that triggered # event git fetch origin "$GITHUB_SHA" --depth="$PUSH_DEPTH" # get last part of refs/heads/$BRANCH git switch -c "${GITHUB_REF#'refs/heads/'}" "$GITHUB_SHA" elif [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then echo "This is a pull_request event, cloning incoming and base refs." # fetch base and PR branches so that comparisons can be done git fetch origin \ --update-head-ok \ --force \ "+refs/heads/$GITHUB_BASE_REF:refs/heads/$GITHUB_BASE_REF" \ "refs/heads/$GITHUB_HEAD_REF:refs/heads/$GITHUB_HEAD_REF" \ --depth=1 echo "Checking out $GITHUB_HEAD_REF" git checkout "$GITHUB_HEAD_REF" else echo "Not a pull_request or push event, cloning $DEFAULT_BRANCH" git fetch origin "$DEFAULT_BRANCH" --depth=1 git checkout "$DEFAULT_BRANCH" fi echo "Done."