...
1#!/usr/bin/env bash
2
3# clones repositories using Github Actions environment variables
4# https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables
5
6set -eu
7
8repo_name="$(echo "${GITHUB_REPOSITORY}" | cut -d'/' -f2)"
9git config --global --add safe.directory "/__w/$repo_name/$repo_name"
10
11# default clone depth for push events
12PUSH_DEPTH="${PUSH_DEPTH:-2}"
13# default clone depth for PR events
14PULL_DEPTH="${PULL_DEPTH:-1}"
15# only used when event isnt a push or pull_request
16DEFAULT_BRANCH="${DEFAULT_BRANCH:-master}"
17
18"$(dirname "${BASH_SOURCE[0]}")/delete-repo.sh"
19
20git init
21# Fake username because some combination of GitHub and Git are total... Gits.
22git remote add origin "https://token:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY"
23
24if [ "$GITHUB_EVENT_NAME" == "push" ]; then
25 echo "This is a push event, only cloning pushed ref."
26 # fetch by Sha instead of Ref because there may be multiple pushes in rapid
27 # sequence to eg, master. this ensures we get commit that triggered
28 # event
29 git fetch origin "$GITHUB_SHA" --depth="$PUSH_DEPTH"
30 # get last part of refs/heads/$BRANCH
31 git switch -c "${GITHUB_REF#'refs/heads/'}" "$GITHUB_SHA"
32elif [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
33 echo "This is a pull_request event, cloning incoming and base refs."
34 # fetch base and PR branches so that comparisons can be done
35 git fetch origin \
36 --update-head-ok \
37 --force \
38 "+refs/heads/$GITHUB_BASE_REF:refs/heads/$GITHUB_BASE_REF" \
39 "refs/heads/$GITHUB_HEAD_REF:refs/heads/$GITHUB_HEAD_REF" \
40 --depth=1
41 echo "Checking out $GITHUB_HEAD_REF"
42 git checkout "$GITHUB_HEAD_REF"
43else
44 echo "Not a pull_request or push event, cloning $DEFAULT_BRANCH"
45 git fetch origin "$DEFAULT_BRANCH" --depth=1
46 git checkout "$DEFAULT_BRANCH"
47fi
48
49echo "Done."
View as plain text