DEV Community

abbazs
abbazs

Posted on

A function to clone git repository using gitlab personal access token

A step-by-step tutorial on how to use the gitlab_clone function from the provided code:

Step 1: Create a Folder for Scripts

  • Open a terminal or command prompt.
  • Run the following command to create a folder named "scripts" in your home directory:
  mkdir $HOME/scripts
Enter fullscreen mode Exit fullscreen mode

Step 2: Save the Function in a File

  • Open a text editor of your choice.
  • Copy the following code block:
  #####GITLAB CLONE WITH TOKEN############################################
  gitlab_clone() {
      get_token() {
          echo "Secrets file not found or GITLAB_TOKEN is empty. Please provide Git username and personal access token:"
          read -r -p "Username: " username
          read -r -p "Token: " token
          echo "export GITLAB_TOKEN=$username:$token" > $HOME/scripts/secrets.sh
          source $HOME/scripts/secrets.sh
      }

      if [[ -f $HOME/scripts/secrets.sh ]]; then
          source $HOME/scripts/secrets.sh
      fi

      local repo_url=$1
      local cred=${GITLAB_TOKEN}

      if [[ -z $cred ]]; then
          get_token
          cred=${GITLAB_TOKEN}  # Assign the value of GITLAB_TOKEN after calling get_token
      fi

      local url=$(echo "$repo_url" | sed "s|https://|https://$cred@|")
      git clone "$url"
  }
  alias gclone="gitlab_clone"
Enter fullscreen mode Exit fullscreen mode
  • Paste the copied code into the text editor.
  • Save the file with the name "git_func.sh" inside the "scripts" folder created in Step 1. Make sure to keep the .sh extension for shell script files.

Step 3: Source the File in .bashrc

  • Open a terminal or command prompt.
  • Run the following command to open the .bashrc file in a text editor:
  nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • Scroll to the end of the file and add the following line:
  source $HOME/scripts/git_func.sh
Enter fullscreen mode Exit fullscreen mode
  • Press Ctrl+X to exit the text editor, then press Y to save the changes, and Enter to confirm the file name.

Step 4: Activate the Changes

  • In the same terminal or command prompt, run the following command to reload the .bashrc file and activate the changes:
  source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Step 5: Using the gitlab_clone Function

  • Now, you can use the gitlab_clone function, which is aliased to gclone, in your terminal.
  • Run the following command to clone a GitLab repository:
  gclone <repository_url>
Enter fullscreen mode Exit fullscreen mode

Replace <repository_url> with the actual URL of the repository you want to clone.

  • If the GITLAB_TOKEN is not set or the secrets file doesn't exist, the function will prompt you to provide your Git username and personal access token.
  • Enter your Git username when prompted and press Enter.
  • Enter your personal access token when prompted (the input will be hidden) and press Enter.
  • The function will save the GitLab token in the secrets.sh file and automatically source it for future use.
  • The repository will be cloned using the provided credentials.

You have now set up the gitlab_clone function and can use it to clone GitLab repositories conveniently.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay