Execute Git commands using Java and Docker on Jenkins - java

I am trying to execute some git commands running a java project in a docker image. I already added the git package into the docker file like this:
RUN apk add --no-cache git &&
apk info git
And I have a class in Java in order to execute git commands like this:
public String getCurrentGitBranch() throws IOException, InterruptedException {
return runCommand("git rev-parse --abbrev-ref HEAD");
}
However, when running the project on Jenkins all the information from git is null.
Am I missing somethind?
Could you help me please.
I am expecting to execute the git commands and get the correct information.

There could be a few reasons why the information from git is null when running the project on Jenkins. Some possible reasons are:
The working directory is not set to the repository: Make sure that the working directory is set to the root of the repository where the git repository is located, otherwise git may not be able to find the repository.
The repository is not initialized: Make sure that the repository is initialized and the local branch is checked out.
Jenkins is not running in the correct user context: Jenkins might be running as a different user or with a different environment than your local machine. Ensure that the user running Jenkins has access to the repository and has the correct environment variables set up.
Jenkins does not have access to the git executable: Make sure that the git executable is installed and accessible in the Jenkins environment. You can check this by running which git on the Jenkins machine.
Git configuration is not set up: Make sure that the Git configuration is set up correctly, including the user name and email, otherwise git might not be able to identify the user.
You may also want to add logging statements to your Java code to see if there are any error messages or exceptions being thrown, which could give you a better idea of what's going wrong.

Related

How can I Trigger local maven project from remote Jenkins?

