Git Deployment: Automatic for Staging, Manual for Prod

fs_cl

Registered
Mar 3, 2022
3
0
1
New York
cPanel Access Level
Website Owner
We are using a shared server with CPanel installed. We have two folders/sites setup ("www" and "staging") and two subdomains pointing to those folders/sites. We will be using Git for source control. The "master" branch will be for prod/"www" and we will create a "staging" branch for "staging". We would like commits and pushes to the "staging" branch to automatically deploy to our "staging" folder. For commits and pushes to the "master" branch, we want to do a manual deployment. Can this be accomplished with CPanel?

Thanks
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
15,235
2,423
363
cPanel Access Level
Root Administrator
Hey there! I don't see why this wouldn't work well on a cPanel system, but you just would be using these tools outside of cPanel itself. As long as the permissions and ownership on the files getting pulled from the Git repo are correct, I'd expect it to work.
 

fs_cl

Registered
Mar 3, 2022
3
0
1
New York
cPanel Access Level
Website Owner
Can you elaborate? I created a .cpanel.yml file with a condition that checks for the current git branch and deploys to a folder based on the branch. That's good. But it always deploys automatically. I would like it to deploy automatically if the branch is "staging" but not if "master." I want "master" to only deploy when the deployment is started manually.

Are they are any environment variables that I can read in the .cpanel.yml file that will tell me if the deployment was started manually vs automatically? If so, I should be able to use that variable.

My current .cpanel.yml file.

YAML:
---
deployment:
  tasks:      
    - export current_branch=$(git rev-parse --abbrev-ref HEAD)  
    - echo $current_branch
    - if [ $current_branch == "master" ]; then export DEPLOYPATH=/home/my_username/site_prod/; fi;
    - if [ $current_branch == "staging" ]; then export DEPLOYPATH=/home/my_username/site_staging/; fi;
    - echo $DEPLOYPATH
    - /bin/cp -R ./Code/. $DEPLOYPATH
Thanks
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
15,235
2,423
363
cPanel Access Level
Root Administrator
I'm not sure I really have much to elaborate on, honestly. I think it would be easier to have two deployments, and just push to the one you want to update. My original recommendation was just not using the cPanel Git tools at all, and strictly using Git from the command line interface, as that would eliminate any restrictions or limitations from our implementation.
 

fs_cl

Registered
Mar 3, 2022
3
0
1
New York
cPanel Access Level
Website Owner
Unfortunately, that doesn't help me much.

2 Deployments? I already have 2 folders and corresponding git branches. Are you suggesting a second cPanel, or, in our case, a second hosting account?

And I use the git command line interface but that is on my local machine. That doesn't have anything to do with what the server does once the changes have been pushed. That is controlled by .cpanel.yml (correct?).

Unless I can view a variable/parameter within the .cpanel.yml script, that specifies how the deployment was started, I don't see this working on a single cPanel.

The only way I can see "2 Deployments" working would be:
- 2 separate hosting accounts/cpanels
- Staging cPanel uses its own cPanel git repo (with 2 branches - master and staging)
- When "staging" pushes are made, since the Staging server hosts the git repo, the changes will be automatically deployed (with "staging" logic in the .cpanel.yml).
- The Prod/master cPanel uses the repo on the Staging cpanel/server (essentially a remote repo).
- Since the Prod cpanel is not hosting the repo, changes need to be deployed manually (going into cPanel and click "Deploy HEAD Commit" - with "master" logic in the .cpanel.yml).

Thoughts?

Thanks
 

DroneSpec

Registered
Aug 20, 2022
1
0
0
Arizona
cPanel Access Level
Website Owner
I had this question too, but maybe the OP should have said "How do you" instead of "Can you"? Fortunately I was able to figure it out from other sources. Here is example of what I did to only deploy when the master branch is pushed:
YAML:
---
deployment:
  tasks:
    - export DEPLOYPATH=/home/public_html/
    - export SRCPATH=/home/repositories/
    - /bin/cp $SRCPATH/file $DEPLOYPATH
...
  when:
    condition:
      all:
        executeForMasterBranch: "'${{CF_BRANCH}}' == 'master'"
Hope this is helpful
Thanks
 
Last edited: