pages-deploy/deploy.sh
2024-04-26 16:05:10 -05:00

64 lines
2 KiB
Bash
Executable file

set -e
COMMIT_ID=$(git rev-parse --short HEAD)
FOLDER=$INPUT_FOLDER
DEPLOY_BRANCH=${INPUT_BRANCH:-pages}
CLEAN=${INPUT_CLEAN:-false}
GIT_SERVER_HOST=$(echo $GITHUB_SERVER_URL | sed 's/.*:\/\///' | sed 's/\/$//')
GIT_REMOTE="https://$GIT_SERVER_HOST/$GITHUB_REPOSITORY"
PAGES_SERVER="pages.gay"
GITHUB_REPOSITORY_AUTHOR=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f1)
GITHUB_REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2)
PAGES_HOSTNAME="$DEPLOY_BRANCH.$GITHUB_REPOSITORY_NAME.$GITHUB_REPOSITORY_AUTHOR.$PAGES_SERVER"
TMP_DIR=$(mktemp -d)
mv $FOLDER "$TMP_DIR/build"
NEEDS_NEW_BRANCH="false"
echo "fetching origin"
git fetch origin
if [ "$CLEAN" = "true" ]; then
NEEDS_NEW_BRANCH="true"
elif git rev-list --count "origin/$DEPLOY_BRANCH" 2>/dev/null; then
echo "branch $DEPLOY_BRANCH exists on remote"
else
echo "branch $DEPLOY_BRANCH does not exist on remote, needs new branch"
NEEDS_NEW_BRANCH="true"
fi
find . -maxdepth 1 ! \( -name '.git' -o -name '.' -o -name '..' \) -exec rm -rf {} +
if [ "$NEEDS_NEW_BRANCH" = "true" ]; then
echo "Making clean orphan branch"
git checkout --orphan $DEPLOY_BRANCH
else
echo "checking out $DEPLOY_BRANCH"
git switch -c $DEPLOY_BRANCH "origin/$DEPLOY_BRANCH"
fi
find . -maxdepth 1 ! \( -name '.git' -o -name '.' -o -name '..' \) -exec rm -rf {} +
cp -rT "$TMP_DIR/build" .
git config user.name "[BOT] Pages Deployer"
git config user.email "noreply.deploy@pages.gay"
echo "Making commit..."
git add --all
git commit -m "Deploy $COMMIT_ID to $PAGES_HOSTNAME"
echo "Pushing to $GIT_SERVER_HOST..."
git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@$GIT_SERVER_HOST/$GITHUB_REPOSITORY.git"
if [ "$CLEAN" = "true" ]; then
git push --force origin "$DEPLOY_BRANCH:$DEPLOY_BRANCH"
else
git push origin "$DEPLOY_BRANCH"
fi
# request with cache-control: no-cache, should update pages.gay's cache
echo "Requesting $PAGES_HOSTNAME to update cache..."
curl -sL -H 'cache-control: no-cache' "https://$PAGES_HOSTNAME" > /dev/null