0

Context

I'm currently working on a CMake project that uses Jenkins for its continuous integration.

This CMake project includes a number of libraries which it retrieves using FetchContent. Basically, rather than using Git submodules, we chose to use FetchContent.

The Jenkins that clone the project, and credentials to this project are easily passed on thanks to the git plugin.

Problem

The problem is that the git plugin does not pass credentials as it did during the clone, in the configuration phase of my CMake project, so that the configuration fails. I have this logs

[1/9] Performing download step (git clone) for '<my_module>-populate'

Cloning into '<my_module>'...
fatal: could not read Username for 'https://<my_private_server>': No such device or address

The start of a solution?

After much searching, I came to use the withCredentials function, which makes my stage look like this:

stage('Project Configure') {
    steps {
        withCredentials([gitUsernamePassword(credentialsId: '<CredentialID>']) {
            sh "cmake --preset=MyPreset"
        }
    }
}

But this doesn't completely solve the problem, as only the user name is passed, but not the password. The logs are:

[1/9] Performing download step (git clone) for '<my_module>-populate'
Cloning into '<my_module>'...
****: 3: ?!: not found
error: unable to read askpass response from ****
fatal: could not read Password for 'https://<my_user>@<my_private_server>': terminal prompts disabled

Limitation

I also thought of using the SSH link instead of the HTTPS link in the Git repository, but that doesn't work, because the functionality isn't available on the server.

Brinfer
  • 398
  • 1
  • 10

2 Answers2

0

Finally, after much searching, I found the solution (or rather the error).

All you have to do is this:

stage('Project Configure') {
    steps {
         withCredentials([gitUsernamePassword(credentialsId: '<CredentialID>']) {
            sh "cmake --preset=MyPreset"
        }
    }
}

The error I had was that my password contained special characters (notably a &) and that they were interpreted (linked to this issue?).

I just had to change the password for it to work properly.

Brinfer
  • 398
  • 1
  • 10
-1

To address the issue with passing credentials to the configuration phase of your CMake project in Jenkins, you can consider the following steps:

  1. Use the withCredentials function: Modify your Jenkins pipeline stage to use the withCredentials function to pass both the username and password. Update your stage as follows:
stage('Project Configure') {
    steps {
        withCredentials([
            usernamePassword(credentialsId: '<CredentialID>', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')
        ]) {
            sh "export GIT_ASKPASS=/path/to/askpass.sh" // Set the path to your askpass script
            sh "echo $GIT_PASSWORD | git clone https://<my_user>@<my_private_server>/<my_module>.git"
            sh "cmake --preset=MyPreset"
        }
    }
}
  1. Use an askpass script: Create an askpass script (e.g., askpass.sh) that will provide the password when Git prompts for it. The script should contain the following:
#!/bin/bash
echo "$GIT_PASSWORD"

Make sure to update the path to the askpass script in the Jenkins pipeline step.

  1. Grant execute permissions: Ensure that the askpass script has execute permissions. You can do this by running the following command in the terminal:
chmod +x /path/to/askpass.sh
  1. Configure Git to use the askpass script: Configure Git to use the askpass script by setting the GIT_ASKPASS environment variable to the path of the askpass script. This step is included in the modified pipeline stage above.

With these modifications, both the username and password will be passed to the Git clone command during the configuration phase of your CMake project. This should allow the configuration to proceed without encountering authentication issues.

Note: If the SSH link is not available on the server, using the askpass script approach with HTTPS should provide a solution.

  • Isn't it possible to pass the echo command directly into the `GIT_ASKPASS` rather than using a script that does it? And the clone is made by CMake, so I don't have to invoke the `git clone` command. – Brinfer Jun 07 '23 at 07:08