我可以使用我现有的 git 回购与 openshift?

是否有必要只在开班时进行 Git 回购?我已经有 bitbucket/github git repo,并且希望只推送到那里。我可以简单地挂进去,这样开放式轮班就能得到暗示吗?

或者为了简化,我只在 github 上推送,但是当我想要部署时,我使用 openshift 做一些事情?

我确实检查了 这个,但它让我感到困惑: 它是在谈论合并 exiting 和 new (openshift) git 吗?

54539 次浏览

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".

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.

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.

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.

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.

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.

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

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/

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://23456781234567@yourapp-namespace.rhcloud.com/~/git/yourapp.git
git remote set-url openshift-git-repo --push --add ssh://23456781234567@yourapp-namespace.rhcloud.com/~/git/yourapp.git
git remote set-url github-repo --push --add git@github.com: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 git@github.com: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://23456789@myapp-namespace.rhcloud.com/~/git/myapp.git/
a036a44..3fc96b2  master -> master
MyLaptop:myapp User$

Hope this helps

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

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!