104

Is it necessary to have git repo on openshift only? I already have bitbucket / github git repo and would prefer to push there only. Can I simply hook into it so that openshift gets intimation ?

Or for simplification, I push only at github, but when I want to deploy, I do something with openshift?

I did check this but it confused me: it's talking about merging exiting and new (openshift) git ?

1
  • 6
    Could you proofread the question? It's very hard to understand. Dec 10, 2012 at 21:35

11 Answers 11

229

I have the impression that you're not used to use git enough yet. I'd advise you to get into git to fully understand how to push your code to openshift. Nevertheless let me try to explain you the steps involved: As you'd do with git in general, the approach to choose here is to clone your other git repo (ex. on bitbucket) to your local machine:

git clone <bitbucket-repo-url>

Your local clone has then your other repo (bitbucket etc.) as remote repo. Your remote repo is stored with the alias "origin" (the default alias used by git if you clone). You then add the openshift repo as remote to your clone. You do that while explicitly using an alias for the remote repo you add - I'm using "openshift" as alias here:

git remote add openshift -f <openshift-git-repo-url>

In order to then be able to push the code from your local git repo to openshift you first have to merge your openshift repo with your local bitbucket clone. You do that by issuing locally:

git merge openshift/master -s recursive -X ours

With this command you tell git to merge the master branch in the openshift git repo with your local git repo. You tell it to merge using the recursive merging strategy and to choose your ("ours") version when there are conflicts.

Once the merge is executed you're ready to push your git repo to openshift. You do that by doing:

git push openshift HEAD

You tell git to push your local code to the HEAD branch on the remote repo called "openshift" (the alias we stored the openshift git repo at, some paragraphs further up).

btw. I wrote a jboss tools blog which was demonstrating how to use the openshift-java-client some months ago: https://community.jboss.org/wiki/Enable-openshift-ciFullExampleUsingOpenshift-java-client . You'll spot the above steps in the last paragraph "We're almost there".

12
  • 30
    I think this still does not answer the question. The question is about not using openshift's git repo as the remote, but instead using the repo on github (or bitbucket for that matter) as the git repo. A push to github repo used for collaborating should also ensure that it gets reflected in openshift. I'm also looking for the same but have not find the answer. I'll try to update if I get a solution for this
    – Manoj N V
    Jan 30, 2013 at 17:32
  • 9
    You cannot not use the openshift git repo. The git repo within OpenShift is how you hand OpenShift your code over. There's no alternative, there's no "use github instead". As I tried to outline above, the git repo on OpenShift is not excluding you from using github. If you use github/bitbucket/XX as your master source control repo - and most users will do that - , then you would simply add the OpenShift git repo as remote to your local github/bitbucket/XX-clone. Pushing to OpenShift is then equivalent to deplyoing to OpenShift. Feb 20, 2013 at 16:21
  • 1
    If I understand it, if I work with openshift I should work with a development repo (for example github) and if I want to deploy it just push on openshift HEAD, riht?
    – Ricardo
    Nov 13, 2013 at 21:03
  • 1
    Worth noting that the -f flag, and the ssh part of the git URL are both essential
    – Simon H
    Aug 28, 2014 at 12:11
  • 1
    @adietisheim with the new git 2.9 you will need to add --allow-unrelated-histories as git default has changed to not allow merging unrelated histories.
    – Alon Burg
    Sep 18, 2016 at 10:59
23

I know the question is 2-year old and the @adietisheim's answer has been accepted. I personally don't like to merge the openshift repo into my local clone because I don't want to mix the OpenShift repo into the master branch of my public repo.

Assuming you have added the remote using git remote add openshift <openshift-git-repo-url>, here is what I would do:

Create a new local branch openshift based on the master branch.

git checkout -b openshift

You could make some commits on the branch openshift such as your app deployment configurations. Then, push the current branch to the remote ref matching master in the OpenShift repository with the flag -f to overwrite everything in the remote master branch.

git push openshift master -f

Whenever I want to deploy my app to OpenShift, I would check out the local openshift branch and merge master branch with it, then force push to OpenShift, however -f may not be required for the next pushes:

git checkout openshift
git merge --no-ff master
git push openshift master -f
6

From you project folder, do

git remote add backup user@server:/path/to/git/test.git
git push backup master

You can read Pushing to two git remote origins from one repository and Changing git remote origin.

2
  • git push backup master is sufficient, you don't need to specify both sides of the refspec.
    – user456814
    Jun 23, 2014 at 7:43
  • 1
    The tip to push to 2 git remote is perfect. You can also then do a: git push -u all to 'all' to the default remote. When doing git push, it will the subsequently push to the 2 repos ! May 29, 2015 at 22:18
5

I agree with @adietisheim's answer: you need to understand better git before deploying with openshift =)

Now, even if you understand git, it is not necessarily obvious how to deploy your existing repo if your directory structure does not match the directory structure required by openshift, and if you want to keep your old directory structure.

