UAPI Clone Remote Git Repository

Operating System & Version
linux
cPanel & WHM Version
86.0 (build 21)

akreu

Member
Jun 10, 2020
5
0
1
USA
cPanel Access Level
Reseller Owner
I'm looking to automatically clone a public git repo using the UAPI. The following code is working as far as creating a git repository into the public_html, however it's not actually cloning the remote repo. Am I missing something that would make it clone the remote repo?

Example:
Code:
https://www.domain.com:2083/cpsess3276056603/execute/VersionControl/create?type=git&name=test&repository_root=%2Fhome%2Ftest%2Fpublic_html&branch=master&source_repository%3D%27%7B%22remote_name%22%3A%22origin%22%2C%22url%22%3A%22https%3A%2F%2Fmycompany%40bitbucket.org%2Fmycompany%2Ftest.git%22%7D%27

And that returns the following:
JSON:
{"status":1,"messages":null,"warnings":null,"metadata":{},"errors":null,"data":{"name":"test","tasks":[],"last_update":null,"available_branches":[],"deployable":0,"repository_root":"/home/test/public_html","last_deployment":null,"clone_urls":{"read_write":["ssh://[email protected]:7822/home/test/public_html"],"read_only":[]},"type":"git","branch":null}}
So now I have a .git folder in my public_html but it didn't clone any files from the remote repository. My end goal is to push to my remote repo and then pull those changes into public_html.

Any ideas what I'm missing? Thanks!
 

akreu

Member
Jun 10, 2020
5
0
1
USA
cPanel Access Level
Reseller Owner
Just adding to this, my end goal really is just be able to automatically clone a remote repo as soon as their account is created. Willing to do this via a hook or in any other manner if there's a way. Figured UAPI was the way to go but can't see to get it to work other than just creating a git repo.
 

cPSamuelM

Technical Analyst Team Lead
Staff member
Nov 20, 2019
196
38
103
USA
cPanel Access Level
Root Administrator
Hello @akreu

Thank you for contacting cPanel! The VersionControl::create API function should automatically clone your repo. You can find a detailed tutorial about deploying applications via API calls on the following page:

https://blog.cpanel.com/cpanel-application-manager-and-app-deployment-101/

Reviewing the output you received when you executed the API call, I did notice the following:

"available_branches":[]

Are you certain the branch is published and you set up SSH key authentication? The following pages should help you validate that:

https://docs.cpanel.net/knowledge-b...to-git-set-up-access-to-private-repositories/

https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html

Please let us know if you have any questions.
 

akreu

Member
Jun 10, 2020
5
0
1
USA
cPanel Access Level
Reseller Owner
Thanks @cPSamuel I'll take a look at those links.

In my API call, it's a public repo and I'm using HTTPS link instead of SSH to avoid having to do any extra steps / create keys each time. (maybe there's a way to generate a single key that will work for all of my clients? Then I could use a private repo)

But yeah, I need this to run seamlessly / automatically when a new customer signs up. Basically they're ordering a template and hosting from us, and I'm wanting the template to be auto deployed via git. But yea, the caveat here is I need it to run automatically once their cpanel account is created. So I'm not sure I could have that work if needing to generate a new SSH key each time and manually add it to my bitbucket account.

I have everything pushed to master branch in our public repo
 

cPSamuelM

Technical Analyst Team Lead
Staff member
Nov 20, 2019
196
38
103
USA
cPanel Access Level
Root Administrator
Hello again @akreu

Thanks for the update. Now that you mention it, I do see that you provided an https:// URL for the repository in your API call. But then I noticed that in the API output, there is only an SSH URL in the clone_urls section:

"clone_urls":{"read_write":["ssh://[email protected]:7822/home/test/public_html"],"read_only":[]}

I'm curious about whether this is because the API is not properly recognizing a read_only https:// URL, or if maybe Bitbucket is not configured to make it visible. I understand that ultimately you would like to use the API to automatically clone the repo upon creating a cPanel account, and therefore using SSH keys may not be ideal. As a preliminary troubleshooting step can you let us know if you can successfully clone the repo via the command line with the https:// URL? I'm not sure if this will be possible with cPanel's implementation of Git Version Control, as our documentation says SSH access is required:

https://docs.cpanel.net/knowledge-b...to-git-set-up-access-to-private-repositories/

Please let us know if you have any success with cloning via the https:// URL on the command line.

Best regards
 

akreu

Member
Jun 10, 2020
5
0
1
USA
cPanel Access Level
Reseller Owner
Hey @cPSamuel

So I tried cloning via command line with the https:// url and it actually didn't work because the directory was not empty. It looks like cpanel automatically adds a cig-bin and .well-known folder to the public_html directory. Although I'm not sure if those folders are added before or after the API runs. If I run git clone from the command line on an empty directory under public_html, it works just fine.

Trying to think of any other ways to achieve this workflow.. guess it might be back to the drawing board lol

Thanks!
 

akreu

Member
Jun 10, 2020
5
0
1
USA
cPanel Access Level
Reseller Owner
Just thought of something, not sure if this would be possible -- but maybe an idea.

I could add my repos '.git' folder to the cpanel3-skel/public_html folder so it's automatically included on each account. If I have that already in there and then run the following via command line:

Code:
git fetch origin master
git reset --hard origin/master
it then pulls my repo into public_html without any issues. So now my next question would be, is it possible to fire off these 2 shell commands via the UAPI?

EDIT

So I actually got this to work using a php library that allows me to ssh into the server remotely, so I'm able to clone my repository that way. Now I'm running into a separate issue.

In doing this, I'm also creating a cron job that will automatically run git pull so I can have each server check for code updates. I'm doing the following to achieve this which actually does create a new cron job visible from cpanel:

Code:
https://www.domain.com:2083/cpsess3276056603/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=Cron&cpanel_jsonapi_func=add_line&command=cd%20/home/myuser/public_html%20%26%26%20git%20pull&day=*&hour=*&minute=*&month=*&weekday=*
However, it doesn't seem like the cron job ever actually runs. Any ideas on how to troubleshoot that aspect?

Thanks!
 
Last edited: