0

For a little background, I am trying to use GIT functionality as a way of deploying a website. I have been following this:

https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks

To me, in order for it to work, I need to be able to get code out of one Git Repository and transfer it to another that is initialized with "git init --bare". By doing this, webhooks fire and the code is deployed to the correct directory on the server (as seen in the link)

What I have been doing:

I have a Target Repository that has been initialized with: git init --bare

I have an Original Repository - stored in Gitlab - that has a branch named "cons".

I have a job that is supposed to do the following on a regular basis:

  1. get the contents of the "cons" branch (from the Original Repository)
  2. push the "cons" branch to the Target Repository

So, essentially, I need to set up a repetitive job that will copy code from the Original Repository to the Target Repository.

To me, the flow would look like:

  1. initialize Target Repository (get rid of all the files)
  2. clone the branch "cons" from the Original Repository
  3. change "cons" branch to "master" branch
  4. move the "master" branch to the Target Repository

I have tried something like this:

  1. to initialize the remote repository REFERENCE: Delete all files and history from remote Git repo without deleting repo itself

    problem: but since this will be running on a regular basis, I felt that there should be a way to do it in one command

  2. create a work directory and "cd' into it

  3. clone the branch git clone -b cons --single-branch git@repos.com:CCLVIN/laravel-work.git

    this works but it creates a "repository" with only a branch name of "cons", there is no "master" branch. In order for me to push to the Target Repository, I need to have a master branch.

What can one do to resolve this problem?

TIA

Community
  • 1
  • 1
Casey Harrils
  • 2,793
  • 12
  • 52
  • 93
  • 1
    1.Clone the repo from Original Repository. 2.`git remote add origin ;git push origin cons:master` where `origin` is the url of Target Repository. I don't know if Step 1 can be done by Gitlab apis. You don't have to clone the whole repo. Fetching master only is enough. If you can access the server which hosts Target, run `git fetch origin cons:master` in Target. This origin is the url of the Original in Gitlab. – ElpieKay Mar 31 '17 at 03:40
  • Thanks for the response. I tried the notes above but got the following when executing "git push origin cons:master". Basically, the message I got was: "error: src refspec cons does not match any." I am wondering if this happened because the Target Directory was created with "git init --bare" – Casey Harrils Mar 31 '17 at 03:54
  • run `git remote -v` to check if the push url of origin is right. It should look like `ssh://host/Target.git` or `http://host/Target.git`. Besides you need make sure the branch `cons` does exist. – ElpieKay Mar 31 '17 at 03:59
  • Thanks for he information. Along with your advice, I followed this here: http://stackoverflow.com/questions/27635955/how-do-i-create-a-master-branch-in-a-bare-git-repository and now I have code in the Target Repository. – Casey Harrils Mar 31 '17 at 04:27
  • This is supposed to run on a CI server - so - another goal was to make sure not so much code stayed on this host. The idea was to put this into a type of job so that it can be ran periodically. The goal was also to assign a new version with the run of each job. – Casey Harrils Mar 31 '17 at 04:27
  • Am I to understand you correctly in that you suggest to run "git clone" to initialize everything and then run "git fetch" afterwards to get the changes? If so, how would one incorporate the use of versions in this case? TIA – Casey Harrils Mar 31 '17 at 04:28
  • sorry English is not my mother tongue. I don't understand what you mean by 'incorporate the use of versions'. Clone-and-fetch is one of the possible modes. I am a bit confused since you initialized the Target with --bare. A bare repo is for storage purpose only. No code is able to be checked out in it. I read your post again and find the 3rd you tried is ok. You can either rename the branch cons to master or create master based on cons (check git branch --help) after the clone. – ElpieKay Mar 31 '17 at 05:03
  • >> A bare repo is for storage purpose only. No code is able to be checked out in it. Thanks for this. Basically, I am using Git as a way to deploy websites. I followed something similar to this documentation here: http://stackoverflow.com/questions/27635955/how-do-i-create-a-master-branch-in-a-bare-git-repository – Casey Harrils Mar 31 '17 at 05:38
  • >> I don't understand what you mean by 'incorporate the use of versions I suppose in Git, it would be called "Tags" - so, the idea would be each time the job runs a new version would be created: Version 1.0, 1.1, 1.2, etc. But, based upon what you have said, it seems that the versions would be generated in the Source Repository first (~before~ the cloning takes place) – Casey Harrils Mar 31 '17 at 05:39
  • as long as you know a commit or a ref (e.g HEAD, master) that points to it, you can always create a git tag by `git tag `. It can be done either in Source or Target. – ElpieKay Mar 31 '17 at 06:47

0 Answers0