Skip to content

Instantly share code, notes, and snippets.

@ganezasan
Created January 16, 2020 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganezasan/4c1eecb9eab302cba16edb3a3ab8ad7c to your computer and use it in GitHub Desktop.
Save ganezasan/4c1eecb9eab302cba16edb3a3ab8ad7c to your computer and use it in GitHub Desktop.
shallow clone example
# copy from this orb
# https://circleci.com/orbs/registry/orb/guitarrapc/git-shallow-clone
version: 2.1
commands:
checkout_shallow:
description: checkout with git shallow clone. Support Alpine, Ubuntu, Debian and
others.
parameters:
depth:
default: 1
description: |
Limit fetch depth to the specified number of commit from a remote branch history. Refer git fetch documentation for more information.
type: integer
fetch_depth:
default: 10
description: |
Addtional fetch depth fetch depth to the specified number of commit from a remote branch history. Pass more number then depth when you want to check futher commit history.
type: integer
keyscan_bitbucket:
default: false
description: |
Pass `true` to dynamically get ssh-rsa from `bitbucket.org`.
type: boolean
keyscan_github:
default: false
description: |
Pass `true` to dynamically get ssh-rsa from `github.com`.
type: boolean
steps:
- run:
command: |
#!/bin/sh
set -ex
# 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
# known_hosts
mkdir -p ~/.ssh
if [ -x "$(command -v ssh-keyscan)" ] && ([ "<< parameters.keyscan_github >>" == "true" ] || [ "<< parameters.keyscan_bitbucket >>" == "true" ])
then
if [ "<< parameters.keyscan_github >>" == "true" ]
then
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
fi
if [ "<< parameters.keyscan_bitbucket >>" == "true" ]
then
ssh-keyscan -H bitbucket.org >> ~/.ssh/known_hosts
fi
fi
if [ "<< parameters.keyscan_github >>" != "true" ]
then
echo 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
' >> ~/.ssh/known_hosts
fi
if [ "<< parameters.keyscan_bitbucket >>" != "true" ]
then
echo 'bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==
' >> ~/.ssh/known_hosts
fi
(umask 077; touch ~/.ssh/id_rsa)
chmod 0600 ~/.ssh/id_rsa
(cat $CHECKOUT_KEY > ~/.ssh/id_rsa)
# use git+ssh instead of https
git config --global url."ssh://git@github.com".insteadOf "https://github.com" || true
git config --global gc.auto 0 || true
# checkout
git clone --depth << parameters.depth >> $CIRCLE_REPOSITORY_URL .
# Fetch remote and check the commit ID of the checked out code
if [ -n "$CIRCLE_TAG" ]
then
# tag
git fetch -t --depth << parameters.fetch_depth >> --force origin "refs/tags/${CIRCLE_TAG}"
elif [[ $(echo $CIRCLE_BRANCH | grep -e ^pull\/*) ]] # sh version of bash `elif [[ "$CIRCLE_BRANCH" =~ ^pull\/* ]]`
then
# pull request
git fetch --depth << parameters.fetch_depth >> --force origin "${CIRCLE_BRANCH}/head:remotes/origin/${CIRCLE_BRANCH}"
else
# others
git fetch --depth=<< parameters.fetch_depth >> --force origin "$CIRCLE_BRANCH:remotes/origin/$CIRCLE_BRANCH"
fi
# Check the commit ID of the checked out code
if [ -n "$CIRCLE_TAG" ]
then
git reset --hard "$CIRCLE_SHA1"
git checkout -q "$CIRCLE_TAG"
elif [ -n "$CIRCLE_BRANCH" ] && [ "$CIRCLE_BRANCH" != 'HEAD' ]
then
git reset --hard "$CIRCLE_SHA1"
git checkout -q -B "$CIRCLE_BRANCH"
fi
git reset --hard "$CIRCLE_SHA1"
name: Checkout code shallow
jobs:
build:
docker:
- image: circleci/node:latest
steps:
- checkout_shallow
workflows:
version: 2
build:
jobs:
- build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment