jenkinsgitlabjenkins-pipeline

How to clone private GitLab repo in Jenkinsfile


I am trying to do a git clone of a private GitLab repo inside a shell script in Jenkinsfile and I have not been able to find a way. It is unfortunate that GitLab support always comes with extra cost compared to GitHub.

I have searched the internet and can't find a solution to my problem. I have the GitLab plugin and also tried to use the GitLab API token, but there is no support for GitLab API token in Jenkins, no way to use it with withcredentials or any other thing.

I have also tried regular withcredentials with username and password and that does not work either.

I also tried http and git repo url. The http one says not found and the git or ssh one gives host key errors.

I tried this:

   steps {
     git url: 'https://gitlab.example.com/user/example_repo.git', branch: 'master', credentialsId: 'my-gitlab-repo-creds'
   }

got this error

git: 'url:' is not a git command. See 'git --help'.

Did you mean this? prune

other errors from other trials and errors

Cloning into 'example_repo'... Host key verification failed. fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

and

Cloning into 'example_repo'... fatal: could not read Username for 'https://gitlab.example.com': No such device or address


Solution

  • I would recommend to use the checkout pipeline step instead of the git one.

                checkout([$class: 'GitSCM',
                    branches: [[name: '*/master' ]],
                    extensions: scm.extensions,
                    userRemoteConfigs: [[
                        url: 'https://gitlab.example.com/user/example_repo.git',
                        credentialsId: 'my-gitlab-repo-creds'
                    ]]
                ])
    

    https://jenkins.io/doc/pipeline/steps/git/