Need Example of JGit to push code files to git server - selenium

I am trying to push the selenium scripts to git server so that jenkins will automatically download the latest scripts. I wanted to give web interface to select the automation scripts and upload the same scripts in Git. I want to programatically handle the pushing of choosen files to server so that only those files are run for automation. I need an example from JGit.

You use the PushCommand for invoking a push to a remote server. There are options among others for choosing which remote is pushed to.
Keep in mind that you cannot push some files selectively, but only whole commits on a branch, so there is no way to do what you would like with simple push. An option would be to create separate commits on a separate branch and only push this one branch via defining the references via the PushCommand.add() method.
try (Git git = new Git(repository)) {
git.push().call();
}
See also a related snippet in the jgit-cookbook

Related

Need a way to update two gitlab repositories through single intellij project window

I am working on a project which is hosted on a particular gitlab repository. Often we dont get to connect to this repository because of network issues. Hence I have created my own local gitlab repository.
Now to keep the both repositories updated, i have to copy paste the code from one folder linked to one repository to other folder which is linked to other repository.
Is there a way in intellij wherein i can work in the same window but when committing and pushing the changes, both the repositories get updated at the same time ?
Regards,
Thanks in advance
Personally, I think it is far, far easier to use Git from the terminal/powershell.
If you are interested in using the terminal, or powershell, with Git, and want set multiple remote origins, then there is a already a detailed answer about pushing and pulling from multiple remote locations.
Otherwise, look at Intellij's VCS menu, then select "Git."
Then select "remotes":
Then get add your other Git remotes by clicking "+" and add:
Add you should be set. Just select which remote you would like to push to.

Analogue of fetch for Mercurial? How to see a new branch from IntelliJ?

If a branch is created in the repository (Git or Mercurial), I still don't see it in the IntelliJIdea. If the CVS is Git, I have to issue fetch command for to see a new branches.
But for Mercurial I don't see neither fetch, nor something similar. How can I make that new branch visible for IntelliJ?
As I understand, you mean a branch created on a remote server.
It is not IntelliJ specific, you need to get information into your local repository first (this is what you actually do calling pull in Tortoise)
But you don't need to use Tortoise, you can pull from IDEA using VCS - Mercurial - Pull

committing to repository as part of CI build

I have a CI pipeline that is likely doing something semi-perverted. Let's not debate that part.
As part of the CI, I will generate an artifact (README.md) which I would like to commit and push back to the same repository. Simply using git push origin ... doesn't work due to authentication error.
Am I constrained to using something like a secret variable and a token, and adding another remote so that it can push?
There are ways to add a ssh token to your build runtime which is able to commit or even do a push to origin.
I think even recently GitLab added a new functionality that for each build a unique token is generated which can be used for same sake.
However in general I dont think you can commit anything on the same git base that build is running on, as the check out is in a detached head mode. This means you will not be able to add to history, specially in a remote.
Next problem to consider is what this means if you were able to commit back for the build system, which can potentially trigger another build and a cycle will be triggered.
So probably either use the artifact system for it or do add the ssh token and clone/checkout/commit/push in a separate directory during the build. Anyway this doc explains how to add the token: https://docs.gitlab.com/ee/ci/ssh_keys/README.html
Gitlab seems to change .git folder after fetching project for CI job. I'm not sure it changes only remote section. So I found only solution is to add gitlab-runner user with sshkeys to gitlab. And in my job make git clone in separate folder, make changes, then commit and push it.

"Bare" git repository: how can I let Apache always "see" the latest commit?

We've got a "bare" git repository on a server, for a Web portal project. Several programmers, designers, etc... perform dozens of push and pull from/to it.
Now we want to test the project on the server itself, and always test the last commit through an Apache web server which is installed on the same machine the "bare" git repository is stored in.
How can we 'unbare' the repository, and let the working directory contain always and only the last commit deriving from the last push?
Or anything else aiming to achieve the same result?
You can use a post-receive hook to do a git pull inside your webserver document root/repository.
in your bare repository
do
mv hooks/post-receive.sample hooks/post-receive
chmod +x .git/hooks/post-receive
the post receive should be something like
#!/bin/sh
WEB_ROOT='/var/www/project'
cd $WEB_ROOT
git pull
A more elegant solution that doesn't involve that the web server area being a git repository, you can also review the git documentation about hooks
Note: if you use the simple solution, please make sure that your webserver doesn't serve the .git directory, this would give some hackers/crackers the access to the website source code!

How to update my server files from a git repo automatically everyday

I am a noob in these server related work. I am writing some PHP code in my local system and has been updating my repo in github regularly. Each time I want to test my application, I copy all the files from my local system onto my server through FTP and then do it. Now I want to know whether is there a way to automatically make the commits that I make to reflect in the files in the server. Is there a way to automatically make the server get the files from the repo periodically? (say, once everyday).
Can this be done other way, like when I make a push from my local machine, the repo gets updated and in turn the files on the server also get updated?
My Server Details: Apache 2.2.15, Architecture i686 with Linux Kernel 2.6.18-194.32.1.el5
In addition to cronjobs, you can use a post-receive hook: http://help.github.com/post-receive-hooks/
If you have cronjobs you can use them. First set up the repository on your server. Then you can set up the cronjob, choose a time in which it should be executed, and then in the cronjob execute the following command:
cd your/repository/folder; git pull master origin
Perhaps explore the git archive command, which you can use to get a zip file or similar of your code. You could then perhaps use a script to copy that to your (other) server?
git archive --format=zip --output=./src.zip HEAD
will create a zip file called src.zip from the HEAD of your repo
More info:
http://www.kernel.org/pub/software/scm/git/docs/git-archive.html
Do a "git export" (like "svn export")?