For that, I have the following tips:

  • separate options that are deployment dependent from those that are not into different files. For example, I separate my database settings from other settings into different files as:

    • settings_deploy/openshift

    • settings_deploy/localhost

    and then symlink to your localhost test as something like:

    ln -s settings_deploy/localhost settings_deploy_file
    

    Another option is to detect the host by using environment variables:

    if 'OPENSHIFT_APP_NAME' in os.environ:
        //openshift configurations
    else:
        //localhost
    

    This is a bit simpler since it allows you to put all the configs on a single file. It is a bit less general, since if ever another one of your hosts offers an OPENSHIFT_APP_NAME environment variable (unlikely for this one) the method breaks. Anyways, you still have to clearly separate what is deployment dependent and what is not.

  • create a local deploy directory

  • clone the initial openshift template into it

  • create a deploy script that:

    • hardlinks everything from your old existing local to their correct locations at the

      hardlinks are fast to create and use very little memory

      you could use something like:

      cp -lrf original_repo_dir deploy_repo_dir

    • keep only the correct settings_deploy file in the deploy repo:

      cd deploy_repo

      mv settings_deploy/openshift settings_deploy_file

      rm -r settings_deploy

    • force push:

      cd deploy_repo

      git push -f origin master

    • clean the deploy repo:

      git reset --hard HEAD

      git clean -df

for those interested in django deployment, I have an example on my github, in particular check out the deploy.sh script and the project projects/elearn which it deploys.

4

You should be able to pass in an existing Git repository into the asset pipeline via

rhc create-app $APPNAME ruby-1.9 --from-code $GIT_LOCATION

The remote Git repository then delivers the initial application for OpenShift.

As a second possibility, you can skip the creation of the local OpenSHift Git repository via

rhc create-app $APPNAME ruby-1.9 --no-git

and then use the steps described above to merge the OpenShift remote Git repository into your local Git repository.

0
4

Mohannd's answer is perfect, but I would like to sum up the complete solution, in case some else needs it:

To use your github repo as an Openshift repo, there is no perfect solution now, because, Openshfit uses git hooks to trigger the deployment or redeployment based on your commits. However, the smartest way would be to use 2 repos (the openshift's one and your github's one) to push simultaneously the code to.

To do this: Add a remote named "all" and add 2 push urls to it.

git remote add all ssh://[email protected]/~/git/yourapp.git
git remote set-url openshift-git-repo --push --add ssh://[email protected]/~/git/yourapp.git
git remote set-url github-repo --push --add [email protected]:youruser/yourapp.git

Then set the remote named 'all' as the default push remote:

git push -u all

To commit and push your code, proceed as usual: It will push on the 2 remotes and deploy on OpenShift

git add .
git commit -m "my commit"
git push

And watch the result:

[master 3fc96b2] my commit
 1 file changed, 2 deletions(-)
MyLaptop:myapp User$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 291 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To [email protected]:User/myapp.git
   a036a44..3fc96b2  master -> master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 291 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Stopping PHP 5.4 cartridge (Apache+mod_php)
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Building git ref 'master', commit 3fc96b2
remote: Preparing build for deployment
remote: Deployment id is 9037d37a
remote: Activating deployment
remote: Starting PHP 5.4 cartridge (Apache+mod_php)
remote: Application directory "/" selected as DocumentRoot
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://[email protected]/~/git/myapp.git/
   a036a44..3fc96b2  master -> master
MyLaptop:myapp User$

Hope this helps

2
  • You have an error. You can have multiple repositories, but they both can't be named "origin". They must be unique, like: origin and origin2
    – Eric P
    Sep 22, 2015 at 13:07
  • using this implementation but i got an error that says No such remote 'openshift-git-repo' .. i think there is a missing on the script above.. Jun 25, 2016 at 19:32
2

There is a way to do what you want, i.e. skip Openshift's repo. What you need to do is set up a jenkins, and have it poll your own repository.

There's a link here that explains how to set it up from scratch: http://blog.anthavio.net/2014/01/deploy-to-openshift-from-github.html

1

I ran into problems deploying a pre-existing code repository to Openshift. In my particular context, where I tried to deploy a tomcat webapp, the Openshift tomcat configuration files included in the .openshift folder were crucial.

What fixed it for me was the inclusion of the .openshift folder in my existing source tree, as well as the inclusion of the openshift profile in my maven pom.xml file.

This is highly likely the same that would happen by merging your repository with the new openshift upstream one. For me, this is the "why" behind following sentence in adietisheim's great answer:

"In order to then be able to push the code from your local git repo to openshift you first have to merge your openshift repo with your local bitbucket clone."

In my case, this merge was needed to get the config files from the .openshift directory. It took a long time to figure out for me because pushing without the .openshift directory still made my app build and deploy successfully. The only behaviour I saw was a report on missing jsp files, which made me think that the problem was related to my own web.xml and servlet configuration.

1

If you're using github you can configure travis to made the deployment each time you made a change in your github repository

http://docs.travis-ci.com/user/deployment/openshift/

0

If you are using java then there's is an alternative approach. But even in this approach you would still use the OpenShift git repository. The git repository provided by OpenShift is how you give OpenShift your code, your deployable(s):

You can - instead of committing your code to the OpenShift git repo - simply give it your war-file. You clone the OpenShift git repo to your local machine. You then build a war from your application source and put this war into the deployments folder within your OpenShift git repo (clone). You then add, commit and push your local clone to OpenShift. Once the push executed successfully, the JBoss AS7 will pick your war and deploy it.

0

TAKE EASY!

step 1: Create app. With your favorite method (from gitRepository, pre-maker Openshift, etc). if you use console metod
step 2: rhc git-clone nameApp
step 3: rhc app-configure nameApp --auto-deploy
step 4: ENJOY!

1
  • That's fine, but that was just not the answer to the question :) May 25, 2020 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.