Skip to content

Instantly share code, notes, and snippets.

@ashishjullia
Created November 23, 2023 06:18
Show Gist options
  • Save ashishjullia/28299edcea0d666f593072fcb345d979 to your computer and use it in GitHub Desktop.
Save ashishjullia/28299edcea0d666f593072fcb345d979 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Define variables
REPO_URL="git@github.com:<org>/<repo-name>.git"
PRIVATE_KEY_FILE="<>"
HOST_REPO_PATH="./<local-dir-name>"
DOCKER_IMAGE="ubuntu"
GIT_USER_NAME="<>"
GIT_USER_EMAIL="<>"
# Ensure the host directory exists
mkdir -p "$HOST_REPO_PATH"
# Use a Docker container to clone the repository
sudo docker run --rm -it \
-v "$HOST_REPO_PATH":/repo \
-v "$PRIVATE_KEY_FILE":/ssh_private_key.pem \
$DOCKER_IMAGE /bin/bash -c "
apt-get update && apt-get install -y openssh-client git;
mkdir -p /root/.ssh;
cp /ssh_private_key.pem /root/.ssh/id_rsa;
chmod 600 /root/.ssh/id_rsa;
# Start the SSH agent and add your key
eval \$(ssh-agent -s);
ssh-add /root/.ssh/id_rsa;
# Add GitHub to known hosts
ssh-keyscan github.com >> /root/.ssh/known_hosts;
# Clone the repository
git clone $REPO_URL /repo;
# Configure Git to recognize the /repo directory as a safe repository
git config --global --add safe.directory /repo;
# Configure Git user information
git config --global user.email '$GIT_USER_EMAIL'
git config --global user.name '$GIT_USER_NAME'
# Change directory to /repo
cd /repo;
# Keep the container running with an interactive shell
exec bash;
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment