Background
I have a Jenkins build pipeline that dynamically spins up a Windows VM, clones a repo containing utilities for our release process, and then uses a python script in that repo to clone a separate private Github repo containing the actual source code and then build it. The Jenkins pipeline is also responsible for providing the github personal access token to the VM.
Previously we had a single dedicated machine for doing this (instead of dynamically-provisioned VMs) and that machine had a private key pair that it used for cloning the private repos via SSH.
Problem
Our IT department does not want us to an SSH key that would have to be shared between a dynamic number of VMs and is requiring that we switch to HTTPS using an access token.
While I can easily clone the repos using git clone https://{TOKEN}@github.com/{org}/{repo}, I run into problems when I try to update the submodules because the .gitmodules file is configured to use SSH.
What I've Tried
I have had some success by:
- Running
git config --global url."https://github.com/".insteadOf "[email protected]:"to configure git to substitute the SSH URLs with the equivalent HTTPS URL. - After doing this I can clone the repo which prompts me with a GUI to input the credentials. I put in my access token and the repo clones successfully. (Not sure if it matters but the current
credential.credentialStorein my gitconfig is dpapi but I'm not tied to this.) - Run
git submodule update --initfrom within the cloned repo, which works successfully since the access token is now stored from step 2.
The problem with this method is that I have to interact with the GUI interface to input the access token, which only works when I am doing this manually. I need this to be an automated process but I cannot figure out how to add the access token as a credential before running the clone operation.
Limitations
- I have to use HTTPS, I cannot use SSH; this is mandated by our IT department.
- I can't rewrite
.gitmodulesto use HTTPS URLs because this will break it for our devs who will still be using SSH to authenticate. - I can't move the logic from the python script into the Jenkins pipeline directly, this is simply out of scope at the moment.
TL;DR
How can I preset the Github access token in my credentials or environment before running the initial clone operation, such that it doesn't require me to type/paste it manually? I'm also open to another approach for making sure that the repos containing the code can be seamlessly cloned and have its submodules updated.
Update
Here's what eventually (mostly) worked for me.
- I changed the credential helper to
storeto prevent having to interact with the GUI element. - It turned out this was being overridden in the gitconfig file in the Git/etc directory to use
wincredmainso I switched the helper there instead of in the global config. - Now I can preload the credentials using
git credential approveand settingprotocol=https,host=github.comandusernameandpassword. This still required manual interaction but it was all from the command line which was a step in the right direction. - I can write the credentials manually to
%UserProfile%\.git-credentialsin order to reproduce the results of doing step 3 interactively. However, this adds a CRLF to the end of the credential line in the file which causes git to ignore it. I have to manually convert the EOLs to Unix-style (just LF). I'd love a solution to this but I will post it as a separate question.