I have created a simple Selenium+java maven project locally on my machine. I am able to execute the testing locally triggering it with Maven.
I want to trigger it from Jenkins (which is installed on a remote machine (my company's QA server))
I am using the option 'custom workspace' of Jenkins.
As the Jenkins is on the server, it's not able to understand/locate the local path
'C:\Automation\MavenProject\'
How I can achieve this?
You can do it with master-slave concepts in Jenkins. Slave machine would be your windows machine which will connect to master Jenkins with the help of some jars. You need to create a node on your Jenkins server and after adding the configuration, you need to download the corresponding slave jars on your machine. Once you execute those jars on your local machine, it will interact with Jenkins server. Your Jenkins job can then perform further activities on your slave machine.
Also in the Jenkins job, you need to refer to this node by enabling the following option.
Restrict where this project can be run
1)Create a Maven Project in Jenkins, you may have to install some plugin if its not visible
Pushing your code to some type of version Control like Github
You need to configure Jenkins to provide GitHub build information and POM Location
Since you Jenkins is hosted up in the server, you have be able to access it public, you need to download Github integration plugin and configure jenkins to provide GitHub hook trigger for GITScm polling and similar action in github itself
This should get you going, also google or youtube, lots of solution , one example
https://www.youtube.com/watch?v=PhxZamqYJws

How to delete local and remote repositories in bitbucket using command line in windows OS?

I am trying delete local and remote repositories in bitbucket using git bash.
I am able to delete my local repository using .git directory in myroot-directory of my repository, but
git remote rm repositoryURL
used to delete remote report but i could not.
Can you please help me
Thanks
You've got a few problems here.
git remote rm repositoryURL
used to delete remote report but i could not.
First, git remote rm is used with the name of the remote, not its URL. You'd have to do something like git remote rm origin.
This updated command won't work because you have already deleted .git/, so you no longer have a local repository. git remote requires a local repository.
But even if you'd run git remote rm before deleting .git/ it wouldn't remove the repository on Bitbucket. The documentation says that git remote rm <name>
Remove the remote named <name>. All remote-tracking branches and configuration settings for the remote are removed.
This simply configures your local copy to forget everything it knows about the remote. The repo on Bitbucket will still exist, e.g., you could clone it again.
To actually remove the repository from Bitbucket you have to log into its web interface and do it there (or possibly use the API).

Maven build outside eclipse, very time consuming

My current Setup:
I have Ecliple Mars installed on Windows 10
I have some repositories checked out in my Project explorer which I basically use for any code changes, debugging purpose.
I have tomcat installed in C directory and have its path configured in the Servers section of eclipse.
So, every time I do any code changes in eclipse, I manually go into the eclipse workspace, and do the maven build using the following commandmvn -Plocal -DskipTests=true clean install, and then manually copy the war generated in the target folder into the webapps directory of my tomcat.
After doing all these, I start my tomcat in debug mode catalina jpda start and then setup Remote Java Application in eclipse for a particular module.
I can then setup breakpoints and see the changes in the debugger.
I am wondering , all of the above steps are time consuming if I have to do frequent code changes and then debug something in Debug mode in eclipse as I have to repeat steps 4 to 6 again and again for any code changes.
Could anyone tell me if there is a quick way to achieve the same goal?
You can run application dirrectly from maven via maven-{your_servlet_container}-plugin (maven-tomcat-plugin, maven-jetty-plugin, etc.). you can also specify debug mode there, so all you need to do is just run maven and connect in eclipse to remote app.
set ENV variable for always run maven in debug:
set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
(unset by setting this value to empty string)
with env variable:
mvn -Plocal -DskipTests=true clean tomcat7:run
or run mvnDebug:
mvnDebug -Plocal -DskipTests=true clean tomcat7:run
If you have your tomcat configured with eclipse properly, then you don't need to perform all these steps. I am not sure if you want to automate all these steps from outside. if yes, then either you can automate above steps with writing all in a batch file & triggering all steps with it (not a standard way) or using maven plugins to automate most of these steps.
Hope this information will help...

Using git to put a project into a repository

I have a new project in Eclipse in the directory ProjectDirectory which contains the src, lib, etc. folders. Note that this project has never before communicated with any repository.
I have a git repository https://github.com/myrepo/mygit.git with a username user and a password pass (note that this is fake info). In that repository, I currently have some files.
I want to do the following.
Remove ALL the current contents in the git repository
Add the new contents from my Eclipse project into this repository. This should result in no conflicts or no errors whatsoever, since I want to completely replace the current repository with these files.
With specific instructions, what set of commands in the terminal would I write to accomplish this?
Its fully simple. First go into the directory in terminal where the eclipse project is.
To start putting this under version managment:
git init
Then add the origin as the git repo ( copy your ssh read+write access from github, this you see when you are loggin in on github and navigates to the actual repositorys directory ):
git remote add origin <copied_adress_from_github>
So now in the last step you will need to overwrite forced the remote repo with:
git push -f origin master
You can start commiting, making branches etc... However if on the remote repo there was other branches you want to remove them with:
git push origin --delete <branch_name>

could the first ever maven build be made offline?

The problem: you have a zipped java project distribution, which depends on several libraries like spring-core, spring-context, jacskon, testng and slf4j. The task is to make the thing buildable offline. It's okay to create project-scope local repo with all required library jars.
I've tried to do that. Looks like even as the project contains the jars it requires for javac and runtime, the build would still require internet access. Maven would still lurk into network to fetch most of its own plugins it requires for the build. I assume that maven is run with empty .m2 directory (as this may be the first launch of the build, which may be an offline build). No, I am not okay with distributing full maven repo snapshot along the project itself, as this looks like an utter mess for me.
A bit of backround: the broader task is to create windows portable-style JDK/IntelliJ Idea distribution which goes along the project and allows for some minimal java coding/running inside IDE with minimal configuration and minimal internet access. The project is targeted towards students in a computer class, with little or no control over system configuration. It is desirable to keep console build system intact for the offline mode, but I guess that maven is overly dependent on the network, so I have to ditch it in favor of good old ant.
So, what's your opinion, could we move first maven build in offline mode completely? My gut feeling is that initial maven distribution just contains the bare minimum required to pull essential plugins off the main repo and is not fully functional without seeing the main repo at least once.
Maven has a '-o' switch which allows you to build offline:
-o,--offline Work offline
Of course, you will need to have your dependencies already cached into your $HOME/.m2/repository for this to build without errors. You can load the dependencies with:
mvn dependency:go-offline
I tried this process and it doesn't seem to fully work. I did a:
rm -rf $HOME/.m2/repository
mvn dependency:go-offline # lot of stuff downloaded
# unplugged my network
# develop stuff
mvn install # errors from missing plugins
What did work however is:
rm -rf $HOME/.m2/repository
mvn install # while still online
# unplugged my network
# develop stuff
mvn install
You could run maven dependency:go-offline on a brand new .m2 repo for the concerned project. This should download everything that maven needs to be able to run offline. If these are then put into a project-scope local repo, you should be able to achieve what you want. I haven't tried this though
Specify a local repository location, either within settings.xml file with <localRepository>...</localRepository> or by running mvn with -Dmaven.repo.local=... parameter.
After initial project build, all necessary artifacts should be cached locally, and you can reference this repository location the same ways, while running other Maven builds in offline mode (mvn -o ...).

Categories