1

I want to git pull a project in GitLab via bash in Jenkins pipeline. It looks something like this:

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                git branch: 'develop',
                    credentialsId: '12345-123-123123-1f54-123141e67c',
                    url: 'ssh://git@gitlab.blabla:PORT/group/project.git'
                
                sh 'cd /var/www/project/'
                sh 'git pull origin develop'
            }
        }
    }

}

But it requires inserting a username and password for every request. I find out about adding the credentials using these ways that stated in this SO discussion.

But using git config credential.helper will store the credentials in plain text which is it is not safe. And also cloning the project with the username and password in the git URL like this https://user:pass@domain/repo is also not safe.

Is there any way to do it securely?

alramdein
  • 810
  • 2
  • 12
  • 26

1 Answers1

1

Sure. You can:

        stage('Cloning git repository') {
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/dev']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'my-repository']], userRemoteConfigs: [[credentialsId: 'MY_GIT_CREDENTIAL', url: 'https://github.com/user/my-repository.git']]])
                    
                }
            }
        }

Then just cd my-repository. You don't need to do that through a sh step. Jenkins will hand credentials through the checkout() step. You can use SSH keys or username and password stored into Jenkins credentials with that.

Best regards.

Stefano Martins
  • 472
  • 2
  • 7
  • Is this script do a `git clone`? I need to do a `git pull`. How to do that? – alramdein Apr 14 '21 at 05:40
  • Jenkins works using a distributed approach. If in the node Jenkins is ran there's already a workspace with the repository in it, the step will perform a `git fetch` and a `git pull`. If there's not, it will then clone it. – Stefano Martins Apr 14 '21 at 12:13