Skip to content

Instantly share code, notes, and snippets.

@samandmoore
Created August 2, 2018 22:21
Show Gist options
  • Save samandmoore/1df4f75c002e673d830d387d09c3692b to your computer and use it in GitHub Desktop.
Save samandmoore/1df4f75c002e673d830d387d09c3692b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
PROJECT_DIR="${CIRCLE_WORKING_DIRECTORY/#\~/$HOME}"
# Workaround old docker images with incorrect $HOME
# check https://github.com/docker/docker/issues/2968 for details
if [ "${HOME}" = "/" ]
then
export HOME=$(getent passwd $(id -un) | cut -d: -f6)
fi
mkdir -p ~/.ssh
ssh-keyscan github.com > ~/.ssh/known_hosts
(umask 077; touch ~/.ssh/id_rsa)
chmod 0600 ~/.ssh/id_rsa
(cat <<EOF > ~/.ssh/id_rsa
$CHECKOUT_KEY
EOF
)
# use git+ssh instead of https
echo "Use git+SSH for git config"
git config --global url."ssh://git@github.com".insteadOf "https://github.com" || true
if [ -e $PROJECT_DIR/.git ]
then
echo "Setting remote host for git repo"
cd $PROJECT_DIR
git remote set-url origin "$CIRCLE_REPOSITORY_URL" || true
else
echo "git directory not found, fetch without tags"
mkdir -p $PROJECT_DIR
cd $PROJECT_DIR
git init
git remote add origin "$CIRCLE_REPOSITORY_URL"
fi
git fetch --no-tags --force origin "$CIRCLE_BRANCH"
git reset --hard "$CIRCLE_SHA1